-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLab-04.sql
165 lines (141 loc) · 5.46 KB
/
Lab-04.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
--question 1
select * from Sales.SalesTerritory
select * from Sales.SalesOrderHeader
WITH newTabel1(TerritoryName, TerritoryRegion) as
(select Name, [Group]
from Sales.SalesTerritory)
select case GROUPING(Sales.SalesTerritory.Name)
when 0 then Sales.SalesTerritory.Name
when 1 then 'ALL Territories'
END AS TerritoryName,
case GROUPING(Sales.SalesTerritory.[Group])
when 0 then Sales.SalesTerritory.[Group]
when 1 then case GROUPING(Sales.SalesTerritory.Name)
when 0 then (select TerritoryRegion from newTabel1 where Sales.SalesTerritory.Name = newTabel1.TerritoryName)
when 1 then 'All Regions'
end
END AS Region,
SUM(Sales.SalesOrderHeader.SubTotal) as SalesTotal,
COUNT(Sales.SalesOrderHeader.SalesOrderID) as SalesCount
from Sales.SalesTerritory INNER JOIN Sales.SalesOrderHeader
ON (Sales.SalesTerritory.TerritoryID = Sales.SalesOrderHeader.TerritoryID)
GROUP BY GROUPING SETS(
(),
(Sales.SalesTerritory.Name),
(Sales.SalesTerritory.[Group])
)
order by Sales.SalesTerritory.Name, Sales.SalesTerritory.[Group]
--question 1 (version 2)
WITH
TerritorySales(TerritoryID, SalesTotal, SalesCount)
as(
SELECT TerritoryID , SUM(SubTotal), COUNT(TerritoryID)
FROM Sales.SalesOrderHeader as SOH
GROUP BY TerritoryID
),
TerritoryTotalCount(TerritoryName, Region, SalesTotal, SalesCount)
as(
SELECT ST.Name AS TerritoryName , ST.[Group] as Region, TS.SalesTotal, TS.SalesCount
from TerritorySales as TS INNER JOIN Sales.SalesTerritory as ST
ON TS.TerritoryID = ST.TerritoryID
),
RegionTotalCount(TerritoryName, Region, SalesTotal, SalesCount)
as(
select 'All Territories', Region, SUM(SalesTotal), SUM(SalesCount)
FROM TerritoryTotalCount
GROUP BY Region
),
AllRegionTotalCount(TerritoryName, Region, SalesTotal, SalesCount)
as(
SELECT 'All Territories', 'All Regions', SUM(SalesTotal), SUM(SalesCount)
FROM RegionTotalCount
)
SELECT *
FROM TerritoryTotalCount as TTC
UNION
SELECT *
FROM RegionTotalCount
UNION
SELECT *
FROM AllRegionTotalCount;
--question 2
select * from Sales.SalesOrderDetail
select * from Production.Product
select * from Production.ProductSubcategory
select * from Production.ProductCategory
WITH newTable1 (CatName, ProductCategoryID) AS
(select Production.ProductCategory.Name, Production.ProductCategory.ProductCategoryID
from Production.ProductCategory),
newTable2 (SubCatName, ProductCategoryID) AS
(select Production.ProductSubcategory.Name, Production.ProductSubcategory.ProductCategoryID
from Production.ProductSubcategory),
newTable3 (CatName, SubCatName) AS
(select newTable1.CatName, newTable2.SubCatName
from newTable1 INNER JOIN newTable2
ON (newTable1.ProductCategoryID = newTable2.ProductCategoryID))
select case GROUPING(Production.ProductSubcategory.Name)
when 0 then Production.ProductSubcategory.Name
when 1 then 'ALL SubCat'
END as SubCat,
case GROUPING(Production.ProductCategory.Name)
when 0 then Production.ProductCategory.Name
when 1 then case GROUPING(Production.ProductSubcategory.Name)
when 0 then (select newTable3.CatName from newTable3 where newTable3.SubCatName = Production.ProductSubcategory.Name)
when 1 then 'ALL Cat'
end
END as Cat,
COUNT(Sales.SalesOrderDetail.OrderQty) as SalesCount,
SUM(Sales.SalesOrderDetail.LineTotal) as SalesTotal
from Sales.SalesOrderDetail INNER JOIN Production.Product
ON (Sales.SalesOrderDetail.ProductID = Production.Product.ProductID)
INNER JOIN Production.ProductSubcategory
ON (Production.ProductSubcategory.ProductSubcategoryID = Production.Product.ProductSubcategoryID)
INNER JOIN Production.ProductCategory
ON (Production.ProductCategory.ProductCategoryID = Production.ProductSubcategory.ProductCategoryID)
GROUP BY GROUPING SETS(
(),
(Production.ProductSubcategory.Name),
(Production.ProductCategory.Name)
)
ORDER BY Cat, SubCat
--question 2 (version 2)
WITH
productSubCatID(SubCatID, SalesTotal, SalesCount)
as(
SELECT p.ProductSubcategoryID, sum(SOD.LineTotal) , sum(SOD.OrderQty)
FROM Sales.SalesOrderDetail AS SOD INNER JOIN Production.Product as P
on SOD.ProductID = P.ProductID
GROUP BY P.ProductSubcategoryID
),
productSubCatName(cat, subCat, SalesCount, SalesTotal)
as(
SELECT PSC.ProductCategoryID, PSC.Name, PSCID.SalesCount, PSCID.SalesTotal
FROM productSubCatID as PSCID INNER JOIN Production.ProductSubcategory as PSC
ON PSC.ProductSubcategoryID = PSCID.SubCatID
),
table1(subCat, cat, SalesCount, SalesTotal)
AS(
SELECT PSCN.subCat, PC.Name, PSCN.SalesCount, PSCN.SalesTotal
FROM productSubCatName as PSCN INNER JOIN Production.ProductCategory as PC
ON PSCN.cat = PC.ProductCategoryID
),
catSum(subCat, cat, SalesCount, SalesTotal)
as(
SELECT 'All sub', cat, SUM(SalesCount), SUM(SalesTotal)
FROM table1
GROUP BY cat
),
allCatSum(subCat, cat, SalesCount, SalesTotal)
AS(
SELECT 'All sub', 'All cat', SUM(SalesCount), SUM(SalesTotal)
FROM catSum
)
SELECT *
FROM table1
UNION
select *
FROM catSum
UNION
select *
FROM allCatSum
ORDER BY cat, subCat