-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_03.sql
38 lines (31 loc) · 1.22 KB
/
04_03.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
-- FILE: 04_03.sql
-- Video: Built-in functions
---------------------------------------------------------------------------------------------------------
USE sandbox; -- to update the Database Name in this Query file or notebook. ()
---------------------------------------------------------------------------------------------------------
select * -- ORIGINAL
from dbo.customer c
inner join dbo.product_order o
on c.customer_id = o.customer_id
---------------------------------------------------------------------------------------------------------
-- WORKING- Group by
select COUNT(*) , c.full_name
from dbo.customer c
inner join dbo.product_order o
on c.customer_id = o.customer_id
GROUP BY C.full_name
-- Sum Total amount spend by customer
SELECT c.full_name, SUM(o.order_total) as Total_spend_by_customer
from dbo.customer c
inner join dbo.product_order o
on c.customer_id = o.customer_id
GROUP BY C.full_name
-- We ge the highest value in order total per Year, per Customer
-- Check for Agreagete Functions
SELECT c.full_name,
yr = year(o.order_date),
highest_order_total =MAX(o.order_total)
from dbo.customer c
inner join dbo.product_order o
on c.customer_id = o.customer_id
GROUP BY c.full_name , year(o.order_date)