Repository navigation
Expand file tree
/
Copy path22_SQL_Value-window-functions.sql
More file actions
159 lines (116 loc) · 3.66 KB
/
Copy path22_SQL_Value-window-functions.sql
File metadata and controls
159 lines (116 loc) · 3.66 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
---------SQL Value Window Functions----------
-------LEAD() & LAG()-------
/*
LEAD() - Dostaje dostep do wartosci z nastepnego wiersza w obrebie okna.
LAG() - Dostaje dostep do wartosci z poprzedniego wiersza w obrebie okna.
SYNTAX:
LEAD(expr,offset,default)
LAG(expr,offset,default)
LEAD(Sales,2,10) OVER(PARTITION BY ProductID ORDER BY OrderDate)
- Expr jest wygagane, moze to byc kazdy typ danych
- 2 to nasz offset, mowi on ile ma pojsc do przodu/tylu i zwrocic wartosc (optional)
default: 1
-10 to nasza default value jesli nie mozna dostac dostepu do nastepnych/poprzednich wierszy (optional)
default: NULL
- ORDER BY jest wymagany!
*/
---USE CASE: Month-Over-Month ANALYSIS---
/*task: Analyze the month-over-month (MoM) peformance by finding the percentage change
in sales between the current and previous month */
/*
Year-over-Year (YoY) - analiza ogolnego trendu wzrostu lub spadku wydajnosci biznesu na przestrzeni czasu.
Month-over-Month (Mom) - analiza krotkotrwalych trendow i odkrywanie wzorow w sezonowosci.
*/
SELECT
CONCAT(ISNULL(ROUND((CAST(SumOfSales AS FLOAT) - CAST(LAG(SumOfSales,1,0) OVER(ORDER BY miesiace)AS FLOAT))
/ CAST(LAG(SumOfSales) OVER(ORDER BY miesiace) AS FLOAT) * 100,2),0),'%') [(MoM) percentage change]
FROM(
SELECT
DATETRUNC(MONTH,OrderDate) [miesiace],
SUM(Sales) [SumOfSales]
FROM Sales.Orders
GROUP BY DATETRUNC(MONTH,OrderDate)
)t;
SELECT
DATENAME(MONTH,OrderDate) [miesiace],
SUM(Sales) [SumOfSales]
FROM Sales.Orders
GROUP BY DATENAME(MONTH,OrderDate);
--USE CASE: Customer Retention Analysis---
/*
Mierzy zachowanie i lojalnosc klientow by pomoc biznesowi budowac
silne relacje z klinentami.
*/
/*task: Analyze customer loyalty by ranking customers based on tyhe averaged number of
days between orders */
SELECT
CustomerID,
AVG(ileDni) [srednia],
RANK() OVER(ORDER BY
CASE
WHEN AVG(ileDni) IS NULL THEN 1 ELSE 0
END ASC,
AVG(ileDni))
FROM(
SELECT
CustomerID,
OrderDate,
LEAD(OrderDate) OVER(PARTITION BY CustomerID ORDER BY OrderDate ASC) [nastepneZamowienie],
DATEDIFF(day,OrderDate,LEAD(OrderDate) OVER(PARTITION BY CustomerID ORDER BY OrderDate ASC)) [ileDni]
FROM Sales.Orders
)t
GROUP BY CustomerID
-----------
SELECT
RANK() OVER(ORDER BY AVG(ileDni)) [ranking],
CustomerID,
AVG(ileDni) [srednia]
FROM(
SELECT
CustomerID,
OrderDate,
DATEDIFF(day,LAG(OrderDate) OVER(PARTITION BY CustomerID ORDER BY OrderDate),OrderDate) [ileDni]
FROM Sales.Orders
)t
GROUP BY CustomerID
HAVING AVG(ileDni) IS NOT NULL
---lub---
SELECT
ROW_NUMBER() OVER(
ORDER BY
CASE WHEN AVG(ileDni) IS NULL THEN 1 ELSE 0 END ASC,
AVG(ileDni) ASC
) [ranking],
CustomerID,
AVG(ileDni) [srednia]
FROM (
SELECT
CustomerID,
OrderDate,
DATEDIFF(day, LAG(OrderDate) OVER(PARTITION BY CustomerID ORDER BY OrderDate), OrderDate) [ileDni]
FROM Sales.Orders
) t
GROUP BY CustomerID
ORDER BY
CASE WHEN AVG(ileDni) IS NULL THEN 1 ELSE 0 END ASC,
AVG(ileDni) ASC;
------FIRST_VALUE() & LAST_VALUE()------
/*
FIRST_VALUE() - ma dostep do wartosci z pierwszego wiersza w oknie
LAST_VALUE() - ma dostep do ostatniego wiersza w oknie
*/
/*task: Find the lowest and highest sales for each product*/
SELECT
ProductID,
Sales,
FIRST_VALUE(Sales) OVER(PARTITION BY ProductID ORDER BY Sales DESC) [najwiekszaWartosc],
LAST_VALUE(Sales) OVER(PARTITION BY ProductID ORDER BY Sales DESC ROWS BETWEEN CURRENT ROW AND
UNBOUNDED FOLLOWING) [najmniejszaWartosc]
FROM Sales.Orders
/*task: Find the difference is sales between the current and the lowest sales*/
SELECT
ProductID,
Sales,
OrderDate,
ABS(Sales - MIN(Sales) OVER(PARTITION BY ProductID)) [LsDiff]
FROM Sales.Orders