-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule_2_Sprint_1_Sales_reason.sql
32 lines (32 loc) · 1.1 KB
/
Module_2_Sprint_1_Sales_reason.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
WITH reasons_count AS(
SELECT
DISTINCT SalesOrderID OrderID,
COUNT (SalesReasonID) reasons_no
FROM
`adwentureworks_db.salesorderheadersalesreason`
GROUP BY SalesOrderID
)
--
SELECT
salesheader.SalesOrderID,
salesheader.OrderDate,
LEFT(CAST(DATE_TRUNC(salesheader.OrderDate, Year) AS STRING), 4) AS YEAR,
salesheader.CustomerID,
salesheader.SalesPersonID,
salesheader.TerritoryID,
salesheader.TotalDue,
CASE
WHEN salesheader.SalesPersonID IS NULL THEN salesreason.Name
ELSE 'Sales representative'
END SalesReason,
COALESCE(reasons_count.reasons_no, 1) CountReason,
ROUND( salesheader.TotalDue / COALESCE(reasons_count.reasons_no, 1), 2) AdaptedTotalSales
FROM `tc-da-1.adwentureworks_db.salesorderheader` salesheader
LEFT JOIN `adwentureworks_db.salesorderheadersalesreason` salesreasonjoin
ON salesheader.SalesOrderID = salesreasonjoin.SalesOrderID
LEFT JOIN `adwentureworks_db.salesreason` salesreason
ON salesreason.SalesReasonID = salesreasonjoin.SalesReasonID
LEFT JOIN reasons_count
ON reasons_count.OrderID = salesheader.SalesOrderID
ORDER BY SalesOrderID DESC
LIMIT 1000