[CTE] Select mismatch in SQL
See original GitHub issueWith the following CTE I get this error:
Exception message: 'rateCost' has fewer columns than were specified in the column list
The following C# CTE
(from rateEntry in _cargoWiseContext.RateEntries
from rateLine in _cargoWiseContext.RateLines.LeftJoin(lines => lines.TL_TI == rateEntry.TI_PK)
from rateLineItem in _cargoWiseContext.RateLineItems.LeftJoin(items =>
items.TM_TL == rateLine.TL_PK)
where (rateEntry.TI_RateEndDate == null || rateEntry.TI_RateEndDate > Sql.CurrentTimestamp)
&& rateLineItem.TM_Type.In("MIN", "FLT", "BAS", "UNT")
group rateLineItem by rateEntry
into _
select new RateCharges
{
RateEntry = _.Key,
FlatRate = _.Sum(r => r.TM_Type.In("FLT", "BAS") ? r.TM_Value : 0),
MinRate = _.Sum(r => r.TM_Type == "MIN" ? r.TM_Value : 0),
VariableRate = _.Sum(r => r.TM_Type == "UNT" ? r.TM_Value : 0)
}).AsCte("rateCost");
After being used in a join on another query, selecting out most of the fields and joining on a property of RateEntry will generate the following SQL with the error
WITH rateCost
(
TI_TH,
TI_OH_TransportProvider,
cte_field0,
TI_RC,
TI_DestinationLRC,
TI_CartageDeliveryAddressPostCode,
TI_OriginLRC,
TI_CartagePickupAddressPostCode,
TI_RH_NKCommodityCode,
cte_field0,
TI_PK,
TI_LineOrder,
TI_RateStartDate,
TI_RateEndDate,
TI_RX_NKCurrency,
TI_Frequency,
TI_OH_Supplier,
TI_OH_Consignor,
TI_OH_Consignee,
TI_OA_CartagePickupAddressOverride,
TI_OA_CartageDeliveryAddressOverride,
TI_RS_NKServiceLevel_NI,
TI_ViaLRC,
TI_PageHeading,
TI_PageOpeningText,
TI_PageClosingText,
TI_OH_AgentOverride,
TI_ParentID,
TI_QuotePageIncoTerm,
TI_BuyersConsolRateMode,
TI_SystemCreateTimeUtc,
TI_SystemCreateUser,
TI_SystemLastEditTimeUtc,
TI_SystemLastEditUser,
TI_ContractNumber,
TI_PL_NKCarrierServiceLevel,
TI_TZ_OriginZone,
TI_TZ_DestinationZone,
TI_IsValid,
TI_IsCrossTrade,
TI_DataChecked,
TI_MatchContainerRateClass,
TI_TransitTime,
TI_FrequencyUnit,
TI_FromID,
TI_FromTableCode,
TI_ToId,
TI_ToTableCode,
TI_OH_ControllingCustomer,
TI_GC_Publisher,
TI_IsTact,
TI_ParentTableCode,
TI_RateKey,
cte_field0,
cte_field1,
cte_field2,
cte_field3,
cte_field4,
cte_field5,
TI_Mode,
TI_RateCategory
)
AS
(
SELECT rateEntry.TI_TH,
rateEntry.TI_OH_TransportProvider,
rateEntry.TI_RC,
rateEntry.TI_DestinationLRC,
rateEntry.TI_CartageDeliveryAddressPostCode,
rateEntry.TI_OriginLRC,
rateEntry.TI_CartagePickupAddressPostCode,
rateEntry.TI_RH_NKCommodityCode,
rateEntry.TI_RateCategory,
rateEntry.TI_Mode,
rateEntry.TI_PK,
rateEntry.TI_LineOrder,
rateEntry.TI_RateStartDate,
rateEntry.TI_RateEndDate,
rateEntry.TI_RX_NKCurrency,
rateEntry.TI_Frequency,
rateEntry.TI_OH_Supplier,
rateEntry.TI_OH_Consignor,
rateEntry.TI_OH_Consignee,
rateEntry.TI_OA_CartagePickupAddressOverride,
rateEntry.TI_OA_CartageDeliveryAddressOverride,
rateEntry.TI_RS_NKServiceLevel_NI,
rateEntry.TI_ViaLRC,
rateEntry.TI_PageHeading,
rateEntry.TI_PageOpeningText,
rateEntry.TI_PageClosingText,
rateEntry.TI_OH_AgentOverride,
rateEntry.TI_ParentID,
rateEntry.TI_QuotePageIncoTerm,
rateEntry.TI_BuyersConsolRateMode,
rateEntry.TI_SystemCreateTimeUtc,
rateEntry.TI_SystemCreateUser,
rateEntry.TI_SystemLastEditTimeUtc,
rateEntry.TI_SystemLastEditUser,
rateEntry.TI_ContractNumber,
rateEntry.TI_PL_NKCarrierServiceLevel,
rateEntry.TI_TZ_OriginZone,
rateEntry.TI_TZ_DestinationZone,
rateEntry.TI_IsValid,
rateEntry.TI_IsCrossTrade,
rateEntry.TI_DataChecked,
rateEntry.TI_MatchContainerRateClass,
rateEntry.TI_TransitTime,
rateEntry.TI_FrequencyUnit,
rateEntry.TI_FromID,
rateEntry.TI_FromTableCode,
rateEntry.TI_ToId,
rateEntry.TI_ToTableCode,
rateEntry.TI_OH_ControllingCustomer,
rateEntry.TI_GC_Publisher,
rateEntry.TI_IsTact,
rateEntry.TI_ParentTableCode,
rateEntry.TI_RateKey,
Sum(IIF(items.TM_Type IN (N'FLT', N'BAS'), items.TM_Value, 0)),
Sum(IIF(items.TM_Type = N'MIN', items.TM_Value, 0)),
Sum(IIF(items.TM_Type = N'UNT', items.TM_Value, 0)),
Sum(IIF(items.TM_Type = N'UNT', items.TM_Value, 0)),
Sum(IIF(items.TM_Type IN (N'FLT', N'BAS'), items.TM_Value, 0)),
Sum(IIF(items.TM_Type = N'MIN', items.TM_Value, 0))
FROM RateEntry rateEntry
LEFT JOIN RateLines lines ON lines.TL_TI = rateEntry.TI_PK
LEFT JOIN RateLineItems items ON items.TM_TL = lines.TL_PK
WHERE (rateEntry.TI_RateEndDate IS NULL OR rateEntry.TI_RateEndDate > CURRENT_TIMESTAMP)
AND items.TM_Type IN (N'MIN', N'FLT', N'BAS', N'UNT')
GROUP BY rateEntry.TI_PK,
rateEntry.TI_LineOrder,
rateEntry.TI_RateStartDate,
rateEntry.TI_RateEndDate,
rateEntry.TI_RX_NKCurrency,
rateEntry.TI_Frequency,
rateEntry.TI_OH_Supplier,
rateEntry.TI_OH_TransportProvider,
rateEntry.TI_OH_Consignor,
rateEntry.TI_OH_Consignee,
rateEntry.TI_OA_CartagePickupAddressOverride,
rateEntry.TI_OA_CartageDeliveryAddressOverride,
rateEntry.TI_CartagePickupAddressPostCode,
rateEntry.TI_CartageDeliveryAddressPostCode,
rateEntry.TI_RS_NKServiceLevel_NI,
rateEntry.TI_OriginLRC,
rateEntry.TI_DestinationLRC,
rateEntry.TI_ViaLRC,
rateEntry.TI_PageHeading,
rateEntry.TI_PageOpeningText,
rateEntry.TI_PageClosingText,
rateEntry.TI_OH_AgentOverride,
rateEntry.TI_ParentID,
rateEntry.TI_QuotePageIncoTerm,
rateEntry.TI_BuyersConsolRateMode,
rateEntry.TI_SystemCreateTimeUtc,
rateEntry.TI_SystemCreateUser,
rateEntry.TI_SystemLastEditTimeUtc,
rateEntry.TI_SystemLastEditUser,
rateEntry.TI_TH,
rateEntry.TI_RC,
rateEntry.TI_ContractNumber,
rateEntry.TI_PL_NKCarrierServiceLevel,
rateEntry.TI_TZ_OriginZone,
rateEntry.TI_TZ_DestinationZone,
rateEntry.TI_IsValid,
rateEntry.TI_IsCrossTrade,
rateEntry.TI_DataChecked,
rateEntry.TI_MatchContainerRateClass,
rateEntry.TI_TransitTime,
rateEntry.TI_FrequencyUnit,
rateEntry.TI_Mode,
rateEntry.TI_RH_NKCommodityCode,
rateEntry.TI_RateCategory,
rateEntry.TI_FromID,
rateEntry.TI_FromTableCode,
rateEntry.TI_ToId,
rateEntry.TI_ToTableCode,
rateEntry.TI_OH_ControllingCustomer,
rateEntry.TI_GC_Publisher,
rateEntry.TI_IsTact,
rateEntry.TI_ParentTableCode,
rateEntry.TI_RateKey
)
I’ve tried to reproduce it in a simpler form, but I’m not exactly sure what’s causing it.
Environment details
linq2db version: 2.6.4
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
CTE error: "Types don't match between the anchor and ...
Exactly what it says: 'name1' has a different data type to 'name' + CAST((rn+1) as varchar(255)). Try this (untested) ;with cte as (...
Read more >Type mismatch between anchor point and recursive ...
Someone explained why this happens in CTE. Type mismatch between anchor point and recursive section in the "TXT" column of recursive query ......
Read more >Recursive CTE and Type Mismatch
I am in the process of converting a complex Access database over to SQL Server in order to teach myself. As mentioned this...
Read more >Find Mismatched SQL Server Data Between Two Tables
In this tip we look at various ways to find mismatched SQL Server data between two tables using LEFT JOIN, EXCEPT, NOT IN...
Read more >Error while executing CTE query: Argument type mismatch
I am facing an error while trying to write a CTE query in hive. Even though a column is defined with "Date", system...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Corrected variant. For sure not tested, but it should work )
Here you go, I’ve removed a lot of stuff that wasn’t used from
RateLines
andRateLineItems