Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在sql server中使用两个表求和点?_Sql Server - Fatal编程技术网

Sql server 如何在sql server中使用两个表求和点?

Sql server 如何在sql server中使用两个表求和点?,sql-server,Sql Server,创建两个表父表和子表 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 ch

创建两个表父表和子表

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

您可以通过子查询来实现这一点

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)

一些样本数据怎么样?