我有这个查询,可以检索本周和上周某些产品的数量。我希望这个数量与相应的周相关,而不是累积。
SELECT 'Last Week' AS Last_week,
SUM( CASE WHEN week IN ( 1 ) THEN tinta ELSE 0 END ) AS last_week_tinta,
SUM( CASE WHEN week IN ( 1 ) THEN semi ELSE 0 END ) AS last_week_semi,
SUM( CASE WHEN week IN ( 1 ) THEN so_pure ELSE 0 END ) AS last_week_so_pure
FROM wp_8_bi_helper_custom t
WHERE NOT EXISTS ( SELECT 1 FROM wp_8_bi_helper_custom t2 WHERE t2.person = t.person AND t2.week = t.week AND t2.iteration > t.iteration )
UNION ALL
SELECT 'Current Week' AS Current_Week,
SUM( CASE WHEN week IN ( 2 ) THEN tinta ELSE 0 END ) AS tinta,
SUM( CASE WHEN week IN ( 2 ) THEN semi ELSE 0 END ) AS semi,
SUM( CASE WHEN week IN ( 2 ) THEN so_pure ELSE 0 END ) AS so_pure
FROM wp_11_bi_helper_custom t
WHERE NOT EXISTS ( SELECT 1 FROM wp_8_bi_helper_custom t2 WHERE t2.person = t.person AND t2.week = t.week AND t2.iteration > t.iteration )
然而,上周的结果是正确的,但本周不是。
我得到了这个结果
Last_week last_week_tinta last_week_semi last_week_so_pure
Last Week 2 1 3
Current Week 13 3 1
我应该什么时候
Last_week last_week_tinta last_week_semi last_week_so_pure
Last Week 2 1 3
Current Week 0 0 1
第一栏仅用于标注目的,但需要在那里。 我不是SQL的专家,所以任何帮助都表示赞赏。