database: Add member_history and different types of business shares
This commit is contained in:
+60
-17
@@ -40,7 +40,7 @@ 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.name, m.given_name,
|
||||
p.sortid, a.attrid, p.cultid,
|
||||
v.sortid, v.type, a.attrid, p.cultid,
|
||||
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.net_weight, p.gebunden,
|
||||
@@ -52,6 +52,7 @@ SELECT p.year, p.did, p.dpnr,
|
||||
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 wine_variety v ON v.sortid = p.sortid
|
||||
LEFT JOIN wine_attribute a ON a.attrid = p.attrid
|
||||
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
|
||||
@@ -59,7 +60,9 @@ ORDER BY p.year, p.did, p.dpnr, o.modid;
|
||||
|
||||
CREATE VIEW v_stat_season AS
|
||||
SELECT year,
|
||||
SUM(weight) AS sum,
|
||||
SUM(weight) AS weight_total,
|
||||
SUM(IIF(type = 'R', weight, 0)) AS weight_red,
|
||||
SUM(IIF(type = 'W', weight, 0)) AS weight_white,
|
||||
ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
|
||||
ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
|
||||
COUNT(DISTINCT did) AS lieferungen,
|
||||
@@ -68,9 +71,11 @@ FROM v_delivery
|
||||
GROUP BY year
|
||||
ORDER BY year;
|
||||
|
||||
CREATE VIEW v_stat_sort AS
|
||||
CREATE VIEW v_stat_variety AS
|
||||
SELECT year, sortid,
|
||||
SUM(weight) AS sum,
|
||||
SUM(weight) AS weight_total,
|
||||
SUM(IIF(type = 'R', weight, 0)) AS weight_red,
|
||||
SUM(IIF(type = 'W', weight, 0)) AS weight_white,
|
||||
ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
|
||||
ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
|
||||
COUNT(DISTINCT did) AS lieferungen,
|
||||
@@ -81,7 +86,9 @@ ORDER BY year, sortid;
|
||||
|
||||
CREATE VIEW v_stat_attr AS
|
||||
SELECT year, attrid,
|
||||
SUM(weight) AS sum,
|
||||
SUM(weight) AS weight_total,
|
||||
SUM(IIF(type = 'R', weight, 0)) AS weight_red,
|
||||
SUM(IIF(type = 'W', weight, 0)) AS weight_white,
|
||||
ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
|
||||
ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
|
||||
COUNT(DISTINCT did) AS lieferungen,
|
||||
@@ -90,9 +97,11 @@ FROM v_delivery
|
||||
GROUP BY year, attrid
|
||||
ORDER BY year, attrid;
|
||||
|
||||
CREATE VIEW v_stat_sort_attr AS
|
||||
CREATE VIEW v_stat_variety_attr AS
|
||||
SELECT year, sortid, attrid,
|
||||
SUM(weight) AS sum,
|
||||
SUM(weight) AS weight_total,
|
||||
SUM(IIF(type = 'R', weight, 0)) AS weight_red,
|
||||
SUM(IIF(type = 'W', weight, 0)) AS weight_white,
|
||||
ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
|
||||
ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
|
||||
COUNT(DISTINCT did) AS lieferungen,
|
||||
@@ -103,7 +112,9 @@ ORDER BY year, sortid, attrid;
|
||||
|
||||
CREATE VIEW v_stat_member AS
|
||||
SELECT year, mgnr,
|
||||
SUM(weight) AS sum,
|
||||
SUM(weight) AS weight_total,
|
||||
SUM(IIF(type = 'R', weight, 0)) AS weight_red,
|
||||
SUM(IIF(type = 'W', weight, 0)) AS weight_white,
|
||||
ROUND(SUM(kmw * weight) / SUM(weight), 2) AS kmw,
|
||||
ROUND(SUM(oe * weight) / SUM(weight), 1) AS oe,
|
||||
COUNT(DISTINCT did) AS lieferungen
|
||||
@@ -136,17 +147,49 @@ FROM v_delivery d
|
||||
JOIN wine_variety v ON v.sortid = d.sortid
|
||||
ORDER BY d.date, d.time;
|
||||
|
||||
CREATE VIEW v_member_history AS
|
||||
SELECT m.mgnr, s.year,
|
||||
m.shares + SUM(IIF(h1.from_type = 1, h1.shares, 0)) - SUM(IIF(h2.to_type = 1, h2.shares, 0)) AS shares,
|
||||
m.shares_red + SUM(IIF(h1.from_type = 2, h1.shares, 0)) - SUM(IIF(h2.to_type = 2, h2.shares, 0)) AS shares_red,
|
||||
m.shares_white + SUM(IIF(h1.from_type = 3, h1.shares, 0)) - SUM(IIF(h2.to_type = 3, h2.shares, 0)) AS shares_white,
|
||||
m.shares_dormant + SUM(IIF(h1.from_type = 9, h1.shares, 0)) - SUM(IIF(h2.to_type = 9, h2.shares, 0)) AS shares_dormant
|
||||
FROM member m, v_virtual_season s
|
||||
LEFT JOIN member_history h1 ON (SUBSTR(h1.date_notice, 1, 4) + 0) > s.year AND h1.from_mgnr = m.mgnr
|
||||
LEFT JOIN member_history h2 ON (SUBSTR(h2.date_notice, 1, 4) + 0) > s.year AND h2.to_mgnr = m.mgnr
|
||||
GROUP BY m.mgnr, s.year
|
||||
ORDER BY m.mgnr, s.year;
|
||||
|
||||
CREATE VIEW v_total_under_delivery AS
|
||||
SELECT s.year, m.mgnr, m.business_shares,
|
||||
m.business_shares * s.min_kg_per_bs AS min_kg,
|
||||
m.business_shares * s.max_kg_per_bs AS max_kg,
|
||||
COALESCE(d.sum, 0) AS weight,
|
||||
IIF(COALESCE(d.sum, 0) < m.business_shares * s.min_kg_per_bs,
|
||||
COALESCE(d.sum, 0) - m.business_shares * s.min_kg_per_bs,
|
||||
IIF(COALESCE(d.sum, 0) > m.business_shares * s.max_kg_per_bs,
|
||||
COALESCE(d.sum, 0) - m.business_shares * s.max_kg_per_bs,
|
||||
0)) AS diff
|
||||
SELECT s.year, m.mgnr,
|
||||
h.shares,
|
||||
h.shares * COALESCE(s.min_kg_per_share, 0) AS min_kg,
|
||||
h.shares * COALESCE(s.max_kg_per_share, 0) AS max_kg,
|
||||
COALESCE(d.weight_total, 0) AS weight_total,
|
||||
IIF(COALESCE(d.weight_total, 0) < h.shares * COALESCE(s.min_kg_per_share, 0),
|
||||
COALESCE(d.weight_total, 0) - h.shares * COALESCE(s.min_kg_per_share, 0),
|
||||
IIF(COALESCE(d.weight_total, 0) > h.shares * COALESCE(s.max_kg_per_share, 0),
|
||||
COALESCE(d.weight_total, 0) - h.shares * COALESCE(s.max_kg_per_share, 0),
|
||||
0)) AS diff,
|
||||
h.shares_red,
|
||||
h.shares_red * COALESCE(s.min_kg_per_share_red, s.min_kg_per_share, 0) AS min_kg_red,
|
||||
h.shares_red * COALESCE(s.max_kg_per_share_red, s.max_kg_per_share, 0) AS max_kg_red,
|
||||
COALESCE(d.weight_red, 0) AS weight_red,
|
||||
IIF(COALESCE(d.weight_red, 0) < h.shares_red * COALESCE(s.min_kg_per_share_red, s.min_kg_per_share, 0),
|
||||
COALESCE(d.weight_red, 0) - h.shares_red * COALESCE(s.min_kg_per_share_red, s.min_kg_per_share, 0),
|
||||
IIF(COALESCE(d.weight_red, 0) > h.shares_red * COALESCE(s.max_kg_per_share_red, s.max_kg_per_share, 0),
|
||||
COALESCE(d.weight_red, 0) - h.shares_red * COALESCE(s.max_kg_per_share_red, s.max_kg_per_share, 0),
|
||||
0)) AS diff_red,
|
||||
h.shares_white,
|
||||
h.shares_white * COALESCE(s.min_kg_per_share_white, s.min_kg_per_share, 0) AS min_kg_white,
|
||||
h.shares_white * COALESCE(s.max_kg_per_share_white, s.max_kg_per_share, 0) AS max_kg_white,
|
||||
COALESCE(d.weight_white, 0) AS weight_white,
|
||||
IIF(COALESCE(d.weight_white, 0) < h.shares_white * COALESCE(s.min_kg_per_share_white, s.min_kg_per_share, 0),
|
||||
COALESCE(d.weight_white, 0) - h.shares_white * COALESCE(s.min_kg_per_share_white, s.min_kg_per_share, 0),
|
||||
IIF(COALESCE(d.weight_white, 0) > h.shares_white * COALESCE(s.max_kg_per_share_white, s.max_kg_per_share, 0),
|
||||
COALESCE(d.weight_white, 0) - h.shares_white * COALESCE(s.max_kg_per_share_white, s.max_kg_per_share, 0),
|
||||
0)) AS diff_white
|
||||
FROM member m, season s
|
||||
LEFT JOIN v_member_history h ON (h.year, h.mgnr) = (s.year, m.mgnr)
|
||||
LEFT JOIN v_stat_member d ON (d.year, d.mgnr) = (s.year, m.mgnr)
|
||||
ORDER BY s.year, m.mgnr;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user