| 1 | = Przykłady SQL do raportów = |
| 2 | |
| 3 | == Użyteczne konstrukcje i funkcje językowe SQL == |
| 4 | |
| 5 | {{{ |
| 6 | --Formatuje datę |
| 7 | SELECT to_char(d.adddat, 'YYYY-MM-DD') FROM documents d; |
| 8 | |
| 9 | -- Pobiera rok |
| 10 | select extract(YEAR from CURRENT_DATE); |
| 11 | |
| 12 | -- Oblicza czas który upłynął od daty do daty (np. na realizacji zadania) |
| 13 | ((extract(EPOCH FROM d.rlend_) - extract(EPOCH FROM d.rlstrt))/3600)::numeric(12,2) AS duration |
| 14 | |
| 15 | -- Zamienia liczbę sekund na ciąg: H:M:S - np. 00:30:03 |
| 16 | SELECT (1803 || 's')::interval |
| 17 | |
| 18 | -- Formatuje kwotę z pól tekstowych np. z formularzy customowych |
| 19 | select cast(regexp_replace(regexp_replace('301 110,43',' ',''),',', '.') as numeric(12,2)) + 12.50; |
| 20 | |
| 21 | -- Warunkowo koloruje pole |
| 22 | CASE WHEN p.pr_sta = 1 THEN '<div style="color: red">Rozpoczęte</div>' WHEN p.pr_sta = 2 THEN 'W trakcie realizacji' WHEN p.pr_sta = 3 THEN 'Zakończone' END AS status, |
| 23 | |
| 24 | -- Wybiera te rekordy których data jest w tym tugodniu |
| 25 | to_char(CURRENT_DATE, 'WW') = to_char (adddat, 'WW') |
| 26 | |
| 27 | -- wybiera rekordy nie starsze niż miesiąc |
| 28 | AND dcr.adddat > CURRENT_DATE - interval '1 month' |
| 29 | |
| 30 | }}} |
| 31 | |
| 32 | == Przykładowe użyteczne zapytania do bazy edokumenty == |
| 33 | |
| 34 | {{{ |
| 35 | |
| 36 | -- Pobranie danych z formularzy dynamicznych |
| 37 | |
| 38 | SELECT to_char(d.adddat, 'YY-MM-DD') AS day, |
| 39 | rozmowy |
| 40 | (SELECT count(*) /10 |
| 41 | FROM events_view e WHERE e.trmtyp = 'PHONECALL' AND emp_id IN (84,62) AND e.start_ >= d.adddat::date - 7 |
| 42 | AND e.start_ <= d.adddat) AS rozmowy_handlowe, |
| 43 | spotkania |
| 44 | (SELECT count(*) |
| 45 | FROM events_view e WHERE e.trmtyp = 'MEETING' AND emp_id IN (84,62) AND e.start_ >= d.adddat::date - 7 |
| 46 | AND e.start_ <= d.adddat) AS spotkania, |
| 47 | wartość pola featid 98 |
| 48 | f3.data__::int AS odwedok, |
| 49 | f4.ftopnm AS cecha4 |
| 50 | FROM documents d |
| 51 | INNER JOIN features_text_view f3 ON d.doc_id = f3.tbl_id AND f3.featid = 98 |
| 52 | cecha z listy wyboru |
| 53 | INNER JOIN features_opt_view f4 ON d.doc_id = f4.tbl_id AND f4.ftopid = 119 |
| 54 | WHERE d.is_del IS NOT true AND d.gostof IS NULL |
| 55 | |
| 56 | |
| 57 | -- Wybiera symbol teczki z numeru sprawy oo formacie ''2/03/08/UP/AW'' |
| 58 | |
| 59 | SELECT substring(symbol from '[0-9]*/[0-9]*/[0-9]*/([A-Z]*)') FROm processes; |
| 60 | |
| 61 | |
| 62 | -- Wybiera krótką nazwę klienta jeśli jest, a jeśli jest pusta to długą |
| 63 | |
| 64 | SELECT COALESCE(c.name_2, c.name_1) FROM contacts c; |
| 65 | |
| 66 | |
| 67 | Wybiera sprawy z wartościami cech (tekstowe i opcje) |
| 68 | |
| 69 | SELECT p.dscrpt, p.symbol, 'PROCESS'::text AS clsnam, prc_id AS keyval, |
| 70 | f1.data__ AS opinia, f2.ftopnm AS reklamacja, f3.data__ AS dzialanie_korygujace, |
| 71 | f4.data__ AS dzialanie_Klient, f5.data__ AS przyczyna |
| 72 | FROM processes_view p |
| 73 | LEFT JOIN features_text_view f1 ON p.prc_id = f1.tbl_id AND f1.featid = 14 |
| 74 | LEFT JOIN features_opt_view f2 ON p.prc_id = f2.tbl_id AND f2.featid = 15 |
| 75 | LEFT JOIN features_text_view f3 ON p.prc_id = f3.tbl_id AND f3.featid = 16 |
| 76 | LEFT JOIN features_text_view f4 ON p.prc_id = f4.tbl_id AND f4.featid = 17 |
| 77 | LEFT JOIN features_text_view f5 ON p.prc_id = f5.tbl_id AND f5.featid = 18 |
| 78 | WHERE p.prtpid = 1 |
| 79 | |
| 80 | |
| 81 | -- Wybiera dane do trendu - do wykresu |
| 82 | |
| 83 | SELECT extract(month from rlstrt) as m, |
| 84 | sum((time__::numeric(12,2)/3600)::numeric(12,2)) AS sum |
| 85 | FROM rcp_cards_view |
| 86 | WHERE rlstrt IS NOT NULL AND is_fak = TRUE AND is_del = FALSE AND tpstid = 9 |
| 87 | AND rlstrt + interval '1 year' >= CURRENT_DATE |
| 88 | GROUP BY extract(year from rlstrt), |
| 89 | extract(month from rlstrt) |
| 90 | ORDER BY extract(year from rlstrt), extract(month from rlstrt) |
| 91 | |
| 92 | }}} |