-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzadaniedodatkowe
319 lines (307 loc) · 10.5 KB
/
zadaniedodatkowe
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
--zad dodatkowe
--1a
select e."EmployeeID"
, e."FirstName"
, e."LastName"
, o."OrderID"
, o."OrderDate"
from employees e
join orders o on e."EmployeeID" = o."EmployeeID"
where e."EmployeeID" in (2,4,5);
--1b
select e."EmployeeID"
, e."FirstName"
, e."LastName"
, o."OrderID"
, o."OrderDate"
from employees e
join orders o on e."EmployeeID" = o."EmployeeID"
where e."EmployeeID" % 2 = 0
order by e."EmployeeID";
--1c
select distinct upper("FirstName")
from employees
order by upper("FirstName");
--1d
select "OrderDate"
, "OrderID"
from orders o
where date_part('year',"OrderDate") = 2016;
--1e
select "LastName"
, "FirstName"
, case
when random() > 0.7 then 'dodatkowy dzien wolny'
when random() > 0.1 then 'brak nagrody'
else 'musisz zrobic nadgodziny'
end zmienna_losowa
from employees e
where "EmployeeID" = 1;
--1f
select e."EmployeeID"
, e."FirstName"
, e."LastName"
, et."TerritoryID"
, t."TerritoryDescription"
from employees e
join employeeterritories et on e."EmployeeID" = et."EmployeeID"
join territories t on et."TerritoryID" = t."TerritoryID" ;
--1g
select *,
replace(replace(s."Phone",'(',''),')','') new_phone
, coalesce(o."ShipRegion",o."ShipCity") new_Ship_Region
from orders o
join shippers s on o."ShipVia" = s."ShipperID"
where (date_part('month',o."OrderDate") in (10,11,12)) and (date_part('year',o."OrderDate") = 1996);
select
o."OrderID"
, o."OrderDate"
, coalesce (o."ShipRegion", o."ShipCity")
, s."ShipperID"
, replace(replace(s."Phone",'(',''),')','') as new_phone
, EXTRACT(QUARTER FROM o."OrderDate") as kwartal
from orders o
join shippers s on s."ShipperID" = o."ShipVia"
where EXTRACT(QUARTER FROM o."OrderDate") = 1 and EXTRACT(YEAR FROM o."OrderDate") = 1997
;
--1h
select p."ProductName"
, c."CategoryName"
, s."CompanyName"
from orders o
join order_details od on o."OrderID" = od."OrderID"
join products p on od."ProductID" = p."ProductID"
join categories c on p."CategoryID" = c."CategoryID"
join shippers s on o."ShipVia" = s."ShipperID"
where p."UnitsInStock" < 15
or od."Discount" > 0;
_________________________________________
--2a
select count(e."EmployeeID")
from employees e
where lower(e."Title") like '%manager';
--2b
/*select min("HireDate" )
from employees e */
select count(e."EmployeeID")
, abs(round((('1992-04-01')-e."HireDate")/365,0))as liczba_lat
from employees e
group by round((('1992-04-01')-e."HireDate")/365,0);
--2c
select "FirstName"
, "LastName"
, "BirthDate"
, "HireDate"
, round((e."HireDate" - e."BirthDate")/365,0) as wiek_w_momencie_zatrudnienia
from employees e;
--2c- max wartosc
select
max(round((e."HireDate" - e."BirthDate")/365,0)) as max_wiek
from employees e;
--2d = 9
select *
from employees e
--2d - i
select count(e."EmployeeID")
from employees e
where ('2013-11-01'- e."BirthDate")/ 365> 50;
--2d - ii
select count(e."EmployeeID")
from employees e
where ((('2013-11-01'- e."BirthDate")/365 > 65) and (lower(e."TitleOfCourtesy") in ('ms.','mrs.'))) or ((('2013-11-01'- e."BirthDate")/365 > 65) and (lower(e."TitleOfCourtesy") in ('mr.','dr.')));
--2d - iii
select count(e."EmployeeID")
from employees e
where ('2013-11-01'- e."HireDate")/365 > 3;
--2d - iv
select avg(('2013-11-01'- e."HireDate")/365) as sr_staz_pracy
, min(('2013-11-01'- e."HireDate")/365) as min_staz_pracy
, max(('2013-11-01'- e."HireDate")/365) as max_staz_pracy
from employees e;
--2e
select min(o."OrderDate")
, max(o."OrderDate")
from orders o;
--2f
select c."CompanyName"
, o."OrderDate"
, count(o."OrderID") as liczba_zam
, round(min(od."UnitPrice"*od."Quantity"*(1-od."Discount"))::numeric,2) as min_kwota_zam
, round(max(od."UnitPrice"*od."Quantity"*(1-od."Discount"))::numeric,2) as max_kwota_zam
, round(avg(od."UnitPrice"*od."Quantity"*(1-od."Discount"))::numeric,2) as sr_kwota_zam
, round(sum(od."UnitPrice"*od."Quantity"*(1-od."Discount"))::numeric,2) as sum_kwota_zam
, round(min(o."Freight")::numeric,2) as min_freight
, round(max(o."Freight")::numeric,2) as max_freight
, round(avg(o."Freight")::numeric,2) as sr_freight
, round(sum(o."Freight")::numeric,2) as sum_freight
, case
when date_part('month',o."OrderDate") in (1,2,3) then 1
when date_part('month',o."OrderDate") in (4,5,6) then 2
when date_part('month',o."OrderDate") in (7,8,9) then 3
when date_part('month',o."OrderDate") in (10,11,12) then 4
end as kwartal
from orders o
join customers c on o."CustomerID" = c."CustomerID"
join order_details od on o."OrderID" = od."OrderID"
where date_part('year',o."OrderDate") = (select date_part('year',max(o."OrderDate")) from orders o)
group by kwartal, o."OrderDate", o."OrderID", c."CompanyName";
--2g
select c."CompanyName"
, o."OrderDate"
, count(o."OrderID") liczba_zam
, round(min(o."Freight")::numeric,2) min_freight
, round(max(o."Freight")::numeric,2) max_freight
, round(avg(o."Freight")::numeric,2) sr_freight
, round(sum(o."Freight")::numeric,2) sum_freight
, case
when date_part('month',o."OrderDate") in (1,2,3) then 1
when date_part('month',o."OrderDate") in (4,5,6) then 2
when date_part('month',o."OrderDate") in (7,8,9) then 3
when date_part('month',o."OrderDate") in (10,11,12) then 4
end kwartal
from orders o
join customers c on o."CustomerID" = c."CustomerID"
join order_details od on o."OrderID" = od."OrderID"
where date_part('year',o."OrderDate") = (select date_part('year',min(o."OrderDate")) from orders o)
group by kwartal, o."OrderDate", o."OrderID", c."CompanyName";
--2h
select c."CompanyName"
, o."OrderDate"
, count(o."OrderID") liczba_zam
, round(min(o."Freight")::numeric,2) min_freight
, round(max(o."Freight")::numeric,2) max_freight
, round(avg(o."Freight")::numeric,2) sr_freight
, round(sum(o."Freight")::numeric,2) sum_freight
, case
when date_part('month',o."OrderDate") in (1,2,3) then 1
when date_part('month',o."OrderDate") in (4,5,6) then 2
when date_part('month',o."OrderDate") in (7,8,9) then 3
when date_part('month',o."OrderDate") in (10,11,12) then 4
end kwartal
from orders o
join customers c on o."CustomerID" = c."CustomerID"
join order_details od on o."OrderID" = od."OrderID"
where date_part('year',o."OrderDate") = (select date_part('year',max(o."OrderDate")) from orders o)
group by kwartal, o."OrderDate", o."OrderID", c."CompanyName"
union
select c."CompanyName"
, o."OrderDate"
, count(o."OrderID") liczba_zam
, round(min(o."Freight")::numeric,2) min_freight
, round(max(o."Freight")::numeric,2) max_freight
, round(avg(o."Freight")::numeric,2) sr_freight
, round(sum(o."Freight")::numeric,2) sum_freight
, case
when date_part('month',o."OrderDate") in (1,2,3) then 1
when date_part('month',o."OrderDate") in (4,5,6) then 2
when date_part('month',o."OrderDate") in (7,8,9) then 3
when date_part('month',o."OrderDate") in (10,11,12) then 4
end kwartal
from orders o
join customers c on o."CustomerID" = c."CustomerID"
join order_details od on o."OrderID" = od."OrderID"
where date_part('year',o."OrderDate") = (select date_part('year',min(o."OrderDate")) from orders o)
group by kwartal, o."OrderDate", o."OrderID", c."CompanyName";
--2i - a moze distinct?
select c."CompanyName"
, o."OrderDate"
, count(o."OrderID") liczba_zam
, round(min(o."Freight")::numeric,2) min_freight
, round(max(o."Freight")::numeric,2) max_freight
, round(avg(o."Freight")::numeric,2) sr_freight
, round(sum(o."Freight")::numeric,2) sum_freight
, case
when date_part('month',o."OrderDate") in (1,2,3) then 1
when date_part('month',o."OrderDate") in (4,5,6) then 2
when date_part('month',o."OrderDate") in (7,8,9) then 3
when date_part('month',o."OrderDate") in (10,11,12) then 4
end kwartal
from orders o
join customers c on o."CustomerID" = c."CustomerID"
join order_details od on o."OrderID" = od."OrderID"
where date_part('year',o."OrderDate") = (select date_part('year',max(o."OrderDate")) from orders o)
or date_part('year',o."OrderDate") = (select date_part('year',min(o."OrderDate")) from orders o)
group by kwartal, o."OrderDate", o."OrderID", c."CompanyName";
--2j
select count("FirstName") as imie
, count("LastName") as nazwisko
, count(distinct "FirstName") as distint_imie
, count(distinct "LastName") as distinct_nazwisko
from employees e;
--2k
create view v_pracownicy as
select *
from employees e;
select *
, round(('1998-05-01'-"BirthDate")/365,2) wiek_pracownika
, round(('1998-05-01'-"HireDate")/30,2) staz_pracy
, case
when round(('1998-05-01'-"HireDate")/30,2) < 6 then '2 tygodnie'
when round(('1998-05-01'-"HireDate")/30,2) < 36 then '1 miesiac'
else '3 miesiace'
end okres_wypowiedzenia
from v_pracownicy;
--2l
select o."OrderID"
, count(od."ProductID") liczba_prod
, sum(od."Quantity") ilosc_prod
, case
when o."Freight" > 20 then 'ok'
else 'nie ok'
end fright_powyzej_20
, round(sum(od."Quantity"*od."UnitPrice"*(1-od."Discount"))::numeric,2) kwota_zamowienia
, round(sum(od."Quantity"*od."UnitPrice"*od."Discount")::numeric,2) kwota_rabatu
from orders o
join order_details od on o."OrderID" = od."OrderID"
group by o."OrderID"
order by o."OrderID";
--zamowienia z rabatem
select count(o."OrderID")
from orders o
join order_details od on o."OrderID" = od."OrderID"
where od."Discount" > 0;
________________________________________________
--3a - i
select * from employees e;
insert into employees
values (10,'Hewelt', 'Katarzyna', 'CEO', 'Ms.', '1993-05-20', '2010-06-01', 'Jana Pawla II 10', 'Smolno', 'pomorskie', '84100', 'POLAND');
--3a - ii
update employees
set "HireDate" = '2021-03-01'
where lower("LastName") = 'hewelt';
--3a - iii
update employees
set "ReportsTo" = 10
where "ReportsTo" is null;
--3a iv
create view v_tymczas_zamowienia as
select *
from orders o;
update v_tymczas_zamowienia
set "ShipRegion" = 'Brak danych'
where "ShipRegion" is null;
delete from v_tymczas_zamowienia
where lower("ShipCountry") = 'switzerland';
--3a - v
update v_tymczas_zamowienia
set "ShipName" = concat(s."CompanyName",' ',"ShipName")
from shippers s;
______________________________________
--4a - i
select *
, e."FirstName"
from orders o
join employees e on o."EmployeeID" = e."EmployeeID"
where lower(e."FirstName") not in ('janet','margaret');
--4a - ii
select e."EmployeeID"
, count(et."TerritoryID") liczba_terytoriow
from employees e
join employeeterritories et on e."EmployeeID" = et."EmployeeID"
join territories t on et."TerritoryID" = t."TerritoryID"
group by e."EmployeeID";
--4a - iii
select
distinct "OrderDate"
from orders o
where "EmployeeID" != 1;