Difference between the number of the latest date and the earliest date selected by date

Source: Internet
Author: User
If you want to get the difference between the earliest time of the day and the number of the latest time, you can refer.

If you want to get the difference between the earliest time of the day and the number of the latest time, you can refer.

The table structure is as follows:

Number date
8 2009/1/11
7 2009/1/11
6 2009/1/11 12: 00
5 2009/1/11
4 2009/1/12 4:00
3 2009/1/12 10: 00
2 2009/1/12 12:00
1 2009/1/11

The result is as follows:

Difference
2
3
The Code is as follows:
Create table # date
(
Number int identity (1, 1) primary key,
Date datetime
)
Insert into # date select '2014/1/11'
Insert into # date select '2014/1/12'
Insert into # date select '2014/1/12'
Insert into # date select '2014/1/12'
Insert into # date select '2014/1/11'
Insert into # date select '2014/1/11'
Insert into # date select '2014/1/11'
Insert into # date select '2014/1/11'

Select (d2.number-d1.number) number
From
(
Select number, date from # date where date in
(Select max (date) from # date group by convert (varchar (10), date, 120 ))
) D1
,
(
Select number, date from # date where date in
(Select min (date) from # date group by convert (varchar (10), date, 120 ))
) D2
Where converter (varchar (10), d1.date, 120) = convert (varchar (10), d2.date, 120)

Number
-----------
2
3

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.