如何在 UNION ALL 中设置 UNION?

How to set UNION inside UNION ALL?

我正在尝试执行这段代码:

SELECT ItName, ItCode, Qty, Qt2, Qt3
FROM
(
SELECT A.ItName, A.ItCode, COUNT(B.Qty) AS 'Quantity 1', COUNT(B.Qty2) AS 'Quantity 2', 
COUNT(B.Qty3) AS 'Quantity 3' 
FROM tblitem A
INNER JOIN tblstocksummary B ON A.ItCode = B.ItCode
WHERE A.Itcode='IL001-000151'
GROUP BY A.ItCode
HAVING COUNT(B.Qty) !='0'AND COUNT(B.Qty2) !='0' AND COUNT(B.Qty3) !='0'

UNION

SELECT A2.ItName, A2.ItCode, COUNT(B2.Qty), COUNT(B2.Qty2), COUNT(B2.Qty3) FROM tblitem A2
INNER JOIN tblstocksummary B2 ON A2.ItCode = B2.ItCode
WHERE A2.Itcode='IL001-000373' 
GROUP BY A2.ItCode
HAVING COUNT(B2.Qty) !='0'AND COUNT(B2.Qty2) !='0' AND COUNT(B2.Qty3) !='0'
) t
UNION ALL

SELECT A3.ItName, A3.ItCode, COUNT(B3.Qty), COUNT(B3.Qty2), COUNT(B3.Qty3) FROM tblitem A3
INNER JOIN tblstocksummary B3 ON A2.ItCode = B3.ItCode
WHERE A3.Itcode='IL001-000166' 
GROUP BY A3.ItCode
HAVING COUNT(B3.Qty) !='0'AND COUNT(B3.Qty2) !='0' AND COUNT(B3.Qty3) !='0';

但是我得到了错误:

SQL Error(1054): Unknown column 'Qty' in field list

所以,我想要么我使用了错误的语法,要么我正在尝试做一些无法完成的事情。也许最有经验的人会马上看出问题所在。

有人可以帮助我吗?谢谢

因为您有 Quantity1 Quantity2 作为列而不是 Qty

也许试试这个

SELECT ItName, ItCode, Qty, Qt2, Qt3
FROM
(
SELECT A.ItName, A.ItCode, COUNT(B.Qty) AS 'Qty', COUNT(B.Qty2) AS 'Qty2', 
COUNT(B.Qty3) AS 'Qty3' 
FROM tblitem A
INNER JOIN tblstocksummary B ON A.ItCode = B.ItCode
WHERE A.Itcode='IL001-000151'
GROUP BY A.ItCode
HAVING COUNT(B.Qty) !='0'AND COUNT(B.Qty2) !='0' AND COUNT(B.Qty3) !='0'

UNION

SELECT A2.ItName, A2.ItCode, COUNT(B2.Qty), COUNT(B2.Qty2), COUNT(B2.Qty3) FROM tblitem A2
INNER JOIN tblstocksummary B2 ON A2.ItCode = B2.ItCode
WHERE A2.Itcode='IL001-000373' 
GROUP BY A2.ItCode
HAVING COUNT(B2.Qty) !='0'AND COUNT(B2.Qty2) !='0' AND COUNT(B2.Qty3) !='0'
) t
UNION ALL

SELECT A3.ItName, A3.ItCode, COUNT(B3.Qty), COUNT(B3.Qty2), COUNT(B3.Qty3) FROM tblitem A3
INNER JOIN tblstocksummary B3 ON A2.ItCode = B3.ItCode
WHERE A3.Itcode='IL001-000166' 
GROUP BY A3.ItCode
HAVING COUNT(B3.Qty) !='0'AND COUNT(B3.Qty2) !='0' AND COUNT(B3.Qty3) !='0';

你的问题不仅仅是错误的别名:

这是正确的代码(没有错误的代码):

    SELECT ItName, ItCode, `Quantity 1`, `Quantity 2`, `Quantity 3`
FROM
(
SELECT A.ItName, A.ItCode, COUNT(B.Qty) AS 'Quantity 1', COUNT(B.Qty2) AS 'Quantity 2', 
COUNT(B.Qty3) AS 'Quantity 3' 
FROM tblitem A
INNER JOIN tblstocksummary B ON A.ItCode = B.ItCode
WHERE A.Itcode='IL001-000151'
GROUP BY  A.ItName, A.ItCode
HAVING COUNT(B.Qty) !='0'AND COUNT(B.Qty2) !='0' AND COUNT(B.Qty3) !='0'

UNION

SELECT A2.ItName, A2.ItCode, COUNT(B2.Qty), COUNT(B2.Qty2), COUNT(B2.Qty3) 
FROM tblitem A2
INNER JOIN tblstocksummary B2 ON A2.ItCode = B2.ItCode
WHERE A2.Itcode='IL001-000373' 
GROUP BY A2.ItName, A2.ItCode
HAVING COUNT(B2.Qty) !='0'AND COUNT(B2.Qty2) !='0' AND COUNT(B2.Qty3) !='0'
) t
UNION ALL

SELECT A3.ItName, A3.ItCode, COUNT(B3.Qty), COUNT(B3.Qty2), COUNT(B3.Qty3) FROM tblitem A3
INNER JOIN tblstocksummary B3 ON A3.ItCode = B3.ItCode
WHERE A3.Itcode='IL001-000166' 
GROUP BY A3.ItName, A3.ItCode
HAVING COUNT(B3.Qty) !='0'AND COUNT(B3.Qty2) !='0' AND COUNT(B3.Qty3) !='0';
  • 我已将 Qty, Qty2 and Qty3 替换为 'Quantity 1', 'Quantity 2', 'Quantity 3'
  • 还将 ON A2.ItCode = B3.ItCode 替换为 ON A3.ItCode = B3.ItCode,您可以在其中看到您的 table 别名是 此查询错误。
  • 我在每组都加了一个ItName 通过.

这是我的演示:DEMO

我已经试过了,效果如我所愿

SELECT A.ItCode 'Item Code', A.ItName 'Item Name', COUNT(B.Qty) 'Quantity', COUNT(B.Qty2) 'Quantity2', COUNT(B.Qty3) 'Quantity3'
FROM tblitem A
INNER JOIN tblstocksummary B ON A.ItCode = B.ItCode AND A.ItCode = 'IL001-000151'
GROUP BY A.ItCode 
HAVING 'Quantity' !='0' AND 'Quantity2' !='0' AND 'Quantity3' !='0'

UNION ALL 

SELECT A.ItCode, A.ItName, COUNT(B.Qty) 'Quantity', COUNT(B.Qty2) 'Quantity2', COUNT(B.Qty3) 'Quantity3'
FROM tblitem A
INNER JOIN tblstocksummary B ON A.ItCode = B.ItCode AND A.ItCode = 'IL001-000373'
GROUP BY A.ItCode 
HAVING 'Quantity' !='0' AND 'Quantity2' !='0' AND 'Quantity3' !='0'

UNION ALL

SELECT A.ItCode, A.ItName, COUNT(B.Qty) 'Quantity', COUNT(B.Qty2) 'Quantity2', COUNT(B.Qty3) 'Quantity3'
FROM tblitem A
INNER JOIN tblstocksummary B ON A.ItCode = B.ItCode AND A.ItCode = 'IL001-000166'
GROUP BY A.ItCode 
HAVING 'Quantity' !='0' AND 'Quantity2' !='0' AND 'Quantity3' !='0'

但我认为这段代码可以更短。但我不知道如何缩短它。有什么建议吗?

对于三个不同的 A.ItCode,您不需要三个不同的 select 查询来与 union all 组合,而是可以使用 in 子句通过单个 select查询。

SELECT A.ItCode, A.ItName, COUNT(B.Qty) 'Quantity', COUNT(B.Qty2) 'Quantity2', COUNT(B.Qty3) 'Quantity3'
FROM tblitem A
INNER JOIN tblstocksummary B ON A.ItCode = B.ItCode AND A.ItCode in ('IL001-000151','IL001-000373', 'IL001-000166')
GROUP BY A.ItCode 
HAVING 'Quantity' !='0' AND 'Quantity2' !='0' AND 'Quantity3' !='0'