forked from PhaniSankarKoppula/Learn-ShareChannel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject1_SQL_Scripts.sql
More file actions
247 lines (229 loc) · 6.71 KB
/
Project1_SQL_Scripts.sql
File metadata and controls
247 lines (229 loc) · 6.71 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
CREATE TABLE [dbo].[Raw_Sales](
[Transaction Date] [date] NULL,
[Shipment Date] [date] NULL,
[Product] [nvarchar](255) NULL,
[Product Line] [nvarchar](255) NULL,
[Model Name] [nvarchar](255) NULL,
[Subcategory] [nvarchar](255) NULL,
[Category] [nvarchar](255) NULL,
[Seller Country] [nvarchar](255) NULL,
[Seller Customer] [nvarchar](255) NULL,
[Buyer Country] [nvarchar](255) NULL,
[Sales] [money] NULL,
[Sales Quantity] [float] NULL,
[Buyer Customer] [varchar](250) NULL,
[Seller Customer Code] [varchar](50) NULL,
[Buyer Customer Code] [varchar](50) NULL,
[Seller Country Code] [varchar](10) NULL,
[Buyer Country Code] [varchar](10) NULL,
[Sales In Net USD] [decimal](32, 2) NULL,
[Sales In NDP USD] [decimal](32, 2) NULL,
[Sales In ASP USD] [decimal](32, 2) NULL,
[Sales Base Qty] [float] NULL,
[Sales Prod Qty] [float] NULL
) ON [PRIMARY]
GO
GO
-- Fact Table
-- Sales
--[Transaction Date](FK)
--Seller Customer Code(FK)
--Buyer Customer Code(FK)
--Seller Geo Code(FK)
--Buyer Geo Code(FK)
--Product (FK)
--Sales In Net USD
--Sales In NDP USD
--Sales In ASP USD
--Sales Base Qty
--Sales Prod Qty
--DataLoad_TimeStamp
--Dimension Tables.
--Time
--[Transaction Date](PK)
--CY_Month
--CY_Quarter
--CY_HalfYear
--CY_Year
--FY_Month
--FY_Quarter
--FY_HalfYear
--FY_Year
--DataLoad_TimeStamp
--Seller Customer
--Seller Customer Code(PK)
--Seller Customer Name
--DataLoad_TimeStamp
--Buyer Customer
--Buyer Customer Code(PK)
--Buyer Customer Name
--DataLoad_TimeStamp
--Seller Geo
--Seller Geo Code(PK)
--Seller Geo Country
--Seller Sub Region
--Seller Region
--DataLoad_TimeStamp
--Buyer Geo
--Buyer Geo Code(PK)
--Buyer Country
--Buyer Sub Region
--Buyer Region
--DataLoad_TimeStamp
--Product
--Product (PK)
--Product Line
--Model Name
--SubCategory
--Category
--DataLoad_TimeStamp
--Fact Tables
drop table if exists Fact_Sales
go
Create Table Fact_Sales([Transaction Date] date,SellerCustomerCode Varchar(75),BuyerCustomerCode Varchar(75),SellerGeoCode varchar(10)
,BuyerGeoCode varchar(10),Product varchar(75),SalesInNetUSD decimal(32,2),SalesInNDPUSD decimal(32,2),SalesInASPUSD decimal(32,2)
,SalesBaseQty float,SalesProdQty float,DataLoad_TimeStamp datetime)
go
insert into Fact_Sales
SELECT [Transaction Date]
,[Seller Customer Code]
,[Buyer Customer Code]
,[Seller Country Code]
,[Buyer Country Code]
,[Product]
,[Sales In Net USD]
,[Sales In NDP USD]
,[Sales In ASP USD]
,[Sales Base Qty]
,[Sales Prod Qty]
,getdate()
FROM [POC].[dbo].[Raw_Sales]
--Dimension Tables
drop table if exists DimTime
GO
Create table DimTime (
[Transaction Date] date
,CY_Month varchar(25)
,CY_Quarter varchar(25)
,CY_HalfYear varchar(25)
,CY_Year varchar(25)
,FY_Month varchar(25)
,FY_Quarter varchar(25)
,FY_HalfYear varchar(25)
,FY_Year varchar(25)
,DataLoad_TimeStamp datetime)
GO
insert into DimTime
select Cast(PK_Date as Date) as Date,
[Month_Name] as CY_Month,
[Quarter_Name] as CY_Quarter,
[Half_Year_Name] as CY_HalfYear,
[Year_Name] as CY_Year,
[Fiscal_Month_Name] as FY_Month,
[Fiscal_Quarter_Name] as FY_Quarter,
[Fiscal_Half_Year_Name] as FY_HalfYear,
[Fiscal_Year_Name] as FY_Year
,getdate()
from time
drop table if exists DimSellerCustomer
GO
Create table DimSellerCustomer (
[SellerCustomerCode] Varchar(75)
,[SellerCustomerName] varchar(250)
,[DataLoad_TimeStamp] datetime)
GO
insert into DimSellerCustomer
select distinct [Seller Customer Code],[Seller Customer], getdate() FROM [POC].[dbo].[Raw_Sales]
drop table if exists DimBuyerCustomer
GO
Create table DimBuyerCustomer (
[BuyerCustomerCode] Varchar(75)
,[BuyerCustomerName] varchar(250)
,[DataLoad_TimeStamp] datetime)
GO
insert into DimBuyerCustomer
select distinct [Buyer Customer Code],[Buyer Customer], getdate() FROM [POC].[dbo].[Raw_Sales]
drop table if exists DimSellerGeo
GO
Create table DimSellerGeo(
[Seller Geo Code] varchar(10)
,[Seller Geo Country] varchar(75)
,[Seller Sub Region] varchar(75)
,[Seller Region] varchar(75)
,[DataLoad_TimeStamp] datetime)
GO
Insert into DimSellerGeo
SELECT distinct
[Seller Country Code],
[Seller Country],
Case
when [Seller Country Code]='AU' then 'Australia'
when [Seller Country Code]='BE' then 'Central Europe'
when [Seller Country Code]='CA' then 'North America'
when [Seller Country Code]='FR' then 'North Europe'
when [Seller Country Code]='UK' then 'United Kingdom'
when [Seller Country Code]='US' then 'North America' end ,
Case
when [Seller Country Code]='AU' then 'Asiapacific'
when [Seller Country Code]='BE' then 'Europe'
when [Seller Country Code]='CA' then 'Americas'
when [Seller Country Code]='FR' then 'Europe'
when [Seller Country Code]='UK' then 'Europe'
when [Seller Country Code]='US' then 'Americas' end ,
Getdate()
FROM [POC].[dbo].[Raw_Sales]
drop table if exists DimBuyerGeo
GO
Create table DimBuyerGeo(
[Buyer Geo Code] varchar(10)
,[Buyer Geo Country] varchar(75)
,[Buyer Sub Region] varchar(75)
,[Buyer Region] varchar(75)
,[DataLoad_TimeStamp] datetime)
GO
Insert into DimBuyerGeo
SELECT distinct
[Buyer Country Code],
[Buyer Country],
Case
when [Buyer Country Code]='AU' then 'Australia'
when [Buyer Country Code]='BE' then 'Central Europe'
when [Buyer Country Code]='CA' then 'North America'
when [Buyer Country Code]='FR' then 'North Europe'
when [Buyer Country Code]='UK' then 'United Kingdom'
when [Buyer Country Code]='US' then 'North America' end ,
Case
when [Buyer Country Code]='AU' then 'Asiapacific'
when [Buyer Country Code]='BE' then 'Europe'
when [Buyer Country Code]='CA' then 'Americas'
when [Buyer Country Code]='FR' then 'Europe'
when [Buyer Country Code]='UK' then 'Europe'
when [Buyer Country Code]='US' then 'Americas' end ,
Getdate()
FROM [POC].[dbo].[Raw_Sales]
drop table if exists DimProduct
GO
Create table DimProduct(
[Product] varchar(75)
,[Product Line] varchar(75)
,[Model Name] varchar(75)
,[SubCategory] varchar(75)
,[Category] varchar(75)
,[DataLoad_TimeStamp] datetime)
GO
insert into DimProduct
SELECT distinct
[Product]
,[Product Line]
,[Model Name]
,[Subcategory]
,[Category]
,getdate()
FROM [POC].[dbo].[Raw_Sales]
select * from [dbo].[Fact_Sales]
select * from [dbo].[DimBuyerCustomer]
select * from [dbo].[DimBuyerGeo]
select * from [dbo].[DimProduct]
select * from [dbo].[DimSellerCustomer]
select * from [dbo].[DimSellerGeo]
select * from [dbo].[DimTime]