Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Get the latest per one column

$
0
0
I have the following tableDECLARE @TABLE1 TABLE (D_ID int, C_ID int, C_HIST_ID int)INSERT INTO @TABLE1VALUES(1000016,71,77),(1000017,71,547),(1000017,71,553),(1000017,71,564),(1000013,71,565),(1000017,71,3329),(1000017,71,3330)SELECT * FROM @TABLE1 ORDER BY C_HIST_IDD_ID C_ID C_HIST_ID1000016 71 771000017 71 5471000017 71 5531000017 71 5641000013 71 5651000017 71 33291000017 71 3330I was able to get the following resultSELECT D_ID,C_ID,C_HIST_ID FROM @TABLE1 ACHWHERE ACH.C_ID= 71 AND ACH.C_HIST_ID IN (SELECT MAX(ACH1.C_HIST_ID) FROM @TABLE1 ACH1 WHERE ACH1.C_ID = ACH.C_ID AND ACH.D_ID = ACH1.D_ID )ORDER BY C_HIST_IDD_ID C_ID C_HIST_ID1000016 71 771000013 71 5651000017 71 3330But the result I need is D_ID C_ID C_HIST_ID1000016 71 771000017 71 5641000013 71 5651000017 71 3330C_HIST_ID = 564, which I'm unable to get it through my query, there is no other way I could think of and that is the reason I'm looking for any suggestions here:-)

Viewing all articles
Browse latest Browse all 3145

Trending Articles