-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5 solved SQL problems based on supplier tables
More file actions
169 lines (147 loc) · 6.32 KB
/
5 solved SQL problems based on supplier tables
File metadata and controls
169 lines (147 loc) · 6.32 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*----------------------------------------------------------------------------------------------
Problem 1: Which are the top suppliers for World Wide Importers company?
-----------------------------------------------------------------------------------------------*/
--all supplier to the company
drop table if exists #temp_table1;
select distinct z.*
into #temp_table1
from (select a.[SupplierID]
,a.[SupplierName]
,b.[SupplierCategoryName]
,a.[PostalAddressLine2]
,a.[PostalPostalCode]
from [WideWorldImporters].[Purchasing].[Suppliers] a
left join [WideWorldImporters].[Purchasing].[SupplierCategories] b
on a.[SupplierCategoryID]=b.[SupplierCategoryID])z;
--purchase details
drop table if exists #temp_table2;
select distinct z.*
into #temp_table2
from (SELECT a.[PurchaseOrderID]
,a.[SupplierID]
,a.[OrderDate]
,a.[ExpectedDeliveryDate]
,b.[PurchaseOrderLineID]
,b.[StockItemID]
,b.[OrderedOuters]
,b.[Description]
,b.[ReceivedOuters]
,b.[ExpectedUnitPricePerOuter]
,b.[LastReceiptDate]
FROM [WideWorldImporters].[Purchasing].[PurchaseOrders] a
left join [WideWorldImporters].[Purchasing].[PurchaseOrderLines] b
on a.PurchaseOrderID=b.PurchaseOrderID)z;
--purchase orders where received outers is not 0
drop table if exists #temp_table3;
select distinct z.*
into #temp_table3
from (select *,ReceivedOuters*ExpectedUnitPricePerOuter as line_order_amount from
#temp_table2 where receivedOuters<>0)z;
--group by supplier id
drop table if exists #temp_table4;
select distinct z.*
into #temp_table4
from (select SupplierID,sum(line_order_amount) as total_amount from #temp_table3
group by SupplierID)z;
--add suppier data
drop table if exists #temp_table5;
select distinct z.*
into #temp_table5
from (select a.*,b.SupplierName,b.SupplierCategoryName,b.PostalAddressLine2,b.PostalPostalCode from #temp_table4 a
left join #temp_table1 b on a.SupplierID=b.SupplierID
)z order by total_amount desc;
/*
Fabrikam, Inc. Clothing Supplier 678066222.00 Eaglemont
Litware, Inc. Packaging Supplier 266115587.90 Jackson
Northwind Electric Cars Toy Supplier 78816.50 Arlington
The Phone Company Novelty Goods Supplier 50820.00 Ferny Wood
A Datum Corporation Novelty Goods Supplier 25023.00 Surrey
Graphic Design Institute Novelty Goods Supplier 6489.00 Willow
Contoso, Ltd. Novelty Goods Supplier 313.50 Jolimont
*/
/*----------------------------------------------------------------------------------------------
Problem 2: Average order price for each supplier
-----------------------------------------------------------------------------------------------*/
--group by supplier id
drop table if exists #temp_table6;
select distinct z.*,z1.SupplierName,z1.SupplierCategoryName
into #temp_table6
from (select SupplierID,cast(avg(line_order_amount) as decimal(12,2)) as average_amount from #temp_table3
group by SupplierID)z
left join #temp_table1 z1
on z.SupplierID=z1.SupplierID
order by average_amount desc;
/*
Fabrikam, Inc. Clothing Supplier 116988.65
Litware, Inc. Packaging Supplier 107564.91
Northwind Electric Cars Toy Supplier 4378.69
The Phone Company Novelty Goods Supplier 3388.00
A Datum Corporation Novelty Goods Supplier 2502.30
Graphic Design Institute Novelty Goods Supplier 154.50
Contoso, Ltd. Novelty Goods Supplier 104.50
*/
/*----------------------------------------------------------------------------------------------
Problem 3: Supplier invoices which have not been paid
-----------------------------------------------------------------------------------------------*/
drop table if exists #temp_table7;
select distinct z.*, b.SupplierName,b.SupplierCategoryName
into #temp_table7
from (select PurchaseOrderID,TransactionDate,AmountExcludingTax,TaxAmount,TransactionAmount,OutstandingBalance,SupplierID
FROM [WideWorldImporters].[Purchasing].[SupplierTransactions]
where OutstandingBalance>0)z
left join #temp_table1 b on z.SupplierID=b.SupplierID;
/*
PurchaseOrderID- 2071, 2072
*/
/*----------------------------------------------------------------------------------------------
Problem 4: Orders which have not been received
-----------------------------------------------------------------------------------------------*/
drop table if exists #temp_table8;
select distinct z.*,z1.[OrderDate],z1.[SupplierID],z2.SupplierName,z3.[TransactionDate]
,z3.[AmountExcludingTax]
,z3.[TaxAmount]
,z3.[TransactionAmount]
,z3.[OutstandingBalance]
into #temp_table8
from (SELECT [PurchaseOrderLineID]
,[PurchaseOrderID]
,[OrderedOuters]
,[Description]
,[ReceivedOuters]
,[ExpectedUnitPricePerOuter]
FROM [WideWorldImporters].[Purchasing].[PurchaseOrderLines]
where [ReceivedOuters]=0)z
left join [WideWorldImporters].[Purchasing].[PurchaseOrders] z1
on z.PurchaseOrderID=z1.PurchaseOrderID
left join [WideWorldImporters].[Purchasing].[Suppliers] z2
on z1.SupplierID=z2.SupplierID
left join [WideWorldImporters].[Purchasing].[SupplierTransactions] z3
on z.PurchaseOrderID=z3.PurchaseOrderID;
/*
PurchaseOrderID- 2073, 2074
Invoice not found for these orders
*/
/*----------------------------------------------------------------------------------------------
Problem 5: Highest selling items of each supplier
-----------------------------------------------------------------------------------------------*/
drop table if exists #temp_table8;
select z.*
into #temp_table8
from (SELECT a.[PurchaseOrderLineID],a.[PurchaseOrderID],a.[Description],a.[ReceivedOuters],b.[SupplierID],c.SupplierName,d.SupplierCategoryName
FROM [WideWorldImporters].[Purchasing].[PurchaseOrderLines] a
left join [WideWorldImporters].[Purchasing].[PurchaseOrders] b
on a.PurchaseOrderID=b.PurchaseOrderID
left join [WideWorldImporters].[Purchasing].[Suppliers] c
on b.SupplierID=c.SupplierID
left join [WideWorldImporters].[Purchasing].[SupplierCategories] d
on c.SupplierCategoryID=d.SupplierCategoryID
)z;
drop table if exists #temp_table9;
select distinct z1.*
into #temp_table9
from (select z.*, DENSE_RANK() over (partition by z.supplierName order by z.sold_quantity desc) as rank
from (SELECT SupplierName,SupplierCategoryName,[Description],sum(ReceivedOuters) as sold_quantity
FROM #temp_table8
group by SupplierName,SupplierCategoryName,[Description]
)z)z1 where z1.rank=1;
--select * from #temp_table9