SQL查询,我已加入同一个表,但在不同的行有相同的值

时间:2018-02-21 04:11:55

标签: sql select join group-by sum

在同一张表下,我必须找到不同字段的总和,所以我使用以下查询

select db.*, isnull(cb."8",0) as "8", db."PUR Total" - isnull(cb."8",0) as "9", eb.Item1, eb."PO Total"
  from 
  (select b.t_dicl as Department, c.t_item as Item, sum( c.t_qstk )as "8"
  from inforlndb.dbo.twhinr1109980 AS c INNER JOIN inforlndb.dbo.ttcemm1129980 AS b ON  b.t_waid = c.t_cwar
  where c.t_koor = 2 and c.t_kost = 3 and month(c.t_trdt) <= @aMonth - 1
  group by c.t_item, b.t_dicl) as cb
full outer join
    (select b.t_dicl as Department, c.t_item as Item, sum( c.t_qstk )as "PUR Total"
    from inforlndb.dbo.twhinr1109980 AS c INNER JOIN     inforlndb.dbo.ttcemm1129980 AS b ON  b.t_waid = c.t_cwar
    where c.t_koor = 2 and c.t_kost = 3 and month(c.t_trdt) <= @aMonth
    group by c.t_item, b.t_dicl) as db
    on cb.Item = db.Item
--Production order
full outer join
    (select b.t_dicl as Department, c.t_item as Item, sum( c.t_qstk )as "8"
  from inforlndb.dbo.twhinr1109980 AS c INNER JOIN inforlndb.dbo.ttcemm1129980 AS b ON  b.t_waid = c.t_cwar
  where c.t_koor = 1 and c.t_kost = 5 and month(c.t_trdt) <= @aMonth - 1
  group by c.t_item, b.t_dicl) as ab
  on cb.Item = ab.Item
full outer join
(select b.t_dicl as Department, c.t_item as Item1, sum( c.t_qstk )as "PO Total"
    from inforlndb.dbo.twhinr1109980 AS c INNER JOIN inforlndb.dbo.ttcemm1129980 AS b ON  b.t_waid = c.t_cwar
     where c.t_koor = 1 and c.t_kost = 5 and month(c.t_trdt) <= @aMonth
    group by c.t_item, b.t_dicl) as eb
    on cb.Item = eb.Item1

示例输出

Department  Item    PUR Total   8   9       Item1       PO Total
EV           G00046301  25000   0   25000   NULL        NULL
EV           G00053001  10000   10000   0   G00053001   55
EV           G00251701  4500    4500    0   G00251701   220
TF           G01259901  200 0   200 NULL    NULL
NULL        NULL        NULL    0   NULL    NG707460AS  5
NULL        NULL        NULL    0   NULL    G00046301   72
NULL        NULL        NULL    0   NULL    G02280100   6
NULL        NULL        NULL    0   NULL    NG707460BS  5

从输出中,您可以看到列Item和Item1下的不同行有两个相同的数据。如何合并?

抱歉代码很乱,我还在学习过程中==&#34;

1 个答案:

答案 0 :(得分:0)

中间的两个部分将Item连接到Item,而不是Item连接到Item1。 Item to Item1仅发生在最终的连接中。在您的顶级选择列表中,我们有db.Item(来自第二个表)和eb.Item1(来自最后一个表)。查看项G00046301,我们看到它存在于db表中,但是为了在eb表中找到它匹配我们需要:

db.Item -> ab.Item -> eb.Item1

中间表(ab)不得有G00046301行。因为这些是完全外部联接,我们仍然会在中间链接丢失时得到结果,但最终的表格找不到匹配项。所以......

由于您的选择似乎更关注db和ab,您可以尝试直接加入它们:

select db.*, isnull(cb."8",0) as "8", db."PUR Total" - isnull(cb."8",0) as "9", eb.Item1, eb."PO Total"
  from 
  (select b.t_dicl as Department, c.t_item as Item, sum( c.t_qstk )as "8"
  from inforlndb.dbo.twhinr1109980 AS c INNER JOIN inforlndb.dbo.ttcemm1129980 AS b ON  b.t_waid = c.t_cwar
  where c.t_koor = 2 and c.t_kost = 3 and month(c.t_trdt) <= @aMonth - 1
  group by c.t_item, b.t_dicl) as cb
full outer join
    (select b.t_dicl as Department, c.t_item as Item, sum( c.t_qstk )as "Total"
    from inforlndb.dbo.twhinr1109980 AS c INNER JOIN     inforlndb.dbo.ttcemm1129980 AS b ON  b.t_waid = c.t_cwar
    where c.t_koor = 2 and c.t_kost = 3 and month(c.t_trdt) <= @aMonth
    group by c.t_item, b.t_dicl) as db
    on cb.Item = db.Item
--Production order
full outer join
    (select b.t_dicl as Department, c.t_item as Item, sum( c.t_qstk )as "8"
  from inforlndb.dbo.twhinr1109980 AS c INNER JOIN inforlndb.dbo.ttcemm1129980 AS b ON  b.t_waid = c.t_cwar
  where c.t_koor = 1 and c.t_kost = 5 and month(c.t_trdt) <= @aMonth - 1
  group by c.t_item, b.t_dicl) as ab
  on cb.Item = ab.Item
full outer join
(select b.t_dicl as Department, c.t_item as Item1, sum( c.t_qstk )as "Total"
    from inforlndb.dbo.twhinr1109980 AS c INNER JOIN inforlndb.dbo.ttcemm1129980 AS b ON  b.t_waid = c.t_cwar
     where c.t_koor = 1 and c.t_kost = 5 and month(c.t_trdt) <= @aMonth
    group by c.t_item, b.t_dicl) as eb
    on db.Item = eb.Item1

唯一的变化是最后一行

    on db.Item = eb.Item1

我希望这会有所帮助。