-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
122 lines (90 loc) · 2.59 KB
/
Copy pathqueries.sql
File metadata and controls
122 lines (90 loc) · 2.59 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
-- T-SQL query practice against the BikeStores sample database
--1
DECLARE @unit varchar(1) = '-';
;with C as
(
select id, parent, 0 as lvl,CAST(REPLICATE(@unit, 2 + 4 * 0) + txt as varchar(400)) as formatted
from forum
where parent is null
union all
select f.id, f.parent,c.lvl + 1,CAST(REPLICATE(@unit, 2 + 4 * (c.lvl + 1)) + f.txt as varchar(400))
from forum f join C on f.parent = C.id
)
select formatted
from C
order by id;
--2
select p.product_name , count(p.product_name) sumOfProducts
from production.products p
group by p.product_name
having count(p.product_name)>1
--3
;with deleteProducts as
(
select *, ROW_NUMBER() over(partition by p.product_name order by p.product_name) as rowNumber
from production.products p
)
select *
from deleteProducts
where rowNumber>1
--4
select distinct p.product_name
from sales.order_items s join production.products p
on s.product_id=p.product_id
--5
select p.product_id , s.order_id
from sales.order_items s right join production.products p
on s.product_id=p.product_id
where s.product_id is null
--6
select p.product_name , b.brand_name
from production.brands b join production.products p
on b.brand_id=p.brand_id
where b.brand_name in ('Electra','Surly','Trek')
order by b.brand_name
--7
select first_name , last_name ,email, 'c'
from sales.customers
union all
select first_name , last_name ,email, 's'
from sales.staffs s
--8
select first_name , last_name ,email
from sales.customers
where email like '%gmail%'
--9
select *
from sales.orders
where DATEDIFF(DAY,order_date,
case
when shipped_date is not null
then shipped_date
else
GETDATE()
end
) > 2;
--10
select *
from sales.orders
where order_date=EOMONTH(order_date)
--11
Declare @str varchar(50)='1,2,3,4,5,6,7'
;with lastOrder as(
select ROW_NUMBER() over(partition by c.customer_id order by order_date desc ) as rn,c.first_name,c.last_name,c.email,o.customer_id
,o.order_id,o.order_date,sas.store_name,soi.quantity,p.product_name, soi.quantity*soi.list_price*(1-soi.discount) as calPrice
from sales.orders o
join string_split(@str,',') ss on o.customer_id=ss.value
join sales.customers c on o.customer_id=c.customer_id
join sales.stores sas on sas.store_id=o.store_id
join sales.order_items soi on soi.order_id=o.order_id
join production.products p on p.product_id=soi.product_id
)
select *
from lastOrder
where rn =1
--12
declare @cols varchar(100)='first_name,last_name,email,phone'
declare @sortedCols varchar(100)='phone,last_name'
declare @sql nvarchar(max) = 'select '+@cols+' from sales.customers order by '+@sortedCols
print @sql
exec sp_executesql @sql