{
  "engine": "SQLite 3.49.1",
  "postgresql": "Portable SQL written for PostgreSQL as well. PostgreSQL engine execution is not claimed.",
  "fixture": "Existing operations.json / Python CSV project; supplier mapping is a new explicitly synthetic dimension.",
  "source_rows": 36,
  "products": 6,
  "checks": [
    "independent revenue reconciliation",
    "last cumulative revenue equals total",
    "clean fixture quality checks",
    "negative fixture orphan and invalid amount detected"
  ],
  "queries": [
    {
      "id": "01_join",
      "tool": "JOIN",
      "title_ru": "Продажи → товар → поставщик",
      "title_en": "Sales → product → supplier",
      "sql": "SELECT sp.supplier_name, p.category, COUNT(*) AS sale_lines,\n SUM(s.quantity*s.unit_price) AS revenue,\n SUM(s.quantity*p.unit_cost) AS cost_of_sales\nFROM demo_sales s\nJOIN demo_products p ON p.sku=s.sku\nJOIN demo_suppliers sp ON sp.supplier_id=p.supplier_id\nGROUP BY sp.supplier_name,p.category\nORDER BY revenue DESC,sp.supplier_name;",
      "rows": [
        {
          "supplier_name": "Demo Tableware Supplier",
          "category": "Tableware",
          "sale_lines": 12,
          "revenue": 779850,
          "cost_of_sales": 413850
        },
        {
          "supplier_name": "Demo Coffee Supplier",
          "category": "Coffee",
          "sale_lines": 12,
          "revenue": 650250,
          "cost_of_sales": 386550
        },
        {
          "supplier_name": "Demo Tea Supplier",
          "category": "Tea",
          "sale_lines": 12,
          "revenue": 285600,
          "cost_of_sales": 153825
        }
      ]
    },
    {
      "id": "02_finance",
      "tool": "CTE",
      "title_ru": "Проверяемая финансовая цепочка",
      "title_en": "Auditable financial calculation",
      "sql": "WITH lines AS (\n SELECT s.sale_id,s.quantity*s.unit_price AS revenue,\n s.quantity*p.unit_cost AS cost\n FROM demo_sales s JOIN demo_products p ON p.sku=s.sku\n), totals AS (\n SELECT SUM(revenue) AS revenue,SUM(cost) AS cost_of_sales FROM lines\n)\nSELECT revenue,cost_of_sales,revenue-cost_of_sales AS gross_profit,\n ROUND(100.0*(revenue-cost_of_sales)/NULLIF(revenue,0),2) AS margin_pct\nFROM totals;",
      "rows": [
        {
          "revenue": 1715700,
          "cost_of_sales": 954225,
          "gross_profit": 761475,
          "margin_pct": 44.38
        }
      ]
    },
    {
      "id": "03_running",
      "tool": "Window SUM",
      "title_ru": "Накопительная выручка",
      "title_en": "Cumulative revenue",
      "sql": "WITH monthly AS (\n SELECT substr(sale_date,1,7) AS month,SUM(quantity*unit_price) AS revenue\n FROM demo_sales GROUP BY substr(sale_date,1,7)\n)\nSELECT month,revenue,SUM(revenue) OVER (\n ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW\n) AS running_revenue FROM monthly ORDER BY month;",
      "rows": [
        {
          "month": "2026-01",
          "revenue": 247200,
          "running_revenue": 247200
        },
        {
          "month": "2026-02",
          "revenue": 263800,
          "running_revenue": 511000
        },
        {
          "month": "2026-03",
          "revenue": 277100,
          "running_revenue": 788100
        },
        {
          "month": "2026-04",
          "revenue": 295700,
          "running_revenue": 1083800
        },
        {
          "month": "2026-05",
          "revenue": 307200,
          "running_revenue": 1391000
        },
        {
          "month": "2026-06",
          "revenue": 324700,
          "running_revenue": 1715700
        }
      ]
    },
    {
      "id": "04_ranking",
      "tool": "DENSE_RANK",
      "title_ru": "Рейтинг товаров",
      "title_en": "Product ranking",
      "sql": "WITH sku_totals AS (\n SELECT p.sku,p.category,SUM(s.quantity*s.unit_price) AS revenue\n FROM demo_sales s JOIN demo_products p ON p.sku=s.sku\n GROUP BY p.sku,p.category\n)\nSELECT sku,category,revenue,\n DENSE_RANK() OVER (ORDER BY revenue DESC) AS overall_rank,\n DENSE_RANK() OVER (PARTITION BY category ORDER BY revenue DESC) AS category_rank\nFROM sku_totals ORDER BY overall_rank,sku;",
      "rows": [
        {
          "sku": "POT-01",
          "category": "Tableware",
          "revenue": 468000,
          "overall_rank": 1,
          "category_rank": 1
        },
        {
          "sku": "COF-01",
          "category": "Coffee",
          "revenue": 382950,
          "overall_rank": 2,
          "category_rank": 1
        },
        {
          "sku": "POT-02",
          "category": "Tableware",
          "revenue": 311850,
          "overall_rank": 3,
          "category_rank": 2
        },
        {
          "sku": "COF-02",
          "category": "Coffee",
          "revenue": 267300,
          "overall_rank": 4,
          "category_rank": 2
        },
        {
          "sku": "TEA-01",
          "category": "Tea",
          "revenue": 174900,
          "overall_rank": 5,
          "category_rank": 1
        },
        {
          "sku": "TEA-02",
          "category": "Tea",
          "revenue": 110700,
          "overall_rank": 6,
          "category_rank": 2
        }
      ]
    },
    {
      "id": "05_categories",
      "tool": "GROUP BY",
      "title_ru": "Сезонность категорий",
      "title_en": "Category seasonality",
      "sql": "SELECT substr(s.sale_date,1,7) AS month,p.category,\n SUM(s.quantity) AS units,SUM(s.quantity*s.unit_price) AS revenue\nFROM demo_sales s JOIN demo_products p ON p.sku=s.sku\nGROUP BY substr(s.sale_date,1,7),p.category ORDER BY month,category;",
      "rows": [
        {
          "month": "2026-01",
          "category": "Coffee",
          "units": 150,
          "revenue": 93000
        },
        {
          "month": "2026-01",
          "category": "Tableware",
          "units": 190,
          "revenue": 111600
        },
        {
          "month": "2026-01",
          "category": "Tea",
          "units": 210,
          "revenue": 42600
        },
        {
          "month": "2026-02",
          "category": "Coffee",
          "units": 160,
          "revenue": 99150
        },
        {
          "month": "2026-02",
          "category": "Tableware",
          "units": 200,
          "revenue": 118950
        },
        {
          "month": "2026-02",
          "category": "Tea",
          "units": 225,
          "revenue": 45700
        },
        {
          "month": "2026-03",
          "category": "Coffee",
          "units": 170,
          "revenue": 105300
        },
        {
          "month": "2026-03",
          "category": "Tableware",
          "units": 210,
          "revenue": 126300
        },
        {
          "month": "2026-03",
          "category": "Tea",
          "units": 225,
          "revenue": 45500
        },
        {
          "month": "2026-04",
          "category": "Coffee",
          "units": 180,
          "revenue": 111450
        },
        {
          "month": "2026-04",
          "category": "Tableware",
          "units": 220,
          "revenue": 133650
        },
        {
          "month": "2026-04",
          "category": "Tea",
          "units": 250,
          "revenue": 50600
        },
        {
          "month": "2026-05",
          "category": "Coffee",
          "units": 190,
          "revenue": 117600
        },
        {
          "month": "2026-05",
          "category": "Tableware",
          "units": 230,
          "revenue": 141000
        },
        {
          "month": "2026-05",
          "category": "Tea",
          "units": 240,
          "revenue": 48600
        },
        {
          "month": "2026-06",
          "category": "Coffee",
          "units": 200,
          "revenue": 123750
        },
        {
          "month": "2026-06",
          "category": "Tableware",
          "units": 240,
          "revenue": 148350
        },
        {
          "month": "2026-06",
          "category": "Tea",
          "units": 260,
          "revenue": 52600
        }
      ]
    },
    {
      "id": "06_abc",
      "tool": "CASE WHEN + windows",
      "title_ru": "ABC и граница класса",
      "title_en": "ABC and boundary handling",
      "sql": "WITH sku_totals AS (\n SELECT sku,SUM(quantity*unit_price) AS revenue FROM demo_sales GROUP BY sku\n), shares AS (\n SELECT sku,revenue,SUM(revenue) OVER () AS total_revenue,\n COALESCE(SUM(revenue) OVER (ORDER BY revenue DESC,sku\n ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),0) AS revenue_before\n FROM sku_totals\n)\nSELECT sku,revenue,ROUND(100.0*revenue/NULLIF(total_revenue,0),2) AS share_pct,\n CASE WHEN total_revenue=0 THEN NULL\n WHEN revenue_before < total_revenue*0.8 THEN 'A'\n WHEN revenue_before < total_revenue*0.95 THEN 'B' ELSE 'C' END AS abc\nFROM shares ORDER BY revenue DESC,sku;",
      "rows": [
        {
          "sku": "POT-01",
          "revenue": 468000,
          "share_pct": 27.28,
          "abc": "A"
        },
        {
          "sku": "COF-01",
          "revenue": 382950,
          "share_pct": 22.32,
          "abc": "A"
        },
        {
          "sku": "POT-02",
          "revenue": 311850,
          "share_pct": 18.18,
          "abc": "A"
        },
        {
          "sku": "COF-02",
          "revenue": 267300,
          "share_pct": 15.58,
          "abc": "A"
        },
        {
          "sku": "TEA-01",
          "revenue": 174900,
          "share_pct": 10.19,
          "abc": "B"
        },
        {
          "sku": "TEA-02",
          "revenue": 110700,
          "share_pct": 6.45,
          "abc": "B"
        }
      ]
    },
    {
      "id": "07_lag_lead",
      "tool": "LAG / LEAD",
      "title_ru": "Изменение по месяцам",
      "title_en": "Month-over-month change",
      "sql": "WITH monthly AS (\n SELECT substr(sale_date,1,7) AS month,SUM(quantity*unit_price) AS revenue\n FROM demo_sales GROUP BY substr(sale_date,1,7)\n), neighbors AS (\n SELECT month,revenue,LAG(revenue) OVER (ORDER BY month) AS previous_revenue,\n LEAD(revenue) OVER (ORDER BY month) AS next_revenue FROM monthly\n)\nSELECT month,revenue,previous_revenue,next_revenue,\n revenue-previous_revenue AS change_amount,\n ROUND(100.0*(revenue-previous_revenue)/NULLIF(previous_revenue,0),2) AS change_pct\nFROM neighbors ORDER BY month;",
      "rows": [
        {
          "month": "2026-01",
          "revenue": 247200,
          "previous_revenue": null,
          "next_revenue": 263800,
          "change_amount": null,
          "change_pct": null
        },
        {
          "month": "2026-02",
          "revenue": 263800,
          "previous_revenue": 247200,
          "next_revenue": 277100,
          "change_amount": 16600,
          "change_pct": 6.72
        },
        {
          "month": "2026-03",
          "revenue": 277100,
          "previous_revenue": 263800,
          "next_revenue": 295700,
          "change_amount": 13300,
          "change_pct": 5.04
        },
        {
          "month": "2026-04",
          "revenue": 295700,
          "previous_revenue": 277100,
          "next_revenue": 307200,
          "change_amount": 18600,
          "change_pct": 6.71
        },
        {
          "month": "2026-05",
          "revenue": 307200,
          "previous_revenue": 295700,
          "next_revenue": 324700,
          "change_amount": 11500,
          "change_pct": 3.89
        },
        {
          "month": "2026-06",
          "revenue": 324700,
          "previous_revenue": 307200,
          "next_revenue": null,
          "change_amount": 17500,
          "change_pct": 5.7
        }
      ]
    },
    {
      "id": "08_quality",
      "tool": "PostgreSQL-compatible SQL",
      "title_ru": "Контроль полноты и ключей",
      "title_en": "Completeness and key quality",
      "sql": "SELECT 'orphan_sku' AS check_name,COUNT(*) AS issue_count\nFROM demo_sales s LEFT JOIN demo_products p ON p.sku=s.sku WHERE p.sku IS NULL\nUNION ALL SELECT 'invalid_amount',COUNT(*) FROM demo_sales\n WHERE quantity IS NULL OR quantity<0 OR unit_price IS NULL OR unit_price<0\nUNION ALL SELECT 'missing_product_cost',COUNT(*) FROM demo_products\n WHERE unit_cost IS NULL OR unit_cost<0\nUNION ALL SELECT 'duplicate_business_line',COUNT(*) FROM (\n SELECT sale_date,sku,quantity,unit_price FROM demo_sales\n GROUP BY sale_date,sku,quantity,unit_price HAVING COUNT(*)>1\n) duplicates;",
      "rows": [
        {
          "check_name": "orphan_sku",
          "issue_count": 0
        },
        {
          "check_name": "invalid_amount",
          "issue_count": 0
        },
        {
          "check_name": "missing_product_cost",
          "issue_count": 0
        },
        {
          "check_name": "duplicate_business_line",
          "issue_count": 0
        }
      ]
    }
  ]
}