运行计数显示所有值而不是值的总数

时间:2016-12-19 15:04:56

标签: sql amazon-redshift

我的数据存储在Amazon Redshift数据库中。我试图按月计算贷款。这是我的问题:

SocketError: Failed to open TCP connection to localhost:3000/children-centre/london/greenwich/eltham-childrens-centre/join:3000 (getaddrinfo: nodename nor servname provided, or not known)

我的预期结果如下:

SELECT 
TO_CHAR(LD.INITIAL_PURCHASE_DATE,'YYYY-MM') AS INITIAL_PURCHASE,
COUNT( LD.LOAN_ID) OVER (ORDER  BY TO_CHAR(LD.INITIAL_PURCHASE_DATE,'YYYY-MM') ROWS UNBOUNDED PRECEDING ) AS TOTAL_LOANS
FROM LOANS_DETAILS
INNER JOIN LOANS L ON LD.LOAN_ID = L.ID
WHERE L.UNDERWRITING_STATUS IN ('...')
 AND LD.INITIAL_PURCHASE_DATE IS NOT NULL
 GROUP BY
 LD.LOAN_ID, 
 LD.INITIAL_PURCHASE_DATE;

但相反,我会在每个月的每一天获得一条记录

INITIAL_PURCHASE|TOTAL_LOANS
...|...
2016-10|369
2016-11|424

我检查了源系统并确认10月份共有369笔贷款,11月份有424笔,所以我知道数据是正确的。

我如何获得每月的贷款总数?

解: 这是正确的查询。

INITIAL_PURCHASE|TOTAL_LOANS
...|...
2016-10|366
2016-10|367
2016-10|368
2016-10|369
2016-11|371

2 个答案:

答案 0 :(得分:2)

您的group by需要按月而不是按天,您需要从LOAN_ID中删除GROUP BY

SELECT TO_CHAR(LD.INITIAL_PURCHASE_DATE, 'YYYY-MM') AS INITIAL_PURCHASE,
       SUM(COUNT( LD.LOAN_ID)) OVER (ORDER  BY TO_CHAR(LD.INITIAL_PURCHASE_DATE,'YYYY-MM') ROWS UNBOUNDED PRECEDING ) AS TOTAL_LOANS
FROM LOANS_DETAILS LD INNER JOIN
     LOANS L
     ON LD.LOAN_ID = L.ID
WHERE L.UNDERWRITING_STATUS IN ('...') AND
      LD.INITIAL_PURCHASE_DATE IS NOT 
GROUP BY TO_CHAR(LD.INITIAL_PURCHASE_DATE, 'YYYY-MM')

注意:

  • 我认为Amazon Redshift允许GROUP BY中的别名,因此您可以使用GROUP BY INITIAL_PURPOSE, LD.LOAN_ID
  • SUM(COUNT(*))应该为您提供运行金额。
  • 如果您希望按月计算总数,则
  • LOAN_ID不应位于GROUP BY

答案 1 :(得分:0)

这就是你的目标。
您按INITIAL_PURCHASE(' YYYY-MM')进行分组,并按计数(*)执行总计。

SELECT      TO_CHAR(LD.INITIAL_PURCHASE_DATE,'YYYY-MM') AS INITIAL_PURCHASE

           ,sum(count(*)) OVER 
                 (ORDER BY TO_CHAR(LD.INITIAL_PURCHASE_DATE,'YYYY-MM')
                       ROWS UNBOUNDED PRECEDING ) AS TOTAL_LOANS

FROM                    LOANS_DETAILS   LD
            INNER JOIN  LOANS           L 
            ON          LD.LOAN_ID = L.ID

WHERE       L.UNDERWRITING_STATUS IN ('...')
        AND LD.INITIAL_PURCHASE_DATE IS NOT NULL

GROUP BY    INITIAL_PURCHASE

P.S。
我认为别名INITIAL_PURCHASE应该在GROUP BY子句中被识别,如果我弄错了,那么使用TO_CHAR(LD.INITIAL_PURCHASE_DATE,'YYYY-MM')