Previously found that more classic sentences, are recorded on the computer, I want to move to the blog today, in my opinion, blogging is really a very headache, it is the heart of a hurdle, I have to cross it.
1 CREATE TABLET_jeff (2Idint not NULL,3Vinnumbervarchar(255)DEFAULT NULL,4Channelidvarchar(255)DEFAULT NULL,5 TimeStamp datetime DEFAULT NULL,6 PRIMARY KEY(ID)7 ) ;8 INSERT intoT_jeffVALUES('1','Group3','3','2015-05-27 00:00:00');9 INSERT intoT_jeffVALUES('2','Group2','5','2015-05-23 00:00:00');Ten INSERT intoT_jeffVALUES('3','Group1','4','2015-05-25 00:00:00'); One INSERT intoT_jeffVALUES('4','Group2','3','2015-05-29 00:00:00'); A INSERT intoT_jeffVALUES('7','Group1','2','2015-05-30 00:00:00'); - - the SELECT * fromT_jeff - --1, Vinnumber the same group of put together - --2. Sorting logic between groups: in reverse order of each group Channelid maximum value - --3, the group within the order: In accordance with channelid reverse. + - SELECTA.* + from [dbo].[T_jeff]A A JOIN(SELECT MAX([Channelid]) as [Maxchannelid] , at [Vinnumber] - from [dbo].[T_jeff] - GROUP by [Vinnumber] -) B onA.vinnumber=B.vinnumber - ORDER byB.maxchannelidDESC , -A.channelidDESC in - to
Log SQL query statements once