Classic SQL classification summary

zhaozj2021-02-16  97

The surface structure is as follows

Value Date

100 2000/2/5

123 2000/5/6

3213 2003/5/8

Requires SQL to achieve total quarter and annual total annual quarters in accordance with annual statistics

Year 1 quarter value 2 quarter value 3 quarter value 4 quarter value annual value

2000 100 123 NULL NULL 223

2003 NULL 3213 NULL NULL 3213

answer

SELECT YEAR ([DATE]) AS Year, SUM (Case When (Month ([Date]) 2)

/ 3 = 1 THEN [Value] Else 0 End) AS [1 quarter], SUM (Case When)

(Month ([DATE]) 2)

/ 3 = 2 THEN [Value] Else 0 End) AS [2 quarter], SUM (Case When

(Month ([DATE]) 2)

/ 3 = 3 THEN [Value] Else 0 End) AS [3 Quarter], SUM (Case When

(Month ([DATE]) 2)

/ 3 = 4 THEN [VALUE] ELSE 0 END) AS [4 quarter value], sum ([value]) AS annual value

From test

Group by year ([date])

转载请注明原文地址:https://www.9cbs.com/read-13179.html

New Post(0)