我有一个表销售,其中我显示了特定地区产品的当前季度平均值以及该产品上一季度的销售情况。我的表有一个像
这样的结构Region Product Year Qtr Month Sales
NORTH P1 2015 1 JAN 1000
SOUTH P1 2015 1 FEB 3000
NORTH P1 2015 1 MAR 3000
NORTH P1 2015 2 APR 4000
NORTH P1 2015 2 MAY 5000
NORTH P1 2015 2 JUN 6000
NORTH P1 2015 3 JUL 7000
NORTH P1 2015 3 AUG 8000
NORTH P1 2015 3 SEP 9000
NORTH P1 2015 4 OCT 1000
NORTH P1 2015 4 NOV 2000
NORTH P1 2015 4 DEC 4000
........
SOUTH P3 2015 4 NOV 3000
NORTH P1 2015 1 FEB 2000
SOUTH P1 2015 1 JAN 2000
SOUTH P1 2015 1 JAN 4000
SOUTH P2 2016 1 JAN 2000
SOUTH P2 2016 1 JAN 4000
NORTH P1 2016 1 MAR 3000
NORTH P1 2016 1 FEB 1000
NORTH P1 2016 1 JAN 2000
我已经输入了这种形式 -
Region Product Year Month Sales Qtr Avg_Sale Prev_Qtr_Sale
NORTH P1 2015 JAN 1000 1 2000 null
NORTH P1 2015 FEB 2000 1 2000 null
NORTH P1 2015 MAR 3000 1 2000 null
NORTH P1 2015 APR 4000 2 5000 2000
NORTH P1 2015 MAY 5000 2 5000 2000
NORTH P1 2015 JUN 6000 2 5000 2000
NORTH P1 2015 JUL 7000 3 8000 5000
NORTH P1 2015 AUG 8000 3 8000 5000
NORTH P1 2015 SEP 9000 3 8000 5000
NORTH P1 2015 OCT 1000 4 2333.3 8000
NORTH P1 2015 NOV 2000 4 2333.3 8000
NORTH P1 2015 DEC 4000 4 2333.33 8000
SOUTH P2 2015 JAN 8000 1 6000 null
SOUTH P2 2015 FEB 9000 1 6000 null
SOUTH P2 2015 MAR 1000 1 6000 null
SOUTH P2 2015 APR 2000 2 6333.33 6000
SOUTH P2 2015 MAY 8000 2 6333.33 6000
SOUTH P2 2015 JUN 9000 2 6333.33 6000
SOUTH P2 2015 JUL 1000 3 2333.33 6333.33
SOUTH P2 2015 AUG 2000 3 2333.33 6333.33
SOUTH P2 2015 SEP 4000 3 2333.33 6333.33
SOUTH P2 2015 OCT 5000 4 2666.67 2333.33
SOUTH P2 2015 NOV 2000 4 2666.67 2333.33
SOUTH P2 2015 DEC 1000 4 2666.67 2333.33
NORTH P3 2015 FEB 9000 1 5000 null
NORTH P3 2015 FEB 1000 1 5000 null
NORTH P3 2015 APR 2000 2 2000 5000
NORTH P3 2015 JUL 8000 3 8000 2000
SOUTH P3 2015 AUG 9000 3 9000 null
SOUTH P3 2015 OCT 1000 4 2000 9000
SOUTH P3 2015 NOV 3000 4 2000 9000
NORTH P1 2016 JAN 2000 1 2000 2333.33
NORTH P1 2016 FEB 1000 1 2000 2333.33
NORTH P1 2016 MAR 3000 1 2000 2333.33
SOUTH P2 2016 JAN 2000 1 3000 2666.67
SOUTH P2 2016 JAN 4000 1 3000 2666.67
SOUTH P1 2015 JAN 4000 1 3000 null
SOUTH P1 2015 JAN 2000 1 3000 null
SOUTH P1 2015 FEB 3000 1 3000 null
null显示上一季度该产品没有销售。 查询是 -
WITH AvgSales
AS (SELECT
region,
product,
year,
qtr,
ROUND(AVG(sales), 2) AS avg_Sale
FROM sales
GROUP BY region,
product,
year,qtr
)
SELECT
s.region,
s.product,
s.year,
s.month,
s.sales,
avg.qtr,
avg.avg_Sale AS Qtr_Avg_Sale,
prev.avg_sale AS Prev_Qtr_Avg_Sale
FROM sales s
JOIN AvgSales avg
ON s.region = avg.region
AND s.product = avg.product
AND s.QTR = avg.qtr
AND s.year = avg.year
LEFT JOIN AvgSales prev
ON (s.region = prev.region
AND s.product = prev.product
AND s.year - 1 = prev.year
AND prev.qtr = 4) or
(s.region = prev.region
AND s.product = prev.product
AND s.year = prev.year
AND s.qtr - 1 = prev.qtr) ;
我有问题在第二次加入时加入表如何加入表作为连接条件,为什么在此连接条件下它不包括前一年的2,3,4季度
s.region = prev.region
AND s.product = prev.product
AND s.year - 1 = prev.year
AND prev.qtr = 4
任何人都可以帮忙吗?
答案 0 :(得分:1)
我非常确定Prev_Qtr_Sale
列旨在显示Qtr
和Year
列中一年中该季度之前的季度的平均销售额 - 所以{ 2015年Qtr
中的{1}} 2,应显示2015年第1季度的平均值,依此类推。
如果是这样的话,那么第一组条件中的遗漏就会加入Year
- 别名的CTE;这组条件应特别适用于prev
为1的特殊情况(因此加入上一年的最后一个季度),但条件Qtr
已被省略。
因此,完整的连接条件应如下所示:
s.qtr = 1