-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquerySQL.sql
More file actions
76 lines (65 loc) · 2.3 KB
/
Copy pathquerySQL.sql
File metadata and controls
76 lines (65 loc) · 2.3 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
select numeregiune,count(numeaeroport)
from aeroport natural join locatii
group by numeregiune
select numeavion,count(*) from avion
natural join zboruri
group by numeavion
having count(*) > (select count(*) from avion
natural join zboruri
where numeavion='Airbus A330')
with numeavion as ( select numeavion as NumeAvion from avion),
Zbor1 as(select datazbor,numeavion from (select numeavion, datazbor,row_number() over(partition by numeavion order by datazbor) numar
from avion
natural join zboruri natural join rezervari
order by datazbor)t
where numar = 1),
Zbor2 as(select datazbor,numeavion as navion2 from (select numeavion, datazbor,row_number() over(partition by numeavion order by datazbor) numar
from avion
natural join zboruri natural join rezervari
order by datazbor)t
where numar = 2),
Zbor3 as(select datazbor,numeavion as navion3 from (select numeavion, datazbor,row_number() over(partition by numeavion order by datazbor) numar
from avion
natural join zboruri natural join rezervari
order by datazbor)t
where numar = 3)
select numeavion.numeavion as NumeAvion,Zbor1.datazbor as Zbor1,Zbor2.datazbor as zbor2,Zbor3.datazbor as zbor3 from numeavion
inner join Zbor1 on numeavion.numeavion=Zbor1.numeavion
left join Zbor2 on numeavion.numeavion=Zbor2.navion2
left join Zbor3 on numeavion.numeavion=Zbor3.navion3
with recursive Concatenare AS
(
SELECT
ID,
CAST(Name AS varchar(100)) AS ListaOrase,
Name,
OrdineNume,
MarcaRegiune
FROM separat
WHERE OrdineNume = 1
UNION ALL
SELECT
s.ID,
CAST(C.ListaOrase || ', ' || s.Name AS varchar(100)),
s.Name,
s.OrdineNume,
s.MarcaRegiune
FROM Separat AS s
INNER JOIN Concatenare AS C
ON s.MarcaRegiune = C.MarcaRegiune
AND s.OrdineNume = C.OrdineNume + 1
),
Separat AS
(
SELECT
idlocatie as ID,
numelocatie as Name,
ROW_NUMBER() OVER (PARTITION BY numeregiune ORDER BY numelocatie) AS OrdineNume,
COUNT(*) OVER (PARTITION BY numeregiune) AS MarcaRegiune
FROM locatii
)
SELECT
ID,
ListaOrase
FROM Concatenare
WHERE OrdineNume = MarcaRegiune