子查询带来的总计与按自身查询的不同

bd1hkmkf  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(213)

我正在学习子查询,而不是在ssrs中使用查找。
我创建了一个查询,列出了每个商店的总退款费用,然后构建了一个子查询,汇总每个月所有商店的总退款费用。当我单独运行子查询时,它会列出一定的金额,但是我将它添加到主查询中,它会为每个商店列出不同的金额。例如

Store    Branch_Sum    Month_Sum     Month
1           325         750           1/1/2016 
2           75          1100           1/1/2016
3           525          850          1/1/2016

每个月的总和应该是相同的,而分支总和应该是不同的,加起来就是月份的总和。当查询是分开的时,这是有效的,但是现在当我把它们放在一起时。

SELECT SUM(AMOUNT) AS BRANCH_SUM, SUM(AggTots.POSTMONTHSUM) AS MonthSum,
CASE WHEN FOCUS_TELLER_ID BETWEEN 101 AND 199 THEN 1 
WHEN FOCUS_TELLER_ID BETWEEN 201 AND 299 THEN 2 
WHEN FOCUS_TELLER_ID BETWEEN 301 AND 399 THEN 3 
ELSE 'OTHER' END AS Branch,
DATEADD(MONTH, DATEDIFF(MONTH, 0, FEE_LOG.ENTRY_DATE), 0) AS PostMonth
FROM FEE_LOG

Inner JOIN (
SELECT SUM(AMOUNT) AS POSTMONTHSUM, MEMBER_NBR AS MEM,
DATEADD(MONTH, DATEDIFF(MONTH, 0, FEE_LOG.ENTRY_DATE), 0) AS PostMonth
FROM FEE_LOG
WHERE FOCUS_TELLER_ID < 4000
AND AMOUNT > 0
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, FEE_LOG.ENTRY_DATE), 0), Member_NBR
)AggTots on FEE_LOG.MEMBER_NBR=AggTots.MEM

WHERE FOCUS_TELLER_ID < 4000
AND AMOUNT > 0
GROUP BY CASE WHEN FOCUS_TELLER_ID BETWEEN 101 AND 199 THEN 1 
WHEN FOCUS_TELLER_ID BETWEEN 201 AND 299 THEN 2 
WHEN FOCUS_TELLER_ID BETWEEN 301 AND 399 THEN 3 
ELSE 'OTHER' END, DATEADD(MONTH, DATEDIFF(MONTH, 0, FEE_LOG.ENTRY_DATE), 0)

有什么想法吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题