我需要您的帮助,提出一个查询,该查询会给我付款时效报告。简单数据如下所示:
---------------------------------------------------------------------------------
| ID | Customer Name | Total Invoice | Paid Invoice | Paid Date | Amount | Aging
---------------------------------------------------------------------------------
| 1 | David | $100 | $0 |10/10/2018 | $100 | 1
| 2 | David | $100 | $80 |15/10/2018 | $20 | 5
| 3 | David | $100 | $100 |18/10/2018 | $0 | 8
| 4 | Jonh | $250 | $0 |18/10/2018 | $100 | 0
---------------------------------------------------------------------------------
| ID | Customer Name | Total Invoice | Paid Invoice | Paid Date | Amount | Aging
---------------------------------------------------------------------------------
| 1 | David | $100 | $100 |18/10/2018 | $0 | 8
| 2 | Jonh | $250 | $ 0 |18/10/2018 | $250 | 0
答案 0 :(得分:0)
您需要每个客户的最后一行。一种完善的方法是使用row_number
Windows函数:
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_name ORDER BY aging DESC) AS rn
FROM mytable) t
WHERE rn = 1
答案 1 :(得分:0)
尝试
SELECT *, LAST_VALUE(Aging) OVER (PARTITION BY customer_name ORDER BY aging DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
FROM mytable