SELECT 在 SQL 服务器中显示 OnHand、Sale 和 Purchase 表中具有 either/or Qty 字段不为空的所有不同行
SELECT in SQL Server to show all distinct rows from the tables OnHand, Sale and Purchase that have either/or Qty field not empty
我需要在 SQL 服务器中编写一个 SELECT
查询,该服务器使用 JOIN
或 UNION
选择不同的 ItmNo
或 Code
来自 3 个表的行 OnHand
、Sale
和 Purchase
.
这是我拥有的表格和我需要的表格的详细信息。 ItmNo
and/or Code
列可以用作外键来连接表。
这些是我的输入表-
Table 手头
ID ItmNo Code Qty
----------------------------------
1 I001 001 100
2 I001 001 50
3 I003 003 300
Table 促销
ID ItmNo Code Qty
----------------------------------
1 I001 001 100
2 I004 004
3 I003 003 120
Table 购买
ID ItmNo Code Qty
----------------------------------
1 I005 005 10
2 I003 003 200
3 I003 003 300
这就是我需要的输出。此处应仅显示 DISTINCT ItmNo
和 Code
:
ID ItmNo Code SumQtyOnHand SumQtyOnSale SumQtyOnPurchase
------------------------------------------------------------------------------
1 I001 001 150 100
2 I003 003 300 120 500
3 I005 005 10
这是我在下面尝试过的 SELECT
查询,但我无法获得我想要的输出-
SELECT
A.ItmNo, A.Code,
A2.TOTAL SumQtyOnHand,
B.TOTAL SumQtyOnSale,
C.TOTAL SumQtyOnPurchase
FROM
dbo.OnHand A
LEFT JOIN
(SELECT ItmNo, Code, SUM(Qty) TOTAL
FROM dbo.OnHand
GROUP BY ItmNo, Code) A2 ON A.ItmNo = A2.ItmNo
LEFT JOIN
(SELECT ItmNo, Code, SUM(Qty) TOTAL
FROM dbo.Sale
GROUP BY ItmNo, Code) B ON A.ItmNo = A2.ItmNo
LEFT JOIN
(SELECT ItmNo, Code, SUM(Qty) TOTAL
FROM dbo.Purchase
GROUP BY ItmNo, Code) C ON A.ItmNo = A2.ItmNo
请在 SELECT
查询中提出更正建议以实现上述输出。
提前致谢!
我认为您在预聚合子查询方面走在正确的轨道上。然后,你可以full join
。 SQL服务器语法有点繁琐,不支持using()
子句:
select
coalesce(o.itmno, s.itemno, p.itemno) as itemno,
coalesce(o.code, s.code, p.code) as code,
o.SumQtyOnHand,
s.SumQtyOnSale,
p.SumQtyOnPurchase
from (
select itmno, code, sum(qty) SumQtyOnHand
from dbo.onhand
group by itmno, code
) o
full join (
select itmno, code, sum(qty) SumQtyOnSale
from dbo.sale
group by itmno, code
) s on s.itmno = o.itmno and s.code = o.code
full join (
select itmno, code, sum(qty) SumQtyOnPurchase
from dbo.purchase
group by itmno, code
) p on p.itemno = coalesce(s.itemno, o.itemno) and p.code = coalesce(s.code, o.code)
用union all
和聚合表示可能更简单:
select itemno, code,
sum(qtyOnHand) as SumQtyOnHand,
sum(qtyOnSale) as SumQtyOnSale,
sum(qtyOnPurchase) as SumQtyOnPurchase
from (
select itemno, code, qty as qtyOnHand, null as qtyOnSale, null as qtyOnPurchase from dbo.onhand
union all select itemno, code, null, qty, null from dbo.sale
union all select itemno, code, null, null, qty from dbo.purchase
) t
group by itemno, code
我需要在 SQL 服务器中编写一个 SELECT
查询,该服务器使用 JOIN
或 UNION
选择不同的 ItmNo
或 Code
来自 3 个表的行 OnHand
、Sale
和 Purchase
.
这是我拥有的表格和我需要的表格的详细信息。 ItmNo
and/or Code
列可以用作外键来连接表。
这些是我的输入表-
Table 手头
ID ItmNo Code Qty
----------------------------------
1 I001 001 100
2 I001 001 50
3 I003 003 300
Table 促销
ID ItmNo Code Qty
----------------------------------
1 I001 001 100
2 I004 004
3 I003 003 120
Table 购买
ID ItmNo Code Qty
----------------------------------
1 I005 005 10
2 I003 003 200
3 I003 003 300
这就是我需要的输出。此处应仅显示 DISTINCT ItmNo
和 Code
:
ID ItmNo Code SumQtyOnHand SumQtyOnSale SumQtyOnPurchase
------------------------------------------------------------------------------
1 I001 001 150 100
2 I003 003 300 120 500
3 I005 005 10
这是我在下面尝试过的 SELECT
查询,但我无法获得我想要的输出-
SELECT
A.ItmNo, A.Code,
A2.TOTAL SumQtyOnHand,
B.TOTAL SumQtyOnSale,
C.TOTAL SumQtyOnPurchase
FROM
dbo.OnHand A
LEFT JOIN
(SELECT ItmNo, Code, SUM(Qty) TOTAL
FROM dbo.OnHand
GROUP BY ItmNo, Code) A2 ON A.ItmNo = A2.ItmNo
LEFT JOIN
(SELECT ItmNo, Code, SUM(Qty) TOTAL
FROM dbo.Sale
GROUP BY ItmNo, Code) B ON A.ItmNo = A2.ItmNo
LEFT JOIN
(SELECT ItmNo, Code, SUM(Qty) TOTAL
FROM dbo.Purchase
GROUP BY ItmNo, Code) C ON A.ItmNo = A2.ItmNo
请在 SELECT
查询中提出更正建议以实现上述输出。
提前致谢!
我认为您在预聚合子查询方面走在正确的轨道上。然后,你可以full join
。 SQL服务器语法有点繁琐,不支持using()
子句:
select
coalesce(o.itmno, s.itemno, p.itemno) as itemno,
coalesce(o.code, s.code, p.code) as code,
o.SumQtyOnHand,
s.SumQtyOnSale,
p.SumQtyOnPurchase
from (
select itmno, code, sum(qty) SumQtyOnHand
from dbo.onhand
group by itmno, code
) o
full join (
select itmno, code, sum(qty) SumQtyOnSale
from dbo.sale
group by itmno, code
) s on s.itmno = o.itmno and s.code = o.code
full join (
select itmno, code, sum(qty) SumQtyOnPurchase
from dbo.purchase
group by itmno, code
) p on p.itemno = coalesce(s.itemno, o.itemno) and p.code = coalesce(s.code, o.code)
用union all
和聚合表示可能更简单:
select itemno, code,
sum(qtyOnHand) as SumQtyOnHand,
sum(qtyOnSale) as SumQtyOnSale,
sum(qtyOnPurchase) as SumQtyOnPurchase
from (
select itemno, code, qty as qtyOnHand, null as qtyOnSale, null as qtyOnPurchase from dbo.onhand
union all select itemno, code, null, qty, null from dbo.sale
union all select itemno, code, null, null, qty from dbo.purchase
) t
group by itemno, code