创建两个表父级和子级
Create table parent (ID int, ParentId int,Text varchar(20))
Create table Child (child1Id int, ParentId int, point int)
ID ParentId Text
1 NULL Sony
2 1 phone
3 2 sale
4 2 Rate
child1Id ParentIdId point
100 3 10
200 4 20
我试过这样的事情
Select sum(b.point),a.ParentId,a.Text from parent A join Child B on a.ID =b.ParentId group by a.ParentId,a.Text
我需要这样的输出。
ID ParentId Text Point
1 NULL Sony null
2 1 phone 30
答案 0 :(得分:1)
您可以使用子查询
执行此操作SELECT p.ID
, p.parentId
, p.[Text] AS 'Text'
, cld.pointSum AS 'Point'
FROM dbo.Parent AS p
LEFT JOIN (
SELECT c.ParentId
, SUM(c.point) AS 'pointSum'
FROM dbo.Child AS c
GROUP BY c.ParentId
) cld ON (p.ID = cld.ParentId)