Incorrect prefix error (select count statement)

  • Can somebody check my naming convention and see what am I doing wrong here:

    ,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and c2.DateContractFunded < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedLastYear

    FROM Channels AS C

    INNER JOIN ChannelContacts AS CC ON C.ChannelID = CC.ChannelID

    INNER JOIN ChannelProductPlan AS CPP ON C.ChannelID = CPP.ChannelID

    INNER JOIN tblLuMktReps AS MR ON C.MarketRepID = MR.MarketRepID

    INNER JOIN tblLuHoldingCo AS HC ON C.HoldingCoID = HC.HoldingCoID

    Error message:

    Msg 107, Level 16, State 3, Line 1

    The column prefix 'Channels' does not match with a table name or alias name used in the query.

  • SQLPain (10/7/2015)


    Can somebody check my naming convention and see what am I doing wrong here:

    ,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and c2.DateContractFunded < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedLastYear

    FROM Channels AS C

    INNER JOIN ChannelContacts AS CC ON C.ChannelID = CC.ChannelID

    INNER JOIN ChannelProductPlan AS CPP ON C.ChannelID = CPP.ChannelID

    INNER JOIN tblLuMktReps AS MR ON C.MarketRepID = MR.MarketRepID

    INNER JOIN tblLuHoldingCo AS HC ON C.HoldingCoID = HC.HoldingCoID

    Error message:

    Msg 107, Level 16, State 3, Line 1

    The column prefix 'Channels' does not match with a table name or alias name used in the query.

    You aliased the table "Channels" as C, but are using "Channels.ChannelID" in the WHERE clause.

    WHERE Channels.ChannelID = c.ChannelID

    This doesn't look right. Does it even need to be there?

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • You aliased table Chanels as c, effectively hiding Chanels so you can't use it anymore. Replace Chanels. with c.

    Gerald Britton, Pluralsight courses

  • Also,

    I have the following select statement and it is working fine, I cant understand the logic behind it and not sure why is it even working as I haven't aliased channels.

    ,(SELECT nameshort FROM channels WHERE channelid = c.assignedbranch) AS Facilitator

  • Here is the whole query, everything seems fine except for the count statements & Facilitator column in the select statement(although the facilitator column is giving me somehow the expected results)

    Can someone review the count statements:

    SELECT DISTINCT

    C.OldDealerID

    ,C.NameLong

    ,C.NameShort

    ,C.MailingState

    ,CC.NameFirst

    ,CC.NameLast

    ,CC.Phone

    ,CC.FaxPhone

    ,MR.Name

    ,CC.Address

    ,CC.City

    ,CC.Zip

    ,C.MailingStreet

    ,C.MailingCity

    ,CC.EMailAddress

    ,(SELECT nameshort FROM channels WHERE channelid = c.assignedbranch) AS Facilitator

    ,C.DateLastReview

    ,HC.HoldingCoDescription

    --Funded, Approved,Conditioned, Declined

    /*,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and c2.DateContractFunded < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedLastYear

    ,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedThisYear

    */

    FROM Channels AS C

    INNER JOIN ChannelContacts AS CC ON C.ChannelID = CC.ChannelID

    INNER JOIN ChannelProductPlan AS CPP ON C.ChannelID = CPP.ChannelID

    INNER JOIN tblLuMktReps AS MR ON C.MarketRepID = MR.MarketRepID

    INNER JOIN tblLuHoldingCo AS HC ON C.HoldingCoID = HC.HoldingCoID

    WHERE

    C.ChannelType IN (1, 6)

    AND C.OldDealerID <> 1234

    AND C.ActiveFlag = 'Y'

    AND CC.Relationship = 1

    ORDER BY C.OldDealerID

  • This part:

    Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID

    makes no sense to me. First you aliased the Channels table as c "From Channels c left join..." then you try to refer to the Channels table in the where clause "Where Channels.ChannelID = c.ChannelID". You can't use Channels there, since you aliased it to c. However if you fix it like this:

    Where c.ChannelID = c.ChannelID

    then the where clause is always true. That can't be what you want

    Gerald Britton, Pluralsight courses

  • Try changing your code to this:

    SELECT DISTINCT

    C.OldDealerID

    ,C.NameLong

    ,C.NameShort

    ,C.MailingState

    ,CC.NameFirst

    ,CC.NameLast

    ,CC.Phone

    ,CC.FaxPhone

    ,MR.Name

    ,CC.Address

    ,CC.City

    ,CC.Zip

    ,C.MailingStreet

    ,C.MailingCity

    ,CC.EMailAddress

    ,C2.nameshort

    --,(SELECT nameshort FROM channels WHERE channelid = c.assignedbranch) AS Facilitator

    ,C.DateLastReview

    ,HC.HoldingCoDescription

    --Funded, Approved,Conditioned, Declined

    /*,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and c2.DateContractFunded < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedLastYear

    ,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedThisYear

    */

    FROM Channels AS C

    INNER JOIN ChannelContacts AS CC ON C.ChannelID = CC.ChannelID

    INNER JOIN ChannelProductPlan AS CPP ON C.ChannelID = CPP.ChannelID

    INNER JOIN tblLuMktReps AS MR ON C.MarketRepID = MR.MarketRepID

    INNER JOIN tblLuHoldingCo AS HC ON C.HoldingCoID = HC.HoldingCoID

    INNER JOIN channels C2 ON c.assignedbranch = C2.channelid

    WHERE

    C.ChannelType IN (1, 6)

    AND C.OldDealerID <> 1234

    AND C.ActiveFlag = 'Y'

    AND CC.Relationship = 1

    ORDER BY C.OldDealerID

    The in-line query essentially causes a loop.

    Why are you using DISTINCT?

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • I know it totally makes no sense at all.

    this is something I have inherited:

    OLD CODE:

    Select Distinct

    OldDealerID

    ,NameLong

    ,NameShort

    ,Channels.MailingState

    ,NameFirst

    ,NameLast

    ,Phone

    ,FaxPhone

    ,Name

    ,ChannelContacts.Address

    ,ChannelContacts.City

    ,ChannelContacts.Zip

    ,Channels.MailingStreet as 'Physical Address'

    ,Channels.MailingCity as 'Physical City'

    ,ChannelContacts.EMailAddress

    ,(select NameShort from Channels c where c.channelID = Channels.assignedbranch)as Facilitator

    ,Channels.DateLastReview

    ,HoldingCoDescription

    --Funded, Approved,Conditioned, Declined

    ,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and c2.DateContractFunded < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedLastYear

    ,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedThisYear

    From channels,ChannelProductPlan,ChannelContacts,tblLuMktReps,tblLuHoldingCo

    Where ActiveFlag = 'Y' and channels.ChannelID = ChannelProductPlan.ChannelID and channels.ChannelID = ChannelContacts.ChannelID and Relationship = 1 and channels.MarketRepID = tblLuMktReps.MarketRepID and Channels.OldDealerID <> 1234 and Channeltype = 1 and channels.HoldingCoID = tblLuHoldingCo.HoldingCoID

    or ActiveFlag = 'Y' and channels.ChannelID = ChannelProductPlan.ChannelID and channels.ChannelID = ChannelContacts.ChannelID and Relationship = 1 and channels.MarketRepID = tblLuMktReps.MarketRepID and Channels.OldDealerID <> 1234 and Channeltype = 6 and channels.HoldingCoID = tblLuHoldingCo.HoldingCoID

    Order By OldDealerID

    Changes I have made so far, I am stuck on the count statements now.

    SELECT DISTINCT

    C.OldDealerID

    ,C.NameLong

    ,C.NameShort

    ,C.MailingState

    ,CC.NameFirst

    ,CC.NameLast

    ,CC.Phone

    ,CC.FaxPhone

    ,MR.Name

    ,CC.Address

    ,CC.City

    ,CC.Zip

    ,C.MailingStreet

    ,C.MailingCity

    ,CC.EMailAddress

    ,(SELECT nameshort FROM channels WHERE channelid = c.assignedbranch) AS Facilitator

    ,C.DateLastReview

    ,HC.HoldingCoDescription

    --Funded, Approved,Conditioned, Declined

    ,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where C.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and c2.DateContractFunded < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedLastYear

    /*,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedLastYear

    ,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedThisYear

    */

    FROM Channels AS C

    INNER JOIN ChannelContacts AS CC ON C.ChannelID = CC.ChannelID

    INNER JOIN ChannelProductPlan AS CPP ON C.ChannelID = CPP.ChannelID

    INNER JOIN tblLuMktReps AS MR ON C.MarketRepID = MR.MarketRepID

    INNER JOIN tblLuHoldingCo AS HC ON C.HoldingCoID = HC.HoldingCoID

    WHERE

    C.ChannelType IN (1, 6)

    AND C.OldDealerID <> 1234

    AND C.ActiveFlag = 'Y'

    AND CC.Relationship = 1

    ORDER BY C.OldDealerID

  • Thank you Michael, you solved one problem with the self joins, the major concern is the count statements:

    I need distinct because there are some repetitive values.

    when I changed the code to this:

    (Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where c.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and c2.DateContractFunded < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedLastYear

    From this:

    (Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and c2.DateContractFunded < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedLastYear

    The results for Funded last year columns changes significantly

  • that's he old code which is actually giving out expected results:

    ,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and c2.DateContractFunded < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedLastYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) and DateDetailDisposed < (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedLastYear

    ,(Select Count(C2.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join Contracts2 c2 on a.AppID = c2.AppID Where Channels.ChannelID = c.ChannelID and c2.DateContractFunded > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0))) ) As FundedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'A' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ApprovedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail ='I' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As ConditionedThisYear

    ,(Select Count(ad.AppID) From Channels c left join Applications a on c.ChannelID = a.SourceID left join AppDetails ad on a.AppID = ad.AppID Where Channels.ChannelID = c.ChannelID and StatusAppDetail = 'D' and DateDetailDisposed > (Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() ), 0)))) As DeclinedThisYear

    From channels,ChannelProductPlan,ChannelContacts,tblLuMktReps,tblLuHoldingCo

    Where ActiveFlag = 'Y' and channels.ChannelID = ChannelProductPlan.ChannelID and channels.ChannelID = ChannelContacts.ChannelID and Relationship = 1 and channels.MarketRepID = tblLuMktReps.MarketRepID and Channels.OldDealerID <> 1234 and Channeltype = 1 and channels.HoldingCoID = tblLuHoldingCo.HoldingCoID

    or ActiveFlag = 'Y' and channels.ChannelID = ChannelProductPlan.ChannelID and channels.ChannelID = ChannelContacts.ChannelID and Relationship = 1 and channels.MarketRepID = tblLuMktReps.MarketRepID and Channels.OldDealerID <> 1234 and Channeltype = 6 and channels.HoldingCoID = tblLuHoldingCo.HoldingCoID

    Order By OldDealerID

  • Sorry, but you are guessing at a solution. And you are probably a bit stressed right now. Those two things are a good way to turn out an incorrect report.

    Distinct is rarely a solution.

    You need the number of applications that were funded last calendar year, for each dealer, correct?

    Try this.

    Write the query that gives you the count of funded applications, by dealer, for last year. Use GROUP BY.

    Left join that subquery to the rest of the query.

    Add the field that I called "FundedLastYear_Count" to the SELECT in the original query.

    I may have the join and group by conditions incorrect, but I'll bet it works or it's darn close.

    Attaching the schema and some sample data, like I reference in my signature, would have made this a lot easier.

    Like this:

    SELECT

    C.OldDealerID

    ,C.NameLong

    ,C.NameShort

    ,C.MailingState

    ,CC.NameFirst

    ,CC.NameLast

    ,CC.Phone

    ,CC.FaxPhone

    ,MR.Name

    ,CC.Address

    ,CC.City

    ,CC.Zip

    ,C.MailingStreet

    ,C.MailingCity

    ,CC.EMailAddress

    ,C2.nameshort

    --,(SELECT nameshort FROM channels WHERE channelid = c.assignedbranch) AS Facilitator

    ,C.DateLastReview

    ,HC.HoldingCoDescription

    --Funded, Approved,Conditioned, Declined

    FROM Channels AS C

    INNER JOIN ChannelContacts AS CC ON C.ChannelID = CC.ChannelID

    INNER JOIN ChannelProductPlan AS CPP ON C.ChannelID = CPP.ChannelID

    INNER JOIN tblLuMktReps AS MR ON C.MarketRepID = MR.MarketRepID

    INNER JOIN tblLuHoldingCo AS HC ON C.HoldingCoID = HC.HoldingCoID

    INNER JOIN channels C2 ON c.assignedbranch = C2.channelid

    LEFT JOIN (SELECT

    Count(C2.AppID as FundedLastYear_Count,

    C.OldDealerID

    FROM Channels c

    LEFT JOIN Applications a ON c.ChannelID = a.SourceID

    LEFT JOIN Contracts2 c2 ON a.AppID = c2.AppID

    WHERE c2.DateContractFunded >= dateadd(yy, - 1, DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0))

    AND c2.DateContractFunded < DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0)

    GROUP BY C.OldDealerID) AS FundedLastYear ON C.OldDealerID = FundedLastYear.OldDealerID

    WHERE

    C.ChannelType IN (1, 6)

    AND C.OldDealerID <> 1234

    AND C.ActiveFlag = 'Y'

    AND CC.Relationship = 1

    ORDER BY C.OldDealerID

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • Thanks again Michael,

    Yes you are absolutely right we have to use the group by clause and also I am stressed out too.

    On the select statement I added:

    ,FundedLastYear_Count

    I am still getting the error on line 31 (Line 31: Incorrect syntax near ','.)

    LEFT JOIN (SELECT

    Count(C2.AppID as FundedLastYear_Count, C.OldDealerID -----Line 31

    FROM Channels c

    LEFT JOIN Applications a ON c.ChannelID = a.SourceID

    LEFT JOIN Contracts2 c2 ON a.AppID = c2.AppID

    WHERE c2.DateContractFunded >= dateadd(yy, - 1, DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0))

    AND c2.DateContractFunded < DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0)

    GROUP BY C.OldDealerID) AS FundedLastYear ON C.OldDealerID = FundedLastYear.OldDealerID

    If we can solve this puzzle that would be the best thing ever

  • I tried putting the following directly in the select statement but it didn't worked either and gave me the error, when I run the statement independently it works fine 🙁

    (SELECT

    Count(C2.AppID)

    FROM Channels c

    LEFT JOIN Applications a ON c.ChannelID = a.SourceID

    LEFT JOIN Contracts2 c2 ON a.AppID = c2.AppID

    WHERE c2.DateContractFunded >= dateadd(yy, - 1, DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0))

    AND c2.DateContractFunded < DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0)

    GROUP BY C.OldDealerID)

    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

    (0 row(s) affected)

  • SQLPain (10/7/2015)


    I tried putting the following directly in the select statement but it didn't worked either and gave me the error, when I run the statement independently it works fine 🙁

    (SELECT

    Count(C2.AppID)

    FROM Channels c

    LEFT JOIN Applications a ON c.ChannelID = a.SourceID

    LEFT JOIN Contracts2 c2 ON a.AppID = c2.AppID

    WHERE c2.DateContractFunded >= dateadd(yy, - 1, DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0))

    AND c2.DateContractFunded < DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0)

    GROUP BY C.OldDealerID)

    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

    (0 row(s) affected)

    We need to see the whole query, not just this subquery.

    Based on the error, I believe this is returning more than one row, but you're trying to use it inside another statement where only one returned row is acceptable.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • SQLPain (10/7/2015)


    I tried putting the following directly in the select statement but it didn't worked either and gave me the error, when I run the statement independently it works fine 🙁

    (SELECT

    Count(C2.AppID)

    FROM Channels c

    LEFT JOIN Applications a ON c.ChannelID = a.SourceID

    LEFT JOIN Contracts2 c2 ON a.AppID = c2.AppID

    WHERE c2.DateContractFunded >= dateadd(yy, - 1, DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0))

    AND c2.DateContractFunded < DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0)

    GROUP BY C.OldDealerID)

    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

    (0 row(s) affected)

    For starters, you are still trying to get this working by trial and error. With more error. You are not attempting to determine the problem.

    We cannot see what you see. We cannot run your code because we have nothing.

    We are trying to get you to fish, as opposed to just feeding you.

    If you would have looked, I left out a right parenthesis

    This line:

    Count(C2.AppID as FundedLastYear_Count,

    Needs to be this:

    Count(C2.AppID) as FundedLastYear_Count,

    That will fix it.

    Secondly, do you routinely write your queries as a series of in-line statements? If so, then you probably need to spend some time learning. This is not a good way to write a query. It is a sure-fire way to create reports that are incorrect.

    When you moved the code I gave you back to an in-line statement, it returned an error. Do you understand this error? This statement is executed for each row returned in the main query. I has to return 1 row per row in the main query. This one returns many rows for each row. That is an error.

    We can help you fix this. But we cannot keep guessing. Please refer to the link in my signature. Send us some DDL statements and some sample data.

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

Viewing 15 posts - 1 through 15 (of 24 total)

You must be logged in to reply to this topic. Login to reply