A simple statement that solves the classic SQL statement "Every Y's newest X" _mssql

Source: Internet
Author: User
Copy Code code as follows:

/****** CREATE 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] (m) COLLATE chinese_prc_ci_as not NULL,
[X] [smalldatetime] not NULL
) on [PRIMARY]
Go
--Inserting data
Insert into [Table] (Y, X) VALUES (' CCC ', ' 2007-02-02 02:02:02 ')
Insert into [Table] (Y, X) VALUES (' AAA ', ' 2007-02-02 02:02:02 ')
Insert into [Table] (Y, X) VALUES (' AAA ', ' 2007-03-03 03:03:03 ')
Insert into [Table] (Y, X) VALUES (' BBB ', ' 2007-01-01 01:01:01 ')
Insert into [Table] (Y, X) VALUES (' BBB ', ' 2007-02-02 02:02:02 ')
Insert into [Table] (Y, X) VALUES (' BBB ', ' 2007-03-03 03:03:03 ')
Insert into [Table] (Y, X) VALUES (' CCC ', ' 2007-01-01 01:01:01 ')
Insert into [Table] (Y, X) VALUES (' AAA ', ' 2007-01-01 01:01:01 ')
Insert into [Table] (Y, X) VALUES (' CCC ', ' 2007-03-03 03:03:03 ')
Insert into [Table] (Y, X) VALUES (' DDD ', ' 2007-01-01 01:01:01 ')
Insert into [Table] (Y, X) VALUES (' DDD ', ' 2007-02-02 02:02:02 ')
Insert into [Table] (Y, X) VALUES (' DDD ', ' 2007-03-03 03:03:03 ')
Insert into [Table] (Y, X) VALUES (' EEE ', ' 2007-01-01 01:01:01 ')
Insert into [Table] (Y, X) VALUES (' EEE ', ' 2007-02-02 02:02:02 ')
Insert into [Table] (Y, X) VALUES (' EEE ', ' 2007-03-03 03:03:03 ')
Go

/****** CREATE 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] (m) COLLATE chinese_prc_ci_as not NULL,
[X] [smalldatetime] not NULL
) on [PRIMARY]
Go
--Inserting data
Insert into [Table] (Y, X) VALUES (' CCC ', ' 2007-02-02 02:02:02 ')
Insert into [Table] (Y, X) VALUES (' AAA ', ' 2007-02-02 02:02:02 ')
Insert into [Table] (Y, X) VALUES (' AAA ', ' 2007-03-03 03:03:03 ')
Insert into [Table] (Y, X) VALUES (' BBB ', ' 2007-01-01 01:01:01 ')
Insert into [Table] (Y, X) VALUES (' BBB ', ' 2007-02-02 02:02:02 ')
Insert into [Table] (Y, X) VALUES (' BBB ', ' 2007-03-03 03:03:03 ')
Insert into [Table] (Y, X) VALUES (' CCC ', ' 2007-01-01 01:01:01 ')
Insert into [Table] (Y, X) VALUES (' AAA ', ' 2007-01-01 01:01:01 ')
Insert into [Table] (Y, X) VALUES (' CCC ', ' 2007-03-03 03:03:03 ')
Insert into [Table] (Y, X) VALUES (' DDD ', ' 2007-01-01 01:01:01 ')
Insert into [Table] (Y, X) VALUES (' DDD ', ' 2007-02-02 02:02:02 ')
Insert into [Table] (Y, X) VALUES (' DDD ', ' 2007-03-03 03:03:03 ')
Insert into [Table] (Y, X) VALUES (' EEE ', ' 2007-01-01 01:01:01 ')
Insert into [Table] (Y, X) VALUES (' EEE ', ' 2007-02-02 02:02:02 ')
Insert into [Table] (Y, X) VALUES (' EEE ', ' 2007-03-03 03:03:03 ')

Go resolves "the latest x for every Y" classic SQL question: Here are a few ways to eight Immortals crossing
Copy Code code 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 speed of indexing in different fields is not the same, users differ.
The first method is good in every way, and in the Y column is indexed, you can greatly optimize the speed of the query.

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.