tsql - How to get first available value in a column (not MIN) using aggregate function on a NVARCHAR type column? -
i have temp table (#temptable) looks this:
aid starttime endtime startsid endsid name
79f05d45 2013-07-02 2013-07-03 1226349 1227338 79f05d45 2013-07-03 2013-07-03 1227381 1227901 79f05d45 2013-07-03 2013-07-03 1233976 1233977 john pringle 79f05d45 2013-07-03 2013-07-03 1234386 1234452 79f05d45 2013-07-03 2013-07-03 1235138 1235147 79f05d45 2013-07-03 2013-07-03 1235669 1235708 mike gordon 79f05d45 2013-07-03 2013-07-03 1235828 1239004 jeff smith
how use aggregate function on name when group by
starttime , endtime john pringle in name column (i tried using min(name)
gives me blank value , if use max(name)
jeff smith )
basically, requirement first available value in column name
here query:
select tt.aid, min(starttime) starttime, max(endtime) endtime, min(startssid) startsid, max(endssid) endsid, -- tried min(name) here empty cell, max(name) gives wrong result. want first available value in name column here (which john pringle). #temptable tt group tt.aid, starttime, endtime
result get:
aid starttime endtime startsid endsid name 79f05d45 2013-07-02 2013-07-03 1226349 1227338 79f05d45 2013-07-03 2013-07-03 1227381 1239004
result aiming for:
aid starttime endtime startsid endsid name 79f05d45 2013-07-02 2013-07-03 1226349 1227338 79f05d45 2013-07-03 2013-07-03 1227381 1239004 john pringle
thanks!
;with ( select aid, starttime, endtime, startssid, endssid, rn = row_number() on (partition aid,cast(starttime date) order case when name = '' or name null '20990101' else starttime end, starttime) #temptable tt ) select aid, starttime, endtime, startssid, endssid rn = 1
Comments
Post a Comment