-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_Queries.sql
More file actions
713 lines (580 loc) · 13.1 KB
/
Copy pathSQL_Queries.sql
File metadata and controls
713 lines (580 loc) · 13.1 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
use T5_Padmanabh
select * from northwind_customer
select * from northwind_employee
create table [Dim_Region]
(
[RegionID] int not null,
[RegionDescription] nchar(50) not null
)
select * from dim_region
create table Employee_empid
(
Empid Int,
Ename varchar(50),
Title varchar(15)
)
select * from Employee_empid
create table dim_emp_detail
(
TitleOfCourtesy varchar(10),
First_name varchar(25),
Last_name varchar(25),
Title varchar(50),
City varchar(25),
Country varchar(25)
)
drop table dim_emp_detail
use T5_Padmanabh
create table dim_emp_detail
(
Full_name varchar(50),
Designation varchar(50),
Location varchar(50)
)
select * from dim_emp_detail
create table dim_sales_rep
(
EmployeeId int,
First_name varchar(50),
Last_name varchar(50),
Title varchar(50)
)
use T5_Padmanabh
select * from dim_sales_rep
create table dim_Emp_Loc
(
EmployeeId int,
First_name varchar(50),
Last_name varchar(50),
Title varchar(50),
City varchar(25),
Address varchar(50)
)
use T5_Padmanabh
select * from dim_Emp_Loc
Use Northwind
select * from Employees
where city='LONDON' or city='London'
create table suppliers_empid
(
Company_Name varchar(50),
Contact_Name varchar(50),
Fax nvarchar(20)
)
use northwind
select * from employees
select * from dim_sales_rep
create table dim_emp_uk100
(
EmployeeID int,
LastName varchar(50),
FirstName varchar(50),
Title varchar(75),
TitleOfCourtesy nvarchar(10),
BirthDate datetime,
HireDate datetime,
Address varchar(max),
City varchar(50),
Region varchar(10),
PostalCode nvarchar(20),
Country varchar(10),
HomePhone nvarchar(25),
Extension int,
Photo varbinary(max),
Notes ntext,
ReportsTo int,
PhotoPath nvarchar(255)
)
use T5_Padmanabh
select * from dim_emp_uk100
alter table dim_emp_uk100 alter column Photo varchar(max)
create table dim_sales_97
(
CategoryName varchar(50),
ProductName varchar(50),
ProductSales float
)
Use Northwind
select * from orders
select * from employees
SELECT pro.productname,emp.FirstName
FROM order_details as ordd
join orders as ord on ord.orderid=ordd.orderid
JOIN products as pro on pro.ProductID=ordd.productid
join employees as emp on emp.employeeid=ord.employeeid
where emp.employeeid IN (SELECT EMPLOYEEID FROM EMPLOYEES WHERE TITLE='SALES MANAGER')
use t5_Padmanabh
create table tgt_inflex_t3
(
productname varchar(30),
firstname varchar(20)
)
select * from tgt_inflex_t3
select pro.productname,cat.categoryName,sup.CompanyName
from categories as cat
join products as pro on cat.CategoryID=pro.CategoryID
join Suppliers as sup on pro.SupplierID=sup.SupplierID
select orderid,cust.contactname,orderdate,shipcountry
from orders
join customers as cust on orders.customerid=cust.customerid
where orders.shipcountry='Brazil'
Use T5_Padmanabh
create table tgt_infex_t2
(
orderid varchar(20),
contactname varchar(50),
orderdate datetime,
shipcountry varchar(20)
)
create table tgt_infex_t9
(
orderid varchar(20),
contactname varchar(50),
orderdate datetime,
shipcountry varchar(20),
shipname varchar(20)
)
select * from tgt_infex_t9
select * from tgt_infex_t2
Create table Tgt_Infex_T1
(
ProductName varchar(50),
CategoryName varchar(50),
CompanyName varchar(50)
)
select * from Tgt_infex_t1
select * from orders
select * from orders
where shippeddate=datepart(year,1997)
use T5_Padmanabh
select * from dim_sales_97
use northwind
select * from suppliers
select * from products
select productid,productname,
case when sup.region=NULL then concat((upper(substring(sup.country,1,1))),(upper(substring(sup.city,1,1)))) end as New_Region
from suppliers as sup
join products as pro on sup.supplierid=pro.supplierid
use t5_Padmanabh
create table tgt_infex_t4
(
supplierid int,
companyname varchar(50),
contactname varchar(25),
contacttitle varchar(25),
Address varchar(50),
city varchar(20),
region varchar(15),
postalcode varchar(10),
country varchar(10),
phone int,
fax int,
homepage varchar(max)
)
select * from tgt_infex_t4
alter table tgt_infex_t5 alter column UnitPrice decimal
(
CompanyName varchar(50),
ContactName varchar(50),
City varchar(20),
UnitPrice decimal
)
select CompanyName,ContactName,City,UnitPrice
from products as pro
join suppliers as sup on pro.SupplierID=sup.SupplierID
/* have 2 fetch all records on suppliers table where the value is null in column using logic of case function when in every coloumn=0 or null */
select * from tgt_infex_t5
Use northwind
select ord.CustomerID,ord.ShipName,ter.TerritoryDescription,ordd.UnitPrice,ter.TerritoryID,ord.shipcity,
case
when ordd.Discount=0 then ordd.UnitPrice*0.06
else ordd.Discount
end as Discount
from EmployeeTerritories as empt
join orders as ord on empt.EmployeeID=ord.EmployeeID
join Order_Details as ordd on ord.OrderID=ordd.OrderID
join Territories as ter on empt.TerritoryID=ter.TerritoryID
where (len(cast(ter.TerritoryID as int)))<4 and (ShipVia=1) or(ShipVia=2)
use t5_padmanabh
create table tgt_infex_t6
(
customerid varchar(10),
ShipName varchar(25),
TerritoryDescription varchar(25),
Unitprice decimal,
territoryid int,
shipcity varchar(25),
discount decimal
)
select * from tgt_infex_t6
select ord.OrderID,ord.CustomerID,Ordd.UnitPrice,ordd.Discount
from customers as cust
join orders as ord on cust.CustomerID=ord.CustomerID
join order_details as ordd on ord.OrderID=ordd.OrderID
where ordd.Discount>0
create table tgt_infex_t7
(
orderid varchar(20),
customerid varchar(20),
unitprice int,
quantity int,
discount int
)
select * from tgt_infex_t2
use northwind
select * from employees
use t5_padmanabh
create table tgt_emp
(
EmployeeID int,
firstname varchar(50),
birthdate datetime,
hiredate datetime,
city varchar(25)
)
create table src_emp_scd2
(
EmployeeID int primary key,
firstname varchar(50),
birthdate datetime,
hiredate datetime,
city varchar(25)
)
alter table src_emp_scd2
add empid int
select * from src_emp_scd2
insert into src_emp_scd1
values (1,1,'assa',12-12-2015,15-02-1986,'jay')
select * from tgt_emp_scd1
create table tgt_infex_t8
(
categoryid int,
categoryname varchar(50),
description varchar(50),
productname varchar(50),
supplierid int,
unitprice float
)
create table src_emp2_scd2
(
empid int primary key,
ename varchar(20),
salary int,
create_date date,
update_date date
)
create table tgt_emp2_scd2
(
emp_surr_key int primary key,
empid int,
ename varchar(20),
salary int,
Eff_Start_date date,
Eff_End_date date
)
insert into src_emp2_scd2 values (7369,'smith',24000,'9/15/2015','9/19/2015')
create table emp_src
(
empno int,
ename varchar(25),
job varchar(20),
mgr varchar(20),
hiredate date,
sal int,
deptno int
)
create table emp_trg1
(
emplkey int primary key,
empno int,
ename varchar(25),
job varchar(20),
mgr varchar(20),
hiredate date,
sal int,
deptno int,
start_dt date,
end_dt date
)
use t5_padmanabh
alter table emp_trg1
drop emplkey
alter table emp_trg1 add constraint emp_d_pk primary key (emplkey)
select * from emp_src
insert into emp_src values (1,'padmanabh','clerk','rads','2015-05-05',10000,1)
use northwind
select * from employees
use t5_padmanabh
create table test_emp
(
empno int,
sal int,
start_dt date,
end_dt date,
version int,
flag int
)
create table test_tgt
(
emplkey int primary key,
empno int ,
sal int,
start_dt date,
end_dt date,
version int,
flag int
)
create table products_rank_empid
(
productid int,
productname varchar(100),
supplierid int,
unitprice decimal(10,2),
rank1 int
)
select * from products_rank_empid
create table lkp_employee_id
(
employee_id int not null,
first_name varchar(20) null,
salary numeric(8,2) null,
manager_id int null,
mapping_name varchar(50) null
)
select * from lkp_employee_id ;
delete from lkp_employee_id where employee_id!=100
CREATE TABLE CUST
( CUST_ID int,
CUST_NM VARCHAR(250),
ADDRESS VARCHAR(250),
CITY VARCHAR(50),
STATE VARCHAR(50),
INSERT_DT DATE,
UPDATE_DT DATE)
insert into cust values (80001,'Marion Atkins','100 Main St.','Bangalore','KA','1/7/2011','1/7/2011')
insert into cust values (80002,'Laura Jones','510 Broadway Ave.','Hyderabad','AP','1/7/2011','1/7/2011')
insert into cust values (80003,'Jon Freeman','555 6th Ave.','Bangalore','KA','1/7/2011','1/7/2011')
select * from cust
CREATE TABLE CUST_D1
(
PM_PRIMARYKEY int primary key,
CUST_ID int,
CUST_NM VARCHAR(250),
ADDRESS VARCHAR(250),
CITY VARCHAR(50),
STATE VARCHAR(50),
ACTIVE_DT DATE,
INACTIVE_DT DATE,
INSERT_DT DATE,
UPDATE_DT DATE)
select * from cust_d
-- tgt_infex_t10
create table tgt_infex_t10_shipvia3
(
productname varchar(30),
shipvia varchar(20)
)
alter table tgt_infex_t10_shipvia1
add firstname varchar(20)
select * from tgt_infex_t10_shipvia2
select * from products;
select * from employees;
select * from Orders;
select * from [dbo].[Shippers]
select concat (emp1.FirstName,' ',emp1.LastName),pro.ProductName
from products as pro,employees as emp1
join orders as ord on emp1.EmployeeID=ord.EmployeeID
where emp1.EmployeeID in (select ReportsTo from employees as emp2)
group by emp1.FirstName,emp1.LastName,pro.ProductName
use northwind
select * from [dbo].[Categories]
select * from [dbo].[Products]
use t5_padmanabh
create table dim_sales97
(
categoryname varchar(20),
productname varchar(50),
productsales float
)
use northwind
select * from products
use t5_padmanabh
create table dim_prod_class
(
productid int,
productname varchar(50),
supplierid int,
categoryid int,
quantityperunit varchar(50),
unitprice decimal,
unitsinstock int,
unitsonorder int,
reorderlevel int,
discountinued int
)
drop table dim_prod_class
create table dim_prod_class
(
productid int,
class varchar(5),
quantityperunit int
)
select * from dim_prod_class
select * from dim_prod_class
create table dim_sales
(
orderid int,
productid int,
Unitprice decimal,
quantity int,
discount decimal
)
alter table dim_sales alter column discount varchar(10)
use t5_Padmanabh
select * from Trial_Dim_Sales1
use northwind
select * from customers
create table dim_cust_bad
(
customerid varchar(10),
customername varchar(50),
contactname varchar(50),
contacttitle varchar(25),
address varchar(50),
city varchar(25),
region varchar(20),
postalcode int,
country varchar(20),
phone varchar(20),
fax varchar(20)
)
use t5_padmanabh
select * from dim_cust_clean
create table prod_delivery
(
productname varchar(40),
quantityperunit varchar(20),
supplierid int,
categoryid int,
requiredunits int,
amount decimal
)
select * from prod_delivery
use northwind
use t5_padmanabh
create table Convey_via3
(
orderid int,
CustomerID varchar(10),
employeeid int,
orderdate date,
requiredate date,
shipvia int
)
SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShipVia
FROM Orders
WHERE (ShippedDate IS NULL)
AND (ShipVia = 3)
create table dim_stock_list
(
categoryname varchar(50),
productname varchar(50),
quantityperunit int,
unitsinstock int,
discountinued varchar(10)
)
use northwind
select * from products
select * from categories
use t5_padmanabh
select * from dim_stock_list
select * from [dbo].[Suppliers]
use t5_padmanabh
create table order_with_phone
(
productname varchar(25),
quantityperunit int,
companyname varchar(25),
contactname varchar(25),
fax varchar(25),
categoryid int,
requiredunits int,
amount int
)
SP_RENAME 'order_with_phone.fax','phone'
select * from order_with_fax
create table file_structure
(
firstname varchar(25),
lastname varchar(25),
total_sales decimal
)
use t5_padmanabh
select * from emp_salary
create table emp_id_append
(
empid int,
deptid int,
salary int,
managername varchar(30)
)
alter table emp_id_append alter column empid varchar(10)
select * from emp_id_append
use northwind
select * from orders
use t5_padmanabh
create table ship_Add_num
(
shipaddress varchar(50),
total_sum int
)
select * from ship_add_num
select * from ship_add_num
create table supplier_rollup
(
supplierid int,
productname varchar(50),
total_inventory decimal(15,2)
)
use t5_padmanabh
select * from supplier_rollup
use northwind
select * from products
create table running_sales
(
orderid int,
productid int,
unitprice decimal (10,2),
quantity int,
discount decimal (5,2),
sales decimal (15,2),
running_sum decimal (10,2),
running_avg decimal (10,2)
)
select * from running_sales
use northwind
select * from customers
use t5_padmanabh
create table dim_customers
(
cust_key int,
customerid varchar(10),
companyname varchar(50),
contactname varchar(25),
address varchar(40),
city varchar(25),
region varchar(20),
postalcaode varchar(15),
country varchar(20),
phone varchar(20),
fax varchar(20)
)
select * from dim_customers
create table empl_concat
(
empname varchar(50),
doj varchar(20)
)
select * from empl_concat
use t5_padmanabh
select * from lkp_employee_id