-
Notifications
You must be signed in to change notification settings - Fork 0
/
agregacao-groupby-join.sql
138 lines (81 loc) · 3.12 KB
/
agregacao-groupby-join.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
--DISTINCT
SELECT DISTINCT FirstName FROM PERSON.Person
SELECT DISTINCT LastName FROM PERSON.Person
--COUNT
SELECT * FROM PERSON.Person
SELECT COUNT(*) FROM PERSON.Person
SELECT COUNT(DISTINCT Title) FROM PERSON.Person
--
SELECT * FROM Production.Product
SELECT count(*) FROM Production.Product
SELECT count(size) FROM Production.Product
SELECT count(distinct size) FROM Production.Product
--MIN - MAX - SUM - AVG
SELECT TOP 10 * FROM Sales.SalesOrderDetail
SELECT TOP 10 SUM(LineTotal)AS TOTAL FROM Sales.SalesOrderDetail
SELECT TOP 10 MIN(LineTotal) FROM Sales.SalesOrderDetail
SELECT TOP 10 MAX(LineTotal) FROM Sales.SalesOrderDetail
SELECT TOP 10 AVG(LineTotal) FROM Sales.SalesOrderDetail
--GROUP BY
SELECT * FROM Sales.SalesOrderDetail
SELECT SpecialOfferID, SUM(UnitPrice)AS SOMA FROM Sales.SalesOrderDetail
GROUP BY SpecialOfferID
SELECT ProductID, COUNT(ProductID)AS CONTAGEM FROM Sales.SalesOrderDetail
GROUP BY ProductID
-----------------------------------
SELECT * FROM PERSON.Person
SELECT FirstName, COUNT(FirstName)AS CONTAGEM FROM PERSON.Person
GROUP BY FirstName
------------------------------------
SELECT COLOR, AVG(ListPrice) FROM Production.Product
WHERE Color='SILVER'
GROUP BY Color
--HAVING
SELECT FirstName, COUNT(FirstName) AS QUANTIDADE
FROM Person.Person
GROUP BY FirstName
HAVING COUNT(FirstName) > 10
-------------------------------------
SELECT TOP 10 * FROM SALES.SalesOrderDetail
SELECT ProductID, SUM(LineTotal) AS TOTAL
FROM SALES.SalesOrderDetail
GROUP BY ProductID
HAVING SUM(LineTotal) BETWEEN 162000 AND 500000
---------------------------------------
SELECT * FROM PERSON.Person
SELECT FirstName, COUNT(FirstName) AS QUANTIDADE
FROM PERSON.Person
WHERE Title = 'Mr.'
GROUP BY FirstName
HAVING COUNT(FirstName) > 10
--INNER JOIN
SELECT TOP 10 * FROM Person.Person
SELECT TOP 10 * FROM Person.EmailAddress
SELECT P.BusinessEntityID, P.FirstName, P.LastName, PE.EmailAddress
FROM Person.Person AS P
INNER JOIN Person.EmailAddress AS PE
ON P.BusinessEntityID = PE.BusinessEntityID
---------------------------------------------
SELECT TOP 10 * FROM Production.Product
SELECT TOP 10 * FROM Production.ProductSubcategory
SELECT P.ListPrice, P.Name, PS.Name
FROM Production.Product AS P
INNER JOIN Production.ProductSubcategory AS PS
ON P.ProductSubcategoryID = PS.ProductSubcategoryID
------------------------------------------------
SELECT TOP 10 *
FROM Person.BusinessEntityAddress BA
INNER JOIN Person.Address PA
ON PA.AddressID = BA.AddressID
--------------------------------------------------
SELECT PP.BusinessEntityID, PT.Name, PT.PhoneNumberTypeID, PP.PhoneNumber
FROM Person.PhoneNumberType PT
INNER JOIN Person.PersonPhone PP
ON PT.PhoneNumberTypeID = PP.PhoneNumberTypeID
---------------------------------------------------
SELECT TOP 10 * FROM Person.StateProvince
SELECT TOP 10 * FROM Person.Address
SELECT AD.AddressID, AD.City, ST.StateProvinceID, ST.Name
FROM Person.StateProvince ST
INNER JOIN Person.Address AD
ON ST.StateProvinceID = AD.StateProvinceID