【发布时间】:2017-09-20 12:21:05
【问题描述】:
我有一张桌子:发票
inv_id cus_id due_amt paid total_due
1 71 300 0 300
2 71 200 0 500
3 71 NULL 125 375
4 72 50 0 50
5 72 150 0 200
我想要结果
cus_id total_due
71 375
72 200
那是我想要unique customer 的total_due 或者说我需要unique customer 的latest invoice 详细信息。
我尝试了什么:
SELECT cus_id, total_due FROM invoice GROUP BY cus_id ORDER BY inv_id DESC
但这并没有给出所需的结果。
请有人可以帮助我..
【问题讨论】:
-
试试这个: where inv_id=(select max(inv_id) from table_a as a inner join table_a as b on A.cus_id = B.cus_id)。查找每个 cus_id 的最大 inv_id 的 total_due 是一个自联接。