question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[CTE] Select mismatch in SQL

See original GitHub issue

With 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:closed
  • Created 5 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Feb 4, 2019

Corrected variant. For sure not tested, but it should work )

var summary = 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")
    select new 
    {
        RateEntry = rateEntry,
        FlatRate = Sql.Ext.Sum(rateLineItem.TM_Type.In("FLT", "BAS") ? rateLineItem.TM_Value : 0)
                .Over().PartitionBy(rateEntry.TI_PK).ToValue(),
        MinRate = Sql.Ext.Sum(rateLineItem.TM_Type == "MIN" ? rateLineItem.TM_Value : 0)
                .Over().PartitionBy(rateEntry.TI_PK).ToValue(),
        VariableRate = Sql.Ext.Sum(rateLineItem.TM_Type == "UNT" ? rateLineItem.TM_Value : 0)
                .Over().PartitionBy(rateEntry.TI_PK).ToValue(),
        RN = Sql.Ext.RowNumber()
                .Over().PartitionBy(rateEntry.TI_PK).OrderBy(rateLineItem.TM_TL).ToValue(),
    };

var cteQuery = (from s in summary
        where s.RN == 1
        select new RateCharges
        {
                s.RateEntry,
                s.FlatRate,
                s.MinRate,
                s.VariableRate,
        }).AsCte("rateCost");
0reactions
hahn-kevcommented, Feb 25, 2019

Here you go, I’ve removed a lot of stuff that wasn’t used from RateLines and RateLineItems

private class RateCharges
{
    public RateEntry RateEntry { get; set; }
    public decimal FlatRate { get; set; }
    public decimal MinRate { get; set; }
    public decimal VariableRate { get; set; }
}

public class RateEntry
{
    public const string RateCategoryOrigin = "ORG";
    public const string RateCategoryDestination = "DST";
    public const string HazardoudsCommodityCode = "HAZ";

    public Guid TI_PK { get; set; }
    public short TI_LineOrder { get; set; }
    public DateTime TI_RateStartDate { get; set; }
    public DateTime? TI_RateEndDate { get; set; }
    public string TI_RX_NKCurrency { get; set; }
    public int TI_Frequency { get; set; }
    public Guid? TI_OH_Supplier { get; set; }
    public Guid? TI_OH_TransportProvider { get; set; }
    public Guid? TI_OH_Consignor { get; set; }
    public Guid? TI_OH_Consignee { get; set; }
    public Guid? TI_OA_CartagePickupAddressOverride { get; set; }
    public Guid? TI_OA_CartageDeliveryAddressOverride { get; set; }
    public string TI_CartagePickupAddressPostCode { get; set; }
    public string TI_CartageDeliveryAddressPostCode { get; set; }
    public string TI_RS_NKServiceLevel_NI { get; set; }
    public string TI_OriginLRC { get; set; }
    public string TI_DestinationLRC { get; set; }
    public string TI_ViaLRC { get; set; }
    public string TI_PageHeading { get; set; }
    public string TI_PageOpeningText { get; set; }
    public string TI_PageClosingText { get; set; }
    public Guid? TI_OH_AgentOverride { get; set; }
    public Guid? TI_ParentID { get; set; }
    public string TI_QuotePageIncoTerm { get; set; }
    public string TI_BuyersConsolRateMode { get; set; }
    public DateTime? TI_SystemCreateTimeUtc { get; set; }
    public string TI_SystemCreateUser { get; set; }
    public DateTime? TI_SystemLastEditTimeUtc { get; set; }
    public string TI_SystemLastEditUser { get; set; }
    public Guid TI_TH { get; set; }
    public Guid? TI_RC { get; set; }
    public string TI_ContractNumber { get; set; }
    public string TI_PL_NKCarrierServiceLevel { get; set; }
    public Guid? TI_TZ_OriginZone { get; set; }
    public Guid? TI_TZ_DestinationZone { get; set; }
    public bool TI_IsValid { get; set; }
    public bool TI_IsCrossTrade { get; set; }
    public bool TI_DataChecked { get; set; }
    public bool TI_MatchContainerRateClass { get; set; }
    public string TI_TransitTime { get; set; }
    public string TI_FrequencyUnit { get; set; }

    [ColumnAlias(nameof(TI_Mode)), Column(IsColumn = false)]
    public TransportType? TI_ModeTT { get; set; }

    public string TI_Mode { get; set; }
    public string TI_RH_NKCommodityCode { get; set; }
    public string TI_RateCategory { get; set; }

    [ColumnAlias(nameof(TI_RateCategory)), Column(IsColumn = false)]
    public TransportType? TI_RateCategoryTT { get; set; }

    public Guid? TI_FromID { get; set; }
    public string TI_FromTableCode { get; set; }
    public Guid? TI_ToId { get; set; }
    public string TI_ToTableCode { get; set; }
    public Guid? TI_OH_ControllingCustomer { get; set; }
    public Guid TI_GC_Publisher { get; set; }
    public bool TI_IsTact { get; set; }
    public string TI_ParentTableCode { get; set; }
    public int TI_RateKey { get; set; }

    [ExpressionMethod(nameof(GetModeExpression))]
    public TransportType? GetMode()
    {
        switch (TI_RateCategory)
        {
            case RateEntry.RateCategoryDestination:
            case RateEntry.RateCategoryOrigin:
                return TI_ModeTT;
            default:
                return TI_RateCategoryTT;
        }
    }

    private static Expression<Func<RateEntry, TransportType?>> GetModeExpression()
    {
        return rateEntry => rateEntry.TI_RateCategory == RateEntry.RateCategoryDestination
                            || rateEntry.TI_RateCategory == RateEntry.RateCategoryOrigin
            ? rateEntry.TI_ModeTT
            : rateEntry.TI_RateCategoryTT;
    }
}

public class RateLines
{
    public Guid TL_PK { get; set; }
//a bunch of properties removed
    public Guid TL_TI { get; set; }
}

public class RateLineItem
{
    public Guid TM_PK { get; set; }
    public byte TM_LineOrder { get; set; }
    public string TM_Type { get; set; }
    public decimal TM_Value { get; set; }
    public Guid TM_TL { get; set; }
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found