-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL-Answer-3.txt
170 lines (149 loc) · 5.59 KB
/
SQL-Answer-3.txt
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
Question 3:
-----------
Q3.
Using the database WideWorldImporters, write a T-SQL stored procedure called ReportCustomerTurnover.
This procedure takes two parameters: Choice and Year, both integers.
When Choice = 1 and Year = <aYear>, ReportCustomerTurnover selects all the customer names and their total monthly turnover (invoiced value) for the year <aYear>.
When Choice = 2 and Year = <aYear>, ReportCustomerTurnover selects all the customer names and their total quarterly (3 months) turnover (invoiced value) for the year <aYear>.
When Choice = 3, the value of Year is ignored and ReportCustomerTurnover selects all the customer names and their total yearly turnover (invoiced value).
When no value is provided for the parameter Choice, the default value of Choice must be 1.
When no value is provided for the parameter Year, the default value is 2013. This doesn't impact Choice = 3.
For Choice = 3, the years can be hard-coded within the range of [2013-2016].
NULL values in the resultsets are not acceptable and must be substituted to 0.
All output resultsets are ordered by customer names alphabetically.
Example datasets are provided for the following calls:
EXEC dbo.ReportCustomerTurnover;
EXEC dbo.ReportCustomerTurnover 1, 2014;
EXEC dbo.ReportCustomerTurnover 2, 2015;
EXEC dbo.ReportCustomerTurnover 3;
SQL Query:
----------
USE [WideWorldImporters]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID ( 'dbo.ReportCustomerTurnover', 'P' ) IS NOT NULL
DROP PROCEDURE dbo.ReportCustomerTurnover;
GO
-- =============================================
-- Author: Maher SEBAI
-- Create date: 26-Dec-2018
-- Description: Report Customer Turnover
-- =============================================
CREATE PROCEDURE ReportCustomerTurnover
-- Add the parameters for the stored procedure here
@Choice int = 1,
@Year int = 2013
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
--SELECT @Choice, @Year
-- For Choice = 1, we report the Monthly Turnover for the selected year
-- if no second parameter for year, then default year = 2013
IF @Choice = 1
BEGIN
-- Execute Monthly query: Pivoting Monthly InvoiceTotal
select CustomerName,
coalesce([1], 0) as Jan,
coalesce([2], 0) as Feb,
coalesce([3], 0) as Mar,
coalesce([4], 0) as Apr,
coalesce([5], 0) as May,
coalesce([6], 0) as Jun,
coalesce([7], 0) as Jul,
coalesce([8], 0) as Aug,
coalesce([9], 0) as Sep,
coalesce([10],0) as Oct,
coalesce([11],0) as Nov,
coalesce([12],0) as [Dec]
from (
select Cu.CustomerName,
MONTH(It.InvoiceDate) as InvoiceMonth,
sum(It.InvoiceTotal) as InvoiceTotal
from Sales.Customers as Cu
left join (
select I.CustomerID, I.InvoiceID, I.InvoiceDate, sum(Il.Quantity * Il.UnitPrice) as InvoiceTotal
from Sales.Invoices as I
join Sales.InvoiceLines as Il on I.InvoiceID = Il.InvoiceID
where YEAR(I.InvoiceDate) = @Year
group by I.CustomerID, I.InvoiceID, I.InvoiceDate
) as It on Cu.CustomerID = It.CustomerID
group by Cu.CustomerName, MONTH(It.InvoiceDate)
) as SourceTable
Pivot (
max(InvoiceTotal) for InvoiceMonth in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) as PivotTable
order by CustomerName
END --IF @Choice = 1
ELSE
BEGIN
-- For Choice = 1, we report the Monthly Turnover for the selected year
-- if no second parameter for year, then default year = 2013
IF @Choice = 2
BEGIN
-- Execute Quaterly query: Pivoting Quarterly InvoiceTotal
select CustomerName,
coalesce([1],0) as Q1,
coalesce([2],0) as Q2,
coalesce([3],0) as Q3,
coalesce([4],0) as Q4
from (
select Cu.CustomerName,
datepart(quarter, It.InvoiceDate) as InvoiceQuarter,
sum(It.InvoiceTotal) as InvoiceTotal
from Sales.Customers as Cu
left join (
select I.CustomerID, I.InvoiceID, I.InvoiceDate, sum(Il.Quantity * Il.UnitPrice) as InvoiceTotal
from Sales.Invoices as I
join Sales.InvoiceLines as Il on I.InvoiceID = Il.InvoiceID
where YEAR(I.InvoiceDate) = 2015
group by I.CustomerID, I.InvoiceID, I.InvoiceDate
) as It on Cu.CustomerID = It.CustomerID
group by Cu.CustomerName, datepart(quarter, It.InvoiceDate)
) as SourceTable
Pivot (
max(InvoiceTotal) for InvoiceQuarter in ([1],[2],[3],[4])
) as PivotTable
order by CustomerName
END --IF @Choice = 2
ELSE
BEGIN
IF @Choice = 3
BEGIN
-- Execute Yearly Turnover By pivoting InvoiceTotal from 2013 to 2016
-- second parameter not taken into account
select CustomerName,
coalesce([2013],0) as [2013],
coalesce([2014],0) as [2014],
coalesce([2015],0) as [2015],
coalesce([2016],0) as [2016]
from (
select Cu.CustomerName as CustomerName,
sum(Il.Quantity * Il.UnitPrice) as InvoiceTotal,
YEAR(I.InvoiceDate) as InvoiceYear
from Sales.Customers as Cu,
Sales.Invoices as I,
Sales.InvoiceLines as Il
where Cu.CustomerID = I.CustomerID
and I.InvoiceID = Il.InvoiceID
group by Cu.CustomerName, YEAR(I.InvoiceDate)
) as SourceTable
Pivot (
max(InvoiceTotal) for InvoiceYear in ([2013],[2014],[2015],[2016])
) as PivotTable
order by CustomerName
END -- IF @Choice = 3
ELSE
BEGIN
PRINT 'Invalid Procedure Input Paremeter: Choice in [1..3], Year in [2013..2016]'
END -- ELSE @Choice = 3
END -- ELSE @Choice = 2
END -- ELSE @Choice = 1
END
GO
EXEC dbo.ReportCustomerTurnover 1, 2015;