-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL PIVOT_Cube_Rollup_GROUPING SETs.sql
More file actions
189 lines (161 loc) · 5.4 KB
/
SQL PIVOT_Cube_Rollup_GROUPING SETs.sql
File metadata and controls
189 lines (161 loc) · 5.4 KB
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
180
181
182
183
184
185
186
187
188
189
--Pivot
WITH Fuente (Producto,Año,Total) AS
(
SELECT p.Name AS Producto,
YEAR(soh.OrderDate) AS Año,
Sum(sod.LineTotal) AS Total
FROM Sales.SalesOrderHeader soh
JOIN Sales.SalesOrderDetail sod ON soh.SalesOrderID = sod.SalesOrderID
JOIN Production.Product p ON p.ProductID = sod.ProductID
group by p.Name, YEAR(soh.OrderDate)
)
SELECT *
FROM Fuente
PIVOT (
SUM(Total) FOR Año IN ([2011], [2012], [2013], [2014])
) AS pvt
--La presencia de null ocurre porque en esos años no se realizaron ventas en esos productos
-- Unpivot
WITH Fuente (Producto,Año,Total) AS
(
SELECT p.Name AS Producto,
YEAR(soh.OrderDate) AS Año,
Sum(sod.LineTotal) AS Total
FROM Sales.SalesOrderHeader soh
JOIN Sales.SalesOrderDetail sod ON soh.SalesOrderID = sod.SalesOrderID
JOIN Production.Product p ON p.ProductID = sod.ProductID
group by p.Name, YEAR(soh.OrderDate)
)
SELECT Producto, Año, Total
FROM (
SELECT Producto, [2011], [2012], [2013], [2014]
FROM Fuente
PIVOT (
SUM(Total) FOR Año IN ([2011], [2012], [2013], [2014])
) AS pvt
) AS datos_pivot
UNPIVOT (
Total FOR Año IN ([2011], [2012], [2013], [2014])
) AS unpvt;
-- Con dos CTE es mas prolijo,
WITH Fuente AS (
SELECT
p.Name AS Producto,
YEAR(soh.OrderDate) AS Año,
SUM(sod.LineTotal) AS Total
FROM Sales.SalesOrderHeader soh
JOIN Sales.SalesOrderDetail sod ON soh.SalesOrderID = sod.SalesOrderID
JOIN Production.Product p ON p.ProductID = sod.ProductID
GROUP BY p.Name, YEAR(soh.OrderDate)
),
PivotFuente AS (
SELECT Producto, [2011], [2012], [2013], [2014]
FROM Fuente
PIVOT (
SUM(Total) FOR Año IN ([2011], [2012], [2013], [2014])
) AS pvt
)
SELECT Producto, Año, Total
FROM PivotFuente
UNPIVOT (
Total FOR Año IN ([2011], [2012], [2013], [2014])
) AS unpvt;
-- Ejercicio: Usando AdventureWorks, crear una consulta que muestre cuántas órdenes realizó cada
-- empleado por año (Sales.SalesOrderHeader y HumanResources.Employee). --> CTE
-- Usar PIVOT para mostrar cada año como columna y la cantidad de órdenes por empleado.
WITH ventas_empleado (Empleado,Año,Cantidad_ordenes) AS
(
SELECT
e.BusinessEntityID AS Empleado,
YEAR(soh.OrderDate) AS Año,
COUNT(soh.SalesOrderID) AS Cantidad_ordenes
FROM Sales.SalesOrderHeader soh
JOIN HumanResources.Employee e ON soh.SalesPersonID = e.BusinessEntityID
group by e.BusinessEntityID, YEAR(soh.OrderDate)
)
SELECT *
FROM ventas_empleado
PIVOT (
SUM(Cantidad_ordenes) FOR Año IN ([2011], [2012], [2013], [2014])
) AS pvt
-- CUBE : La cláusula CUBE en SQL Server es una extensión de la cláusula GROUP BY que genera
-- subtotales para todas las combinaciones posibles de columnas especificadas.
-- ej: uso AdventureWorksDW20219
SELECT
fk.name AS ForeignKey,
tp.name AS ParentTable,
ref.name AS ReferencedTable,
cpa.name AS ParentColumn,
cref.name AS ReferencedColumn
FROM sys.foreign_keys fk
INNER JOIN sys.tables tp ON fk.parent_object_id = tp.object_id
INNER JOIN sys.tables ref ON fk.referenced_object_id = ref.object_id
INNER JOIN sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
INNER JOIN sys.columns cpa ON fkc.parent_object_id = cpa.object_id AND fkc.parent_column_id = cpa.column_id
INNER JOIN sys.columns cref ON fkc.referenced_object_id = cref.object_id AND fkc.referenced_column_id = cref.column_id
WHERE tp.name LIKE 'Dim%' OR tp.name LIKE 'Fact%'
ORDER BY ParentTable, ReferencedTable;
SELECT
CalendarYear,
SalesTerritoryGroup,
SUM(SalesAmount) AS TotalVentas
FROM FactResellerSales AS frs
JOIN DimDate AS d ON frs.OrderDateKey = d.DateKey
JOIN DimSalesTerritory AS st ON frs.SalesTerritoryKey = st.SalesTerritoryKey
GROUP BY
CUBE(CalendarYear, SalesTerritoryGroup)
ORDER BY
CalendarYear, SalesTerritoryGroup;
-- ROLLUP: La cláusula ROLLUP en SQL Server se utiliza junto con la cláusula
-- GROUP BY para generar subtotales y totales adicionales para una o
-- más columnas especificadas. Proporciona un resumen jerárquico de
-- los datos al incluir subtotales para combinaciones de columnas
-- específicas.
SELECT
YEAR(fis.OrderDate) AS Año,
dp.EnglishProductName AS Producto,
SUM(fis.SalesAmount) AS TotalVentas
FROM FactInternetSales fis
JOIN DimProduct dp ON fis.ProductKey = dp.ProductKey
GROUP BY
ROLLUP (YEAR(fis.OrderDate), dp.EnglishProductName)
ORDER BY
Año, Producto;
-- GRUPING SETS: permite definir combinaciones específicas de agrupamiento dentro de
-- una sola consulta GROUP BY
SELECT
Color,
Size
FROM Production.Product
WHERE Color IS NOT NULL AND Size IS NOT NULL
GROUP BY
GROUPING SETS (
(Color, Size),
(Color),
(Size),
()
)
ORDER BY Color, Size;
-- otra en DW
SELECT
YEAR(fis.OrderDate) AS Año,
dst.SalesTerritoryGroup AS Region,
SUM(fis.SalesAmount) AS TotalVentas
FROM FactInternetSales fis
JOIN DimSalesTerritory dst ON fis.SalesTerritoryKey = dst.SalesTerritoryKey
GROUP BY
GROUPING SETS (
(YEAR(fis.OrderDate), dst.SalesTerritoryGroup), -- año y región
(YEAR(fis.OrderDate)), -- solo año
(dst.SalesTerritoryGroup), -- solo región
() -- total general
)
ORDER BY Año, Region;
--ej:
SELECT
ISNULL(Color,
CASE WHEN GROUPING(Color) = 1 THEN 'TODOS' ELSE 'DESC' END) AS Color,
COUNT(*) AS Cantidad
FROM Production.Product
GROUP BY ROLLUP(Color);
-- Si el color esta ausente muestra DESC