132 lines
5.0 KiB
Python
132 lines
5.0 KiB
Python
from __future__ import annotations
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# AdventureWorksDW2022 — read-only MSSQL queries
|
|
# Each list contains fallback variants tried in order.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# Daily sales combining FactInternetSales + FactResellerSales
|
|
AW_DAILY_SALES: list[str] = [
|
|
"""
|
|
SELECT
|
|
CAST(d.FullDateAlternateKey AS date) AS sale_date,
|
|
SUM(f.SalesAmount) AS revenue,
|
|
SUM(f.TotalProductCost) AS cost,
|
|
SUM(f.OrderQuantity) AS quantity,
|
|
COUNT_BIG(*) AS orders
|
|
FROM dbo.FactInternetSales AS f
|
|
INNER JOIN dbo.DimDate AS d ON d.DateKey = f.OrderDateKey
|
|
GROUP BY CAST(d.FullDateAlternateKey AS date)
|
|
|
|
UNION ALL
|
|
|
|
SELECT
|
|
CAST(d.FullDateAlternateKey AS date) AS sale_date,
|
|
SUM(r.SalesAmount) AS revenue,
|
|
SUM(r.TotalProductCost) AS cost,
|
|
SUM(r.OrderQuantity) AS quantity,
|
|
COUNT_BIG(*) AS orders
|
|
FROM dbo.FactResellerSales AS r
|
|
INNER JOIN dbo.DimDate AS d ON d.DateKey = r.OrderDateKey
|
|
GROUP BY CAST(d.FullDateAlternateKey AS date)
|
|
|
|
ORDER BY sale_date;
|
|
""",
|
|
# Fallback: internet sales only using OrderDate column directly
|
|
"""
|
|
SELECT
|
|
CAST(OrderDate AS date) AS sale_date,
|
|
SUM(SalesAmount) AS revenue,
|
|
SUM(TotalProductCost) AS cost,
|
|
SUM(OrderQuantity) AS quantity,
|
|
COUNT_BIG(*) AS orders
|
|
FROM dbo.FactInternetSales
|
|
GROUP BY CAST(OrderDate AS date)
|
|
ORDER BY sale_date;
|
|
""",
|
|
]
|
|
|
|
# Sales rep performance — reseller sales attributed to employees
|
|
AW_REP_PERFORMANCE: list[str] = [
|
|
"""
|
|
SELECT
|
|
e.EmployeeKey AS employee_key,
|
|
e.FirstName + ' ' + e.LastName AS rep_name,
|
|
COALESCE(e.Title, 'Sales Rep') AS rep_title,
|
|
COALESCE(st.SalesTerritoryRegion, 'Unknown') AS territory,
|
|
SUM(r.SalesAmount) AS revenue,
|
|
SUM(r.TotalProductCost) AS cost,
|
|
COUNT_BIG(*) AS orders,
|
|
AVG(r.SalesAmount) AS avg_deal_size
|
|
FROM dbo.FactResellerSales AS r
|
|
INNER JOIN dbo.DimEmployee AS e
|
|
ON e.EmployeeKey = r.EmployeeKey
|
|
INNER JOIN dbo.DimSalesTerritory AS st
|
|
ON st.SalesTerritoryKey = r.SalesTerritoryKey
|
|
WHERE e.SalesPersonFlag = 1
|
|
GROUP BY
|
|
e.EmployeeKey,
|
|
e.FirstName, e.LastName,
|
|
e.Title,
|
|
st.SalesTerritoryRegion
|
|
ORDER BY revenue DESC;
|
|
""",
|
|
# Fallback without SalesPersonFlag filter
|
|
"""
|
|
SELECT
|
|
e.EmployeeKey AS employee_key,
|
|
e.FirstName + ' ' + e.LastName AS rep_name,
|
|
COALESCE(e.Title, 'Employee') AS rep_title,
|
|
'Unknown' AS territory,
|
|
SUM(r.SalesAmount) AS revenue,
|
|
SUM(r.TotalProductCost) AS cost,
|
|
COUNT_BIG(*) AS orders,
|
|
AVG(r.SalesAmount) AS avg_deal_size
|
|
FROM dbo.FactResellerSales AS r
|
|
INNER JOIN dbo.DimEmployee AS e ON e.EmployeeKey = r.EmployeeKey
|
|
GROUP BY e.EmployeeKey, e.FirstName, e.LastName, e.Title
|
|
ORDER BY revenue DESC;
|
|
""",
|
|
]
|
|
|
|
# Product demand — internet sales with full category hierarchy
|
|
AW_PRODUCT_DEMAND: list[str] = [
|
|
"""
|
|
SELECT
|
|
p.ProductAlternateKey AS product_id,
|
|
p.EnglishProductName AS product_name,
|
|
COALESCE(pc.EnglishProductCategoryName, 'Unknown') AS category,
|
|
SUM(f.SalesAmount) AS revenue,
|
|
SUM(f.TotalProductCost) AS cost,
|
|
SUM(f.OrderQuantity) AS quantity,
|
|
COUNT_BIG(*) AS orders
|
|
FROM dbo.FactInternetSales AS f
|
|
INNER JOIN dbo.DimProduct AS p
|
|
ON p.ProductKey = f.ProductKey
|
|
LEFT JOIN dbo.DimProductSubcategory AS sc
|
|
ON sc.ProductSubcategoryKey = p.ProductSubcategoryKey
|
|
LEFT JOIN dbo.DimProductCategory AS pc
|
|
ON pc.ProductCategoryKey = sc.ProductCategoryKey
|
|
GROUP BY
|
|
p.ProductAlternateKey,
|
|
p.EnglishProductName,
|
|
pc.EnglishProductCategoryName
|
|
ORDER BY revenue DESC;
|
|
""",
|
|
# Fallback: no category join
|
|
"""
|
|
SELECT
|
|
CAST(f.ProductKey AS nvarchar(50)) AS product_id,
|
|
COALESCE(p.EnglishProductName, CAST(f.ProductKey AS nvarchar(50))) AS product_name,
|
|
'Unknown' AS category,
|
|
SUM(f.SalesAmount) AS revenue,
|
|
SUM(f.TotalProductCost) AS cost,
|
|
SUM(f.OrderQuantity) AS quantity,
|
|
COUNT_BIG(*) AS orders
|
|
FROM dbo.FactInternetSales AS f
|
|
LEFT JOIN dbo.DimProduct AS p ON p.ProductKey = f.ProductKey
|
|
GROUP BY f.ProductKey, p.EnglishProductName
|
|
ORDER BY revenue DESC;
|
|
""",
|
|
]
|