![](/img/trans.png)
[英]how to combine multiple row in same table into 1 row with different column in sql join with laravel query builder
[英]SQL Query, i have join the same table but there is same value at different row
在同一張表下,我必須找到不同字段的總和,因此我正在使用以下查詢
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列下的不同行中有兩個相同的數據。 如何合並它們?
對不起,代碼很亂,我仍在學習過程中==
中間的兩個部分將Item連接到Item,而不是Item連接到Item1。 Item1到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
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.