
CREATE VIEW v_plz AS
SELECT plz, p.dest AS bestimmungsort, g.name AS gemeinde, g.gkz, o.name AS ort, o.okz
FROM AT_gem g
    JOIN AT_ort o ON o.gkz = g.gkz
    JOIN AT_plz_dest p ON p.okz = o.okz;

CREATE VIEW v_area_commitment AS
SELECT c.fbnr, m.mgnr, m.family_name, m.given_name,
       c.sortid, c.cultid, c.area,
       c.kgnr, c.gstnr, c.rdnr,
       c.year_from, c.year_to,
       GROUP_CONCAT(DISTINCT a.attrid) AS attributes,
       c.comment
FROM area_commitment c
    JOIN member m ON m.mgnr = c.mgnr
    LEFT JOIN area_commitment_attribute a ON a.fbnr = c.fbnr
GROUP BY c.fbnr
ORDER BY c.fbnr;

CREATE VIEW v_delivery AS
SELECT p.year, p.did, p.dpnr,
       d.date, d.time, d.zwstid, d.lnr, d.lsnr,
       m.mgnr, m.family_name, m.given_name,
       p.sortid, p.weight, p.kmw, ROUND(p.kmw * (4.54 + 0.022 * p.kmw), 0) AS oe, p.qualid, p.hkid, p.kgnr, p.rdnr,
       p.qualid IN (SELECT l.qualid FROM wine_quality_level l WHERE NOT l.predicate AND (p.kmw >= l.min_kmw OR l.min_kmw IS NULL) ORDER BY l.min_kmw DESC LIMIT 1,100) AS abgewertet,
       p.qualid NOT IN ('WEI', 'RSW', 'LDW') AS min_quw,
       GROUP_CONCAT(DISTINCT a.attrid) as attributes, GROUP_CONCAT(DISTINCT o.modid) as modifiers,
       d.comment, p.comment as part_comment
FROM delivery_part p
    JOIN delivery d ON (d.year, d.did) = (p.year, p.did)
    JOIN member m ON m.mgnr = d.mgnr
    LEFT JOIN delivery_part_attribute a ON (a.year, a.did, a.dpnr) = (p.year, p.did, p.dpnr)
    LEFT JOIN delivery_part_modifier o ON (o.year, o.did, o.dpnr) = (p.year, p.did, p.dpnr)
GROUP BY p.year, p.did, p.dpnr
ORDER BY p.year, p.did, p.dpnr;

CREATE VIEW v_delivery_bin AS
SELECT year, mgnr,
       sortid || IIF(min_quw, REPLACE(COALESCE(attributes, ''), ',', ''), '_') AS bin,
       SUM(weight) AS weight
FROM v_delivery
GROUP BY year, mgnr, bin
ORDER BY year, mgnr, LENGTH(bin) DESC, bin;

CREATE VIEW v_stat_season AS
SELECT year,
       SUM(weight) AS sum,
       ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
       ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
       COUNT(DISTINCT did) AS lieferungen,
       COUNT(DISTINCT mgnr) AS mitglieder
FROM v_delivery
GROUP BY year
ORDER BY year;

CREATE VIEW v_stat_sort AS
SELECT year, sortid,
       SUM(weight) as sum,
       ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
       ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
       COUNT(DISTINCT did) AS lieferungen,
       COUNT(DISTINCT mgnr) AS mitglieder
FROM v_delivery
GROUP BY year, sortid
ORDER BY year, sortid;

CREATE VIEW v_stat_attr AS
SELECT year, attributes,
       SUM(weight) as sum,
       ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
       ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
       COUNT(DISTINCT did) AS lieferungen,
       COUNT(DISTINCT mgnr) AS mitglieder
FROM v_delivery
GROUP BY year, attributes
ORDER BY year, LENGTH(attributes) DESC, attributes;

CREATE VIEW v_stat_sort_attr AS
SELECT year, sortid, attributes,
       SUM(weight) as sum,
       ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
       ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
       COUNT(DISTINCT did) AS lieferungen,
       COUNT(DISTINCT mgnr) AS mitglieder
FROM v_delivery
GROUP BY year, sortid, attributes
ORDER BY year, sortid, LENGTH(attributes) DESC, attributes;

CREATE VIEW v_bki_member AS
SELECT s.year, m.mgnr, m.lfbis_nr, m.family_name,
       (COALESCE(m.prefix || ' ', '') || m.given_name || COALESCE(' ' || m.middle_names, '') || COALESCE(' ' || m.suffix, '')) AS name,
       a.name AS billing_name, COALESCE(a.address, m.address) AS address,
       COALESCE(a.country, m.country) AS country, COALESCE(a.postal_dest, m.postal_dest) AS postal_dest,
       SUM(IIF(c.year_from <= s.year AND (c.year_to IS NULL OR c.year_to >= s.year), c.area, 0)) AS area
FROM season s, member m
    LEFT JOIN member_billing_address a ON a.mgnr = m.mgnr
    LEFT JOIN area_commitment c ON c.mgnr = m.mgnr
GROUP BY s.year, m.mgnr;

CREATE VIEW v_bki_delivery AS
SELECT m.lfbis_nr, m.family_name, m.name, m.billing_name,
       m.address, plz.plz, IIF(INSTR(o.name, ',') = 0, o.name, SUBSTR(o.name, 1, INSTR(o.name, ',') - 1)) AS ort,
       d.date, d.weight, v.type, v.sortid, d.qualid, d.year, d.hkid, d.kmw, d.oe,
       m.area
FROM v_delivery d
    JOIN v_bki_member m ON (m.year, m.mgnr) = (d.year, d.mgnr)
    JOIN postal_dest pd ON (pd.country, pd.id) = (m.country, m.postal_dest)
    LEFT JOIN AT_plz_dest ap ON (ap.country, ap.id) = (pd.country, pd.id)
    LEFT JOIN AT_plz plz ON plz.plz = ap.plz
    LEFT JOIN AT_ort o ON o.okz = ap.okz
    JOIN wine_variety v ON v.sortid = d.sortid
ORDER BY d.date, d.time;
