-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnorthwind_ex1_solucao.sql
68 lines (54 loc) · 1.41 KB
/
northwind_ex1_solucao.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
/*
Problemas Básicos
*/
/*
1. Na tabela OrderDetails, temos os campos UnitPrice e Quantity. Crie um novo campo, TotalPrice e retorne também OrderID, ProductID, UnitPrice e Quantity. Ordene por OrderID e ProductID.
*/
SELECT OrderId
,ProductID
,UnitPrice
,Quantity
,(UnitPrice * Quantity) AS TotalPrice
FROM OrderDetails
ORDER BY OrderId, ProductID;
/*
2. Quantos clientes temos na tabela Clientes?
*/
SELECT COUNT(CustomerID) AS TotalCustomers
FROM Customers;
/*
3. Mostre a data do primeiro pedido já feito na tabela Pedidos.
*/
SELECT OrderDate
FROM Orders
ORDER BY OrderDate ASC limit 1;
SELECT OrderDate
FROM Orders
WHERE OrderDate = (SELECT MIN(OrderDate)
FROM Orders);
/*
4. Mostre uma lista de países onde a empresa Northwind tem clientes.
*/
SELECT Country
FROM Customers
GROUP BY Country;
SELECT DISTINCT Country
FROM Customers;
/*
4B. Liste a quantidade de pedidos feitos por cada funcionário.
*/
SELECT O.EmployeeID, E.FirstName, COUNT(OrderID) AS OrderCount
FROM Orders AS O
JOIN Employees AS E
ON O.EmployeeID = E.EmployeeID
GROUP BY EmployeeID;
/*
4C. Liste o total de vendas (valor total) por cada cliente e ordene por nome de Companyname.
*/
SELECT C.CustomerID, C.Companyname, SUM(UnitPrice * Quantity) AS TotalSales
FROM Orders AS O
JOIN Customers AS C
ON O.CustomerID = C.CustomerID
JOIN OrderDetails AS D
ON O.OrderID = D.OrderID
GROUP BY CustomerID;