我們有一張數據表,需要按照【類別】分組按照【時間】排序,并分組顯示各自的序號。
表Archive
ArchiveId | varchar(30) | 文章編號非數字 |
CategoryId | int | 文章分類Id |
StatusId | int | 狀態,-1表示刪除,0表示新建,1表示啟用 |
PubTime | DateTime | 發布時間 |
select top 100 ArchiveId,StatusId,PubTime,CategoryId from Archive where StatusId>=0order by PubTime desc
查詢結果:
--子表with asm as(select ArchiveId,StatusId,PubTime,CategoryId from Archive where StatusId>=0)--查詢-------------------select bb.ArchiveId,bb.StatusId,bb.PubTime,bb.CategoryId,--序號列生成 (select COUNT(1) from asm where bb.CategoryId=asm.CategoryId and bb.PubTime>=asm.PubTime) rowid --插入臨時表into #temp from Archive bbwhere bb.StatusId>=0
查詢臨時表:
select top 200 * from #temp order by CategoryId desc,rowid asc
見查得結果:
新聞熱點
疑難解答