开发者

issue with MAX statement in t-sql code

This is the code:

Select BCPP.*
from ViewPBCPP   BCPP
inner join
( 
Select MBC.PC     PC
      ,MRT.Name   CT
      ,Max(dbo.CalcDatefromUTC(MBC.CreatedDate)) as LRDate
from TableBACC    MBC

inner join TableSC.RT   MRT
ON MBC.RTid = MRT.id
where MBC.Isdeleted = 'False'
and MBC.PC <> 'NULL'
Group by MBC.PC
        ,MRT.Name
) MBCR
ON BCPP.P_id = MBCR.PC
and BCPP.CreatedDate = MBCR.LRDate
and BCPP.CT = MBCR.CT

Now Max(dbo.CalcDatefromUTC(MBC.CreatedDate)) is actually a function

Query above works fine with Max(dbo.CalcDatefromUTC(MBC.CreatedDate))

Now when I write

Max(dbo.CalcDatefromUTC(MBC.CreatedD开发者_开发技巧ate)) + Min(dbo.CalcDatefromUTC(MBC.CreatedDate))

I cannot extract any values at all from this query written above

If I write just

(dbo.CalcDatefromUTC(MBC.CreatedDate))

it gives me error that it does not contained aggregate function or the group by function

I actually want this

(dbo.CalcDatefromUTC(MBC.CreatedDate))

so that I can use all the values of this function rather than just MAX values of it

How can I change this code written above to achieve my objective??

Anyone??


You can't have dbo.CalcDatefromUTC(MBC.CreatedDate) in the SELECT list as neither you can have MBC.CreatedDate because it's not in the GROUP BY list.

You can have MAX(MBC.CreatedDate) though because it uses an aggregate function (MAX) on thta column. You can also have:

dbo.CalcDatefromUTC(MAX(MBC.CreatedDate)) as LRDate

which is the same actually (although maybe a bit faster), as:

MAX(dbo.CalcDatefromUTC(MBC.CreatedDate)) as LRDate

From your comments, I assume the above is not very helpful. Perhaps grouping by MBC.CreatedDate as well is what you want:

Select BCPP.*
from ViewPBCPP   BCPP
inner join
( 
Select MBC.PC     PC
      ,MRT.Name   CT
      ,dbo.CalcDatefromUTC(MBC.CreatedDate) as LRDate
from TableBACC    MBC

inner join TableSC.RT   MRT
ON MBC.RTid = MRT.id
where MBC.Isdeleted = 'False'
and MBC.PC <> 'NULL'
Group by MBC.PC
        ,MBC.CreatedDate                  --- added
        ,MRT.Name
) MBCR
ON BCPP.P_id = MBCR.PC
and BCPP.CreatedDate = MBCR.LRDate
and BCPP.CT = MBCR.CT
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜