Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL cvičení #190

Open
kokes opened this issue May 19, 2022 · 1 comment
Open

SQL cvičení #190

kokes opened this issue May 19, 2022 · 1 comment

Comments

@kokes
Copy link
Owner

kokes commented May 19, 2022

Různý věci v reálným životě jdou dobře použít na trénování SQL

https://twitter.com/honzabartosek/status/1526999774985175041

tady konkrétně chybí odfiltrování vládních činitelů, řazení atd. A jistě by to šlo napsat líp

with poslanci as (
SELECT
		id_osoba, jmeno, prijmeni
	FROM
		psp.poslanci_osoby
		full outer JOIN psp.poslanci_zarazeni zar USING (id_osoba)
		full outer JOIN psp.poslanci_organy org ON org.id_organ = zar.id_of
	WHERE org.zkratka = 'PSP9' and zar.do_o is null
)
SELECT
		id_osoba,
		max(jmeno) jmeno,
		max(prijmeni) prijmeni,
		count(distinct nazev_organu_cz) pocet,
		string_agg(DISTINCT nazev_organu_cz, ', ') soucasne_organy
	FROM
		poslanci
		left  JOIN psp.poslanci_zarazeni zar USING (id_osoba)
		left JOIN psp.poslanci_organy org ON org.id_organ = zar.id_of and cl_funkce=0 and do_o is null and do_organ is null AND id_typ_organu in (3,4)
	GROUP BY
		id_osoba
@kokes
Copy link
Owner Author

kokes commented Aug 11, 2022

Nejvyšší/nejnižší průměrný věk kandidátů do zastupitelstva (ještě join na strany)

SELECT
	kodzastup, ostrana, max(ko.nazevzast) nazev_zastup, count(*) kandidatu, avg(vek)::numeric(10,2) vek_prumer
FROM
	volby.komunalni_kandidati kn
	inner join volby.komunalni_obce ko using(datum, kodzastup)
WHERE
	datum = '2022-09-23'
	group by ostrana, kodzastup
having avg(vek) > 70 and count(*) > 10
order by 5 desc
LIMIT 10;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant