简体   繁体   中英

What SQL Server (T-SQL) Query can fetch this result?

I have a dataset with 40,000 records. The format is as shown below.

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

I want the SQL query to return unique book titles with the latest publication date (like below)

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

What will be the T-SQL query to achieve this?

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

One option uses a rank analytic function:

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;

This approach would capture mutliple records for a given title should a tie occur for the latest date. If you insist on a single latest record per title, you can replace RANK with ROW_NUMBER . Better yet, keep RANK and add logic to the ORDER BY clause which breaks the tie.

Use aggregate funcion MAX() like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM