并非每一行都显示在查询中

时间:2015-10-15 14:12:31

标签: sql-server

我有一个查询来计算我的表中的交易数量和付款数量。这些计数基于分离表中的数据。我需要按年份和一年中的一周对结果进行分组。日期也来自一张单独的表格。我在with子句中的支付和交易的并集中添加周值,然后进行计算。

WITH CHART_DATES
AS (
    SELECT DD.CAL_YEAR, DD.WEEK_OF_YEAR
    FROM DIM_DATE DD
    RIGHT JOIN FACT_PAY_PAYMENT FPP ON DD.ID = FPP.REQUESTED_EXECUTION_DATE_ID
    GROUP BY DD.CAL_YEAR, DD.WEEK_OF_YEAR

  UNION

    SELECT DD.CAL_YEAR, DD.WEEK_OF_YEAR
    FROM DIM_DATE DD
    RIGHT JOIN FACT_AS_BALANCE FAB ON DD.ID = FAB.BALANCE_DATE_ID
    GROUP BY DD.CAL_YEAR, DD.WEEK_OF_YEAR
  )
SELECT
  CD.WEEK_OF_YEAR,
  CD.CAL_YEAR,
  COALESCE(COUNT(DISTINCT FPP.ID), 0) AS PAYMENTS,
  COALESCE(COUNT(DISTINCT FAB.ACCOUNT_STATEMENT_ID), 0) AS STATEMENTS
FROM FACT_PAY_PAYMENT FPP
RIGHT JOIN DIM_DATE DD ON FPP.REQUESTED_EXECUTION_DATE_ID = DD.ID
FULL JOIN FACT_AS_BALANCE FAB ON DD.ID = FAB.BALANCE_DATE_ID
RIGHT JOIN CHART_DATES CD ON DD.CAL_YEAR = CD.CAL_YEAR
  AND DD.WEEK_OF_YEAR = CD.WEEK_OF_YEAR
INNER JOIN DIM_AS_CHARACTERISTICS DAC ON FAB.BALANCE_TYPE_ID = DAC.ID
WHERE DAC.TRANSACTION_INTRADAY_FLAG = 'N'
GROUP BY CD.CAL_YEAR, CD.WEEK_OF_YEAR

我在表格中添加了以下数据:

  

2015年第42周有1笔付款和2笔交易

     

2015年第41周有1笔付款和0笔交易

执行查询后,我收到以下结果: Wrong result

我的查询有什么问题以及如何解决?

0 个答案:

没有答案