在带join的sql中,对两个独立表的信息求和的最佳方法是什么?

mhd8tkvw  于 2021-07-26  发布在  Java
关注(0)|答案(5)|浏览(298)

我有两张表,结构如下:

CREATE TABLE COST1 (
    ID,
    COUNTER,
    COST
)

CREATE TABLE COST2 (
    ID,
    COUNTER,
    COST
)
``` `ID` 可用于 `JOIN` ; 而当 `COUNTER` 以及 `COST` 在两个表中都有相同的名称它们之间没有这样的关系 `ID` 是。我想创建一个结果集 `COST3` 其形式如下:

ID, sum(COST1.cost) + sum(COST2.cost).

这是我想出来的,但是我不知道是否用一个 `GROUP BY` 这是由于 `JOIN` 会像我想的那样工作吗?

SELECT
ID,
( sum(c1.COST) + sum(c2.COST) ) as COST_TOTAL
FROM
COST1 c1
JOIN COST2 c2 ON c1.ID = c2.ID
GROUP BY
ID;

根据一些数据,结果如下:
成本1
idcountercosta150a230b125b230公司
成本2:
idcountercosta120a240b150b210b320公司
成本3:
艾德科斯塔,140b,135
3lxsmp7m

3lxsmp7m1#

create table COST1(ID varchar(5), Counter int,Cost int);
 insert into COST1 values('A', 1, 50  );
 insert into COST1 values('A', 2, 30  );
 insert into COST1 values('B', 1, 25  );
 insert into COST1 values('B', 2, 30  );
 insert into COST1 values('C', 2, 30  );

 create table COST2(ID varchar(5), Counter int,Cost int);   
 insert into COST2 values('A', 1, 20  );
 insert into COST2 values('A', 2, 40  );
 insert into COST2 values('B', 1, 50  );
 insert into COST2 values('B', 2, 10  );
 insert into COST2 values('B', 3, 20  );
 insert into COST2 values('D', 3, 20  );

查询#1(获取两个表中可用的所有ID的总和)

Select C1.ID, (C1.cost+C2.cost) as Cost
 from
     (select ID, sum(COST) cost from COST1
 group by ID) C1

 Inner join
     (select ID, sum(COST) cost from COST2
 group by ID) C2

 on C1.ID=C2.ID
 GO

输出:
IDA140B135成本
查询#2(获取任何表中可用的所有ID的总和)

select ID, sum(cost) Cost
 from (
     (select id, cost
        from cost1
     ) union all
     (select id, cost
      from cost2
     )
     ) Cost3
 group by id;
 GO

输出:
idcosta140b135c30d20成本
db<在这里摆弄

fkaflof6

fkaflof62#

考虑执行非唯一的 ID 内部查询中的行:

SELECT
    COALESCE( c1.group_id, c2.group_id ) AS id,
    ( COALESCE( c1.group_cost, 0 ) + COALESCE( c2.group_cost, 0 ) ) AS total_cost
FROM
    (
        SELECT
            cost1.id AS group_id,
            SUM( cost1.cost ) AS group_cost
        FROM
            cost1
        GROUP BY
            cost1.id
    ) AS c1
    FULL OUTER JOIN
    (
        SELECT
            cost2.id AS group_id,
            SUM( cost2.cost ) AS group_cost
        FROM
            cost2
        GROUP BY
            cost2.id
    ) AS c2
        ON c1.group_id = c2.group_id
)
ORDER BY
    id
toe95027

toe950273#

在和的并集上使用和:

select id, sum(s) COST_TOTAL
from (
  select id, sum(cost) s
  from cost1
  group by id
  union all
  select id, sum(cost)
  from cost2
  group by id
) u
group by id

请注意,您需要的是 union 保留重复行。

hi3rlvi2

hi3rlvi24#

我倾向于使用 union all 在子查询中,然后进行聚合:

select id, sum(cost)
from ((select id, cost
       from cost1
      ) union all
      (select id, cost
       from cost2
      )
     ) ic
group by id;
7nbnzgx9

7nbnzgx95#

联合所有两个表,然后按子查询的id分组。使用在两个表之间划分特定列值的情况。

create table #COST1(ID varchar(5), Counter int,Cost int);
  insert into #COST1 values('A', 1, 50  );
  insert into #COST1 values('A', 2, 30  );
  insert into #COST1 values('B', 1, 25  );
  insert into #COST1 values('B', 2, 30  );
  insert into #COST1 values('C', 2, 30  );

  create table #COST2(ID varchar(5), Counter int,Cost int);   
  insert into #COST2 values('A', 1, 20  );
  insert into #COST2 values('A', 2, 40  );
  insert into #COST2 values('B', 1, 50  );
  insert into #COST2 values('B', 2, 10  );
  insert into #COST2 values('B', 3, 20  );
  insert into #COST2 values('D', 3, 20  );

   SELECT ID, 
   Sum(case when Type='Cost1' then Cost else 0 end+case when Type='Cost2' then Cost       else 0 end) Cost, 
   Sum(Counter) Counter
   FROM
   (
   SELECT *,'Cost1' Type FROM #COST1
   UNION ALL
   SELECT *,'Cost2' Type FROM #COST2
   )x
   group by ID

   drop table #COST1
   drop table #COST2

输出

ID  Cost    Counter
A   140     6
B   135     9
C   30      2
D   20      3

相关问题