| 1 | = Напредни извештаи од базата (SQL) |
| 2 | |
| 3 | === Листање на секоја реализација заедно со резултат на сите кандидати за таа реализацијата |
| 4 | {{{#!sql |
| 5 | select ri.ri_id, ri.ri_datum, ri.ri_ime, k.kan_id, kan.g_id, g.g_ime, count(distinct gk.ug_id) |
| 6 | from realizacii_na_izbori ri |
| 7 | join realizacii_so_kandidaturi rk on ri.ri_id = rk.ri_id |
| 8 | left join kandidaturi k on rk.ri_id = k.ri_id |
| 9 | left join glasovi_za_kandidat gk on k.kan_id = gk.kan_id |
| 10 | left join kandidati kan on kan.g_id = k.g_id |
| 11 | left join gragjani g on k.g_id = g.g_id |
| 12 | group by ri.ri_id, ri.ri_datum, ri.ri_ime, k.kan_id, kan.g_id, g.g_ime |
| 13 | }}} |
| 14 | |
| 15 | |
| 16 | === Целосни резултати по избирачко место за реализации со кандидатура |
| 17 | {{{#!sql |
| 18 | CREATE EXTENSION IF NOT EXISTS tablefunc; |
| 19 | |
| 20 | DO |
| 21 | $$ |
| 22 | DECLARE |
| 23 | dynamic_sql text; |
| 24 | column_definitions text; |
| 25 | BEGIN |
| 26 | EXECUTE 'SELECT DISTINCT g.g_ime FROM eglas2.kandidaturi kan join eglas2.kandidati k on kan.g_id = k.g_id join eglas2.gragjani g on g.g_id = k.g_id' INTO dynamic_sql; |
| 27 | |
| 28 | column_definitions := (SELECT string_agg(quote_ident(att) || ' bigint', ', ') |
| 29 | FROM (SELECT DISTINCT g.g_ime att |
| 30 | FROM kandidaturi kan |
| 31 | join kandidati k on kan.g_id = k.g_id |
| 32 | join gragjani g on g.g_id = k.g_id) AS t); |
| 33 | |
| 34 | RAISE NOTICE 'Result: %', column_definitions; |
| 35 | |
| 36 | dynamic_sql := format( |
| 37 | 'drop view if exists rezultati_kandidati; ' || |
| 38 | 'create view rezultati_kandidati as ' || |
| 39 | 'SELECT * FROM crosstab(''select im.im_mesto, g.g_ime attribute, ' || |
| 40 | '(select count(gla.ug_id) from glasanja gla join glasovi_za_kandidat glk on gla.ug_id = glk.ug_id where kan_id = kan.kan_id and gla.im_id = im.im_id) value from izbiracki_mesta im cross join kandidaturi kan join kandidati k on kan.g_id = k.g_id join gragjani g on k.g_id = g.g_id order by 1,2'') AS ct(row_name varchar, %s);', |
| 41 | column_definitions |
| 42 | ); |
| 43 | |
| 44 | RAISE NOTICE 'Result: %', dynamic_sql; |
| 45 | EXECUTE dynamic_sql; |
| 46 | |
| 47 | END |
| 48 | $$; |
| 49 | |
| 50 | select * |
| 51 | from rezultati_kandidati |
| 52 | }}} |
| 53 | |
| 54 | |
| 55 | === Целосни резултати по избирачко место за реализации со кандидатска листа |
| 56 | {{{#!sql |
| 57 | CREATE EXTENSION IF NOT EXISTS tablefunc; |
| 58 | |
| 59 | DO |
| 60 | $$ |
| 61 | DECLARE |
| 62 | dynamic_sql text; |
| 63 | column_definitions text; |
| 64 | BEGIN |
| 65 | EXECUTE 'SELECT DISTINCT p.p_ime FROM eglas2.partii p' INTO dynamic_sql; |
| 66 | |
| 67 | column_definitions := (SELECT string_agg(quote_ident(att) || ' bigint', ', ') |
| 68 | FROM (SELECT DISTINCT p.p_ime att |
| 69 | FROM kandidatski_listi kan |
| 70 | join partii p on kan.p_id = p.p_id) AS t); |
| 71 | |
| 72 | RAISE NOTICE 'Result: %', column_definitions; |
| 73 | |
| 74 | dynamic_sql := format( |
| 75 | 'drop view if exists rezultati_partii; |
| 76 | create view rezultati_partii as |
| 77 | select * from crosstab(''select im.im_mesto, p.p_ime, (select count(gla.ug_id) from glasanja gla join glasovi_za_lista glk on gla.ug_id = glk.ug_id join kandidatski_listi kl on kl.kl_id = glk.kl_id where kl.p_id = p.p_id and gla.im_id = im.im_id) from izbiracki_mesta im cross join partii p order by 1, 2'') AS ct(row_name varchar, %s); |
| 78 | ', column_definitions |
| 79 | ); |
| 80 | |
| 81 | RAISE NOTICE 'Result: %', dynamic_sql; |
| 82 | EXECUTE dynamic_sql; |
| 83 | |
| 84 | END |
| 85 | $$; |
| 86 | |
| 87 | select * |
| 88 | from rezultati_partii |
| 89 | }}} |