很有趣的一个SQL语法问题!!!!!

我现在有两个表
表t1:
ID Name
f1 AAA
f2 BBB
f3 CCC
f4 DDD
表t2:
ID DanWeiID Amout
f1 8601 10
f2 8601 20
f1 8602 30
f2 8603 40
f3 8605 50

我想要用一条SQL语句实现下面得表:
ID Name Amout
f1 AAA 40 //10+30
f2 BBB 60 //20+40
f3 CCC 50 //50
f4 DDD 0

有兴趣的人请一起参与

---------------------------------------------------------------

Select b.ID,a.Name,COALESCE(b.Amout,0) As Amout
From Table1 a LEFT JOIN
(
Select ID,SUM(Amout) As Amout
From Table2
Group by ID
) AS b
ON a.ID=b.ID

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus