forked from kursysql/kursysql-live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrupowanieDanych.sql
88 lines (54 loc) · 2.25 KB
/
GrupowanieDanych.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
USE AdventureWorks2017
GO
SELECT * FROM HumanResources.Employee
-- liczba pracowników
SELECT count(*) FROM HumanResources.Employee
-- wiek pracowników
SELECT DATEDIFF(YEAR, BirthDate, GETDATE()), * FROM HumanResources.Employee
-- średni wiek pracowników
SELECT AVG(DATEDIFF(YEAR, BirthDate, GETDATE())) FROM HumanResources.Employee
-- suma godzin chorobowych
SELECT SUM(SickLeaveHours) FROM HumanResources.Employee
-- !
SELECT SUM(SickLeaveHours), LoginID FROM HumanResources.Employee
-- dane pogrupowane dla poszczególnych pracowników
SELECT LoginID, SUM(SickLeaveHours) SickLeaveHoursSum, SUM(VacationHours) VacationHoursSum
FROM HumanResources.Employee
GROUP BY LoginID
-- dane pogrupowane dla poszczególnych stanowisk
SELECT JobTitle, count(*) AS Cnt, SUM(SickLeaveHours) SickLeaveHoursSum, SUM(VacationHours) VacationHoursSum
FROM HumanResources.Employee
GROUP BY JobTitle
-- dane pogrupowane dla poszczególnych stanowisk
-- tylko kobiety
SELECT JobTitle, count(*) AS Cnt, SUM(SickLeaveHours) SickLeaveHoursSum, SUM(VacationHours) VacationHoursSum
FROM HumanResources.Employee
WHERE Gender = 'F'
GROUP BY JobTitle
-- tylko mężczyźni
SELECT JobTitle, count(*) AS Cnt, SUM(SickLeaveHours) SickLeaveHoursSum, SUM(VacationHours) VacationHoursSum
FROM HumanResources.Employee
WHERE Gender = 'M'
GROUP BY JobTitle
-- grupowanie po dwóch kolumnach
SELECT Gender, JobTitle, count(*) AS Cnt, SUM(SickLeaveHours) SickLeaveHoursSum, SUM(VacationHours) VacationHoursSum
FROM HumanResources.Employee
GROUP BY Gender, JobTitle
-- sortowanie po wyniku funkcji agregującej
SELECT Gender, JobTitle, count(*) AS Cnt, SUM(SickLeaveHours) SickLeaveHoursSum, SUM(VacationHours) VacationHoursSum
FROM HumanResources.Employee
GROUP BY Gender, JobTitle
ORDER BY Cnt DESC
-- ORDER BY count(*)
-- ! próba filtrowania po wyniku funkcji agregującej
SELECT Gender, JobTitle, count(*) AS Cnt, SUM(SickLeaveHours) SickLeaveHoursSum, SUM(VacationHours) VacationHoursSum
FROM HumanResources.Employee
WHERE count(*) > 9
GROUP BY Gender, JobTitle
ORDER BY Cnt DESC
-- HAVING
SELECT Gender, JobTitle, count(*) AS Cnt, SUM(SickLeaveHours) SickLeaveHoursSum, SUM(VacationHours) VacationHoursSum
FROM HumanResources.Employee
GROUP BY Gender, JobTitle
HAVING count(*) > 9
ORDER BY Cnt DESC