Two classic SQL statements related to dates

Source: Internet
Author: User
Tags date sql null null
Statement
1. What is the maximum number of days of the month in which a date is obtained with a statement?

SELECT Day (DATEADD (DD,-day (' 2004-02-13 '), DATEADD (mm, 1, ' 2004-02-13 '))-as ' Day number '




2. Fewer records become multiple records problems

There are table TBL
Date Income expenditure
2004-02-11 00:00:00 60 45
2004-03-01 00:00:00 60 45
2004-03-02 00:00:00 40 50
2004-03-05 00:00:00 50 40

/*
Test data:
Create Table TBL ([date] smalldatetime,[revenue] int, [expense] int)

Insert into TBL
SELECT ' 2004-02-11 ', 60, 45
Union SELECT ' 2004-03-01 ', 60, 45
Union SELECT ' 2004-03-02 ', 40, 50
Union SELECT ' 2004-03-05 ', 50, 40
*/

The results to be obtained:
Date income expense balance
2004-02-01 00:00:00 null NULL NULL
2004-02-02 00:00:00 null NULL NULL
2004-02-03 00:00:00 null NULL NULL
2004-02-04 00:00:00 null NULL NULL
2004-02-05 00:00:00 null NULL NULL
2004-02-06 00:00:00 null NULL NULL
2004-02-07 00:00:00 null NULL NULL
2004-02-08 00:00:00 null NULL NULL
2004-02-09 00:00:00 null NULL NULL
2004-02-10 00:00:00 null NULL NULL
2004-02-11 00:00:00 60 45 15
2004-02-12 00:00:00 NULL NULL 15
2004-02-13 00:00:00 NULL NULL 15
2004-02-14 00:00:00 NULL NULL 15
2004-02-15 00:00:00 NULL NULL 15
2004-02-16 00:00:00 NULL NULL 15
2004-02-17 00:00:00 NULL NULL 15
2004-02-18 00:00:00 NULL NULL 15
2004-02-19 00:00:00 NULL NULL 15
2004-02-20 00:00:00 NULL NULL 15
2004-02-21 00:00:00 NULL NULL 15
2004-02-22 00:00:00 NULL NULL 15
2004-02-23 00:00:00 NULL NULL 15
2004-02-24 00:00:00 NULL NULL 15
2004-02-25 00:00:00 NULL NULL 15
2004-02-26 00:00:00 NULL NULL 15
2004-02-27 00:00:00 NULL NULL 15
2004-02-28 00:00:00 NULL NULL 15
2004-02-29 00:00:00 NULL NULL 15
2004-03-01 00:00:00 60 45 30
2004-03-02 00:00:00 40 50 20
2004-03-03 00:00:00 NULL NULL 20
2004-03-04 00:00:00 NULL NULL 20
2004-03-05 00:00:00 50 40 30
2004-03-06 00:00:00 NULL NULL 30
2004-03-07 00:00:00 NULL NULL 30
2004-03-08 00:00:00 NULL NULL 30
2004-03-09 00:00:00 NULL NULL 30
2004-03-10 00:00:00 NULL NULL 30
2004-03-11 00:00:00 NULL NULL 30
2004-03-12 00:00:00 NULL NULL 30
2004-03-13 00:00:00 NULL NULL 30
2004-03-14 00:00:00 NULL NULL 30
2004-03-15 00:00:00 NULL NULL 30
2004-03-16 00:00:00 NULL NULL 30
2004-03-17 00:00:00 NULL NULL 30
2004-03-18 00:00:00 NULL NULL 30
2004-03-19 00:00:00 NULL NULL 30
2004-03-20 00:00:00 NULL NULL 30
2004-03-21 00:00:00 NULL NULL 30
2004-03-22 00:00:00 NULL NULL 30
2004-03-23 00:00:00 NULL NULL 30
2004-03-24 00:00:00 NULL NULL 30
2004-03-25 00:00:00 NULL NULL 30
2004-03-26 00:00:00 NULL NULL 30
2004-03-27 00:00:00 NULL NULL 30
2004-03-28 00:00:00 NULL NULL 30
2004-03-29 00:00:00 NULL NULL 30
2004-03-30 00:00:00 NULL NULL 30
2004-03-31 00:00:00 NULL NULL 30

Answer:
SELECT y.[Date], tbl. [Income], tbl. [Expenditure], (
SELECT SUM (ISNULL (tbl.[ Revenue], 0)-isnull (tbl.[ Expenses], 0)) from TBL WHERE [date]<=y.[Date]) as [balance]
From TBL Right JOIN (
SELECT DATEADD (DD, N.I, DATEADD (DD, 1-day (m.minday), m.minday)) as [date]
From (
SELECT 0 As I
UNION all SELECT 1
UNION All SELECT 2
UNION All SELECT 3
UNION All SELECT 4
UNION all SELECT 5
UNION All SELECT 6
UNION All SELECT 7
UNION All SELECT 8
UNION All SELECT 9
UNION All SELECT 10
UNION All SELECT 11
UNION All SELECT 12
UNION All SELECT 13
UNION All SELECT 14
UNION All SELECT 15
UNION All SELECT 16
UNION all SELECT 17
UNION All SELECT 18
UNION all SELECT 19
UNION All SELECT 20
UNION All SELECT 21
UNION all SELECT 22
UNION all SELECT 23
UNION All SELECT 24
UNION all SELECT 25
UNION all SELECT 26
UNION all SELECT 27
UNION all SELECT 28
UNION all SELECT 29
UNION All SELECT 30
UNION all SELECT 31
N
(
SELECT MIN (date) as Minday
From TBL
GROUP by DATEDIFF (month, 0, date)
) M
WHERE DATEDIFF (MM, DATEADD (DD, N.I, DATEADD (DD, 1-day (m.minday), M.minday)), M.minday) =0) as Y
On TBL. [Date]=y. Date


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.