【问题标题】:SQL Query for finding the sum from two tables用于从两个表中查找总和的 SQL 查询
【发布时间】:2014-10-10 12:50:03
【问题描述】:

我有两个表都有用于识别相应行的序列 ID。但是一个表具有一对多形式的数据输入,而另一个表具有一对一形式的数据。我需要找到与特定序列 ID 对应的值的总和。

桌子是这样的

table1

seq_id      amt1      amt2
  222        0         100 
  223        0         200
  224       300         0

table2

seq_id     code       amt3
 222        001        100
 222        002        150
 223        001        100

我需要从两个表中找到对应id的amt1+amt2+amt3之和

即输出可能看起来像

总金额
850

【问题讨论】:

  • 编辑你的标签,这些是不同的数据库。
  • 850从何而来? seq_id = 222 的总和是 450,seq_id = 223 的总和是 300,seq_id 224 的总和是 300

标签: sql


【解决方案1】:

这将显示 table1 中显示的所有 seq_id 值的总和

select t1.seq_id, sum(t1.amt1 + t1.amt2 + coalesce(t2.amt3)) as total_amount
from table1 t1
  left join table2 t2 on t1.seq_id = t2.seq_id
group by t1.seq_id;

如果 table2 中的 seq_id 值 not 在 table1 中,则需要完全外连接。在这种情况下,您还需要处理 t1 中的空值:

select t1.seq_id, sum(coalesce(t1.amt1,0) + coalesce(t1.amt2,0) + coalesce(t2.amt3)) as total_amount
from table1 t1
  full join table2 t2 on t1.seq_id = t2.seq_id
group by t1.seq_id;

如果您想将所有值单独与它们的 seq_id 相加,则使用联合:

select sum(amount) as total_amount
from (
   select amt1 + amt2 as amount
   from table1
   union all
   select amt3 as amount
   from table2
) t

【讨论】:

    【解决方案2】:

    几个嵌套查询应该可以解决问题:

    SELECT a.seq_id, a_sum + b_sum AS total_sum
    FROM   (SELECT   seq_id, amt1 + amt2 AS a_sum
            FROM     table1) a -- no GROUP BY, as seq_id is unique in this table
    JOIN   (SELECT   seq_id, SUM(amt3) AS b_sum
            FROM     table2
            GROUP BY seq_id) b ON a.seq_id = b.seq_id
    

    【讨论】:

      【解决方案3】:
      declare @table1 table (seq_id INT, amt1 decimal(18,2), amt2 decimal(18,2))
      declare @table2 table (seq_id INT, code INT, amt3 decimal(18,2))
      insert into @table1(seq_id, amt1, amt2)
      values
      (222 , 0 , 100),
      (223 , 0 , 200),
      (224 , 300, 0)
      
      insert into @table2(seq_id, code, amt3)
      values
      (222 , 001 , 100),
      (222 , 002 , 150),
      (223 , 001 , 100)
      
      select distinct
         t1.seq_id,
         t1.amt1 + t1.amt2 + t2.amt3
      from @table1 t1
         join @table2 t2 on t2.seq_id = t1.seq_id
      
      select SUM(t1.amt1) + SUM(t1.amt2) + SUM(t2.amt3)
      from @table1 t1
         join @table2 t2 on t2.seq_id = t1.seq_id
      

      【讨论】:

        【解决方案4】:

        JOIN试试下面的查询

        SELECT IFNULL(a.amt1,0) + IFNULL(a.amt2,0) + IFNULL(b.amt3,0) as total_amount 
        FROM table1 a JOIN table2 b 
        ON a.seq_id=b.seq_id
        

        【讨论】:

          【解决方案5】:

          使用这个

          select table1.seq_id, sum(table1.amt1 + table1.amt2 + coalesce(table2.amt3)) as total_amount
          from table1
            left join table2 on table1.seq_id = table2.seq_id
          group by table1.seq_id;
          

          【讨论】:

            【解决方案6】:

            我想的更简单

            SELECT 
            (SELECT SUM(atm1) FROM table1)+
            (SELECT SUM(amt2) FROM table1)+
            (SELECT SUM(amt3) FROM table2)
            AS remain
            

            【讨论】:

              猜你喜欢
              • 2021-01-26
              • 1970-01-01
              • 2015-11-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-10-18
              • 1970-01-01
              • 2020-07-15
              相关资源
              最近更新 更多