Select the difference for the number of the latest date and the earliest date by date _mssql

Source: Internet
Author: User
Table structure is as follows

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

The difference between the number of the earliest and the latest of the day is the result of the following:

Poor
2
3
Copy Code code as follows:

CREATE TABLE #date
(
Number int identity (1,1) primary key,
Date datetime
)
Insert INTO #date select ' 2009/1/11 17:00 '
Insert INTO #date select ' 2009/1/12 12:00 '
Insert INTO #date select ' 2009/1/12 10:00 '
Insert INTO #date select ' 2009/1/12 4:00 '
Insert INTO #date select ' 2009/1/11 18:00 '
Insert INTO #date select ' 2009/1/11 12:00 '
Insert INTO #date select ' 2009/1/11 5:00 '
Insert INTO #date select ' 2009/1/11 2:00 '

Select (d2.number-d1.number) number
From
(
Select Number,date from #date where date in
(select Max (date) from #date GROUP by CONVERT (varchar), date,120)
) D1
,
(
Select Number,date from #date where date in
(select min (date) from #date GROUP by CONVERT (varchar), date,120)
) D2
where CONVERT (varchar (), d1.date,120) =convert (varchar, 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.