A statement simply solves the classic SQL statement of "The latest X for every Y"

Source: Internet
Author: User

Copy codeThe Code is as follows:
/****** Create a table ******/
If exists (select * from dbo. sysobjects where id = object_id (n' [dbo]. [Table] ') and OBJECTPROPERTY (id, n'isusertable') = 1)
Drop table [dbo]. [Table]
GO
Create TABLE [dbo]. [Table] (
[ID] [int] IDENTITY (1, 1) not null,
[Y] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS not null,
[X] [smalldatetime] NOT NULL
) ON [PRIMARY]
GO
-- Insert data
Insert INTO [Table] (Y, X) values ('ccc ', '2017-02-02 02:02:02 ')
Insert INTO [Table] (Y, X) values ('aaa', '2017-02-02 02:02:02 ')
Insert INTO [Table] (Y, X) values ('aaa', '2017-03-03 03:03:03 ')
Insert INTO [Table] (Y, X) values ('bbb ', '2017-01-01 01:01:01 ')
Insert INTO [Table] (Y, X) values ('bbb ', '2017-02-02 02:02:02 ')
Insert INTO [Table] (Y, X) values ('bbb ', '2017-03-03 03:03:03 ')
Insert INTO [Table] (Y, X) values ('ccc ', '2017-01-01 01:01:01 ')
Insert INTO [Table] (Y, X) values ('aaa', '2017-01-01 01:01:01 ')
Insert INTO [Table] (Y, X) values ('ccc ', '2017-03-03 03:03:03 ')
Insert INTO [Table] (Y, X) values ('ddd ', '2017-01-01 01:01:01 ')
Insert INTO [Table] (Y, X) values ('ddd ', '2017-02-02 02:02:02 ')
Insert INTO [Table] (Y, X) values ('ddd ', '2017-03-03 03:03:03 ')
Insert INTO [Table] (Y, X) values ('Eee ', '2017-01-01 01:01:01 ')
Insert INTO [Table] (Y, X) values ('Eee ', '2017-02-02 02:02:02 ')
Insert INTO [Table] (Y, X) values ('Eee ', '2017-03-03 03:03:03 ')
GO

/****** Create a table ******/
If exists (select * from dbo. sysobjects where id = object_id (n' [dbo]. [Table] ') and OBJECTPROPERTY (id, n'isusertable') = 1)
Drop table [dbo]. [Table]
GO
Create TABLE [dbo]. [Table] (
[ID] [int] IDENTITY (1, 1) not null,
[Y] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS not null,
[X] [smalldatetime] NOT NULL
) ON [PRIMARY]
GO
-- Insert data
Insert INTO [Table] (Y, X) values ('ccc ', '2017-02-02 02:02:02 ')
Insert INTO [Table] (Y, X) values ('aaa', '2017-02-02 02:02:02 ')
Insert INTO [Table] (Y, X) values ('aaa', '2017-03-03 03:03:03 ')
Insert INTO [Table] (Y, X) values ('bbb ', '2017-01-01 01:01:01 ')
Insert INTO [Table] (Y, X) values ('bbb ', '2017-02-02 02:02:02 ')
Insert INTO [Table] (Y, X) values ('bbb ', '2017-03-03 03:03:03 ')
Insert INTO [Table] (Y, X) values ('ccc ', '2017-01-01 01:01:01 ')
Insert INTO [Table] (Y, X) values ('aaa', '2017-01-01 01:01:01 ')
Insert INTO [Table] (Y, X) values ('ccc ', '2017-03-03 03:03:03 ')
Insert INTO [Table] (Y, X) values ('ddd ', '2017-01-01 01:01:01 ')
Insert INTO [Table] (Y, X) values ('ddd ', '2017-02-02 02:02:02 ')
Insert INTO [Table] (Y, X) values ('ddd ', '2017-03-03 03:03:03 ')
Insert INTO [Table] (Y, X) values ('Eee ', '2017-01-01 01:01:01 ')
Insert INTO [Table] (Y, X) values ('Eee ', '2017-02-02 02:02:02 ')
Insert INTO [Table] (Y, X) values ('Eee ', '2017-03-03 03:03:03 ')

GO solves the classic SQL problem of "The latest X of every Y": The following methods are really amazing
Copy codeThe Code is as follows:

Select ID, Y, X
FROM [Table] T1
Where (NOT EXISTS
(Select 1
FROM [Table] T2
Where (T2.Y = T1. Y) AND (T2.X> T1. X or
T2.X = T1. x and T2.ID> T1. ID )))
/*************************************** **************************************/
Select *
FROM [Table]
Where ID IN
(Select MAX (T1.ID)
FROM [Table] T1 JOIN
(Select y, MAX (x) x
FROM [Table]
Group by y) T2 ON T1.Y = T2.Y AND T1.x = T2.x
Group by T1.y)
/*************************************** **************************************/
Select T. ID, T. Y, T. X
FROM [Table] T INNER JOIN
(Select MAX (T1.ID) AS ID
FROM [Table] T1 JOIN
(Select y, MAX (x) x
FROM [Table]
Group by y) T2 ON T1.Y = T2.Y AND T1.x = T2.x
Group by T1.y) T2 on t. ID = T2.ID
/*************************************** **************************************/
Select *
FROM [Table] T1
Where ID IN
(Select TOP 1 ID
FROM [Table]
Where Y = T1.Y
OrDER by x desc)
/*************************************** **************************************/
Select *
FROM [Table] T1
Where (ID =
(Select TOP 1 ID
FROM [Table]
Where Y = T1.Y
OrDER by x desc, id desc ))
/*************************************** **************************************/

/*************************************** **************************************/
Select ID, Y, X
FROM [Table] T1
Where (NOT EXISTS
(Select 1
FROM [Table] T2
Where (T2.Y = T1. Y) AND (T2.X> T1. X or
T2.X = T1. x and T2.ID> T1. ID )))
/*************************************** **************************************/
Select *
FROM [Table]
Where ID IN
(Select MAX (T1.ID)
FROM [Table] T1 JOIN
(Select y, MAX (x) x
FROM [Table]
Group by y) T2 ON T1.Y = T2.Y AND T1.x = T2.x
Group by T1.y)
/*************************************** **************************************/
Select T. ID, T. Y, T. X
FROM [Table] T INNER JOIN
(Select MAX (T1.ID) AS ID
FROM [Table] T1 JOIN
(Select y, MAX (x) x
FROM [Table]
Group by y) T2 ON T1.Y = T2.Y AND T1.x = T2.x
Group by T1.y) T2 on t. ID = T2.ID
/*************************************** **************************************/
Select *
FROM [Table] T1
Where ID IN
(Select TOP 1 ID
FROM [Table]
Where Y = T1.Y
OrDER by x desc)
/*************************************** **************************************/
Select *
FROM [Table] T1
Where (ID =
(Select TOP 1 ID
FROM [Table]
Where Y = T1.Y
OrDER by x desc, id desc ))

/*************************************** **************************************
/Efficiency. The indexing speed varies with different fields.
The speed of the first method is good in all aspects, and the indexing of column Y can greatly optimize the query speed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.