Firebird SQL连接表

时间:2018-05-02 08:26:02

标签: sql firebird

目前我有这样的表:

表客户

ID      Name
01      Angel
02      Charlie

表输入1

Name     Value
Charlie  5
Angel    7
Charlie  10
Angel    3

表输入2

Name     Value
Charlie  10
Angel    5
Charlie  15
Angel    25

如何获得这样的结果

Name     Input1     Input2
Angel    10         30
Charlie  15         25

1 个答案:

答案 0 :(得分:2)

试试这个:

SELECT 
    c.Name, 
    i1."Value" value1, 
    i2."Value" value2
FROM 
    Customer c LEFT JOIN 
    (SELECT Name, SUM("Value") "Value" from input1 group by Name) i1
    ON c.Name = i1.Name LEFT JOIN 
    (SELECT Name, SUM("Value") "Value" from input2 group by Name) i2
    ON c.Name = i2.Name

<强>样本

  

http://sqlfiddle.com/#!9/204220/2