什么SQL Server(T-SQL)查询可以获取此结果?

时间:2017-11-26 04:20:07

标签: sql sql-server sql-server-2008 tsql

我有一个包含40,000条记录的数据集。格式如下所示。

Title  | Publication Date
-------|-----------------
Book-A |   2016-10-20
Book-A |   2017-08-14
Book-B |   2016-09-22
Book-B |   2017-03-16
Book-B |   2017-11-26
Book-C |   2016-06-13
Book-C |   2017-04-15
Book-C |   2016-11-23
Book-C |   2017-12-28

我希望SQL查询使用最新发布日期(如下所示)返回唯一图书标题

Title  | Publication Date
-------|-----------------
Book-A |   2017-08-14
Book-B |   2017-11-26
Book-C |   2017-12-28

实现此目的的T-SQL查询是什么?

3 个答案:

答案 0 :(得分:3)

SELECT [Title],
       Max([Publication Date]) [Publication Date]
FROM   [DataSet]
GROUP  BY [Title]

答案 1 :(得分:0)

一个选项使用排名分析函数:

SELECT Title, [Publication Date]
FROM
(
    SELECT Title, [Publication Date],
        RANK() OVER (PARTITION BY Title ORDER BY [Publication Date] DESC) rank
    FROM yourTable
) t
WHERE rank = 1
ORDER BY Title;

如果最新日期出现平局,此方法将捕获给定标题的多个记录。如果您坚持每个标题使用一条最新记录,则可以将RANK替换为ROW_NUMBER。更好的是,保留RANK并为ORDER BY子句添加逻辑,从而打破平局。

答案 2 :(得分:0)

使用聚合功能MAX(),如

CREATE TABLE Table1(
    Title [nvarchar](50) NULL,
    PublicationDate [datetime] NULL
)

Insert into Table1 values('Book-A', '2016-10-20')
Insert into Table1 values('Book-A', '2017-08-14')
Insert into Table1 values('Book-A', '2016-09-22')
Insert into Table1 values('Book-B', '2017-03-16')
Insert into Table1 values('Book-B', '2017-11-26')
Insert into Table1 values('Book-C', '2016-06-13')
Insert into Table1 values('Book-C', '2017-04-15')
Insert into Table1 values('Book-C', '2016-11-23')
Insert into Table1 values('Book-C', '2017-12-28')

Select Title, format(Max(PublicationDate), 'yyyy-MM-dd') as [Publication Date]
from Table1 group by Title

Drop Table Table1