This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQ3.sql
179 lines (179 loc) · 9.24 KB
/
Q3.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
CREATE OR ALTER PROCEDURE [dbo].[ReportCustomerTurnover](@Choice INT = 1, @Year INT = 2013)
AS
BEGIN
SET nocount ON;
IF (@Choice = 1)
BEGIN
SELECT C.customername,
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 1
AND Year(I.invoicedate) = @Year ) AS 'Jan',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 2
AND Year(I.invoicedate) = @Year ) AS 'Feb',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 3
AND Year(I.invoicedate) = @Year ) AS 'Mar',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 4
AND Year(I.invoicedate) = @Year ) AS 'Apr',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 5
AND Year(I.invoicedate) = @Year ) AS 'May',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 6
AND Year(I.invoicedate) = @Year ) AS 'Jun',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 7
AND Year(I.invoicedate) = @Year ) AS 'Jul',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 8
AND Year(I.invoicedate) = @Year ) AS 'Aug',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 9
AND Year(I.invoicedate) = @Year ) AS 'Sep',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 10
AND Year(I.invoicedate) = @Year ) AS 'Oct',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate)= 11
AND Year(I.invoicedate) = @Year ) AS 'Nov',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Month(I.invoicedate) = 12
AND Year(I.invoicedate) = @Year ) AS 'Dec'
FROM sales.customers AS C
ORDER BY C.customername
END
IF (@Choice = 2)
BEGIN
SELECT C.customername,
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Year(I.invoicedate) = @Year
AND Datepart(quarter, I.invoicedate) = 1 ) AS 'Q1',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Year(I.invoicedate) = @Year
AND Datepart(quarter, I.invoicedate) = 2 ) AS 'Q2',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Year(I.invoicedate) = @Year
AND Datepart(quarter, I.invoicedate) = 3 ) AS 'Q3',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND C.customerid = I.customerid
AND Year(I.invoicedate) = @Year
AND Datepart(quarter, I.invoicedate) = 4 ) AS 'Q4'
FROM sales.customers AS C
ORDER BY C.customername
END
IF (@Choice = 3)
BEGIN
SELECT customername,
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND Cu.customerid = I.customerid
AND Year(I.invoicedate) = 2013 ) AS '2013',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND Cu.customerid = I.customerid
AND Year(I.invoicedate) = 2014 ) AS '2014',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND Cu.customerid = I.customerid
AND Year(I.invoicedate) = 2015 ) AS '2015',
(
SELECT COALESCE(Sum(IL.unitprice*IL.quantity), 0)
FROM sales.invoicelines AS IL,
sales.invoices AS I
WHERE IL.invoiceid = I.invoiceid
AND Cu.customerid = I.customerid
AND Year(I.invoicedate) = 2016 ) AS '2016'
FROM sales.customers AS Cu
ORDER BY customername
END
END