如何在 UNION ALL 中设置 UNION?

Posted

技术标签:

【中文标题】如何在 UNION ALL 中设置 UNION?【英文标题】:How to set UNION inside UNION ALL? 【发布时间】:2021-06-24 08:21:06 【问题描述】:

我正在尝试执行此代码:

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 错误(1054):字段列表中的未知列“数量”

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

有人可以帮帮我吗?谢谢

【问题讨论】:

将别名“数量 1”、“数量 2”和“数量 3”更改为 Qty、Qt2 和 Qt3。 我感觉这个查询可以短得多..短得多。 @FaNo_FN 我也是这么想的,但我不知道怎么做。也许你能给我一些建议? 如果您有表格和样本数据供我处理,我可以。理想的结果也是好的。您可以edit your question 将它们包含在内。 【参考方案1】:

对于三个不同的A.ItCode,您不需要三个不同的选择查询来结合union all,而是可以使用in 子句通过单个选择查询获得三个不同的产品。

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'

【讨论】:

【参考方案2】:

因为您有 Quantity1 Quantity2 作为列而不是数量

不妨试试这个

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';

【讨论】:

这段代码仍然有同样的错误,但这次错误是字段列表中的未知'Qty2'。不过感谢您的帮助,谢谢【参考方案3】:

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

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

    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,您可以在其中看到您的表别名是 此查询错误。 我在每个组中添加了一个ItName 由。

这是我的演示:DEMO

【讨论】:

不应该将外部查询中的'Quantity 1', 'Quantity 2', 'Quantity 3' 包含在反引号而不是撇号中吗? 没有相同的错误,但这次在 Quantity 1 ,2 和 3 列中。它返回 'Quantity 1', Quantity 2, and Quantity 3 for the 1st and 2nd row 那是因为撇号。引用的别名应该用反引号包裹。 Refer this example 我已经更正了我的答案和演示中的代码。感谢@FaNo_FN 的更正。干杯!【参考方案4】:

我已经尝试过了,它就像我想要的那样工作

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'

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

【讨论】:

您可以对这三种产品使用一个选择查询。请检查我的答案。

以上是关于如何在 UNION ALL 中设置 UNION?的主要内容,如果未能解决你的问题,请参考以下文章

order by 和union all 如何共存

如何知道为 union 设置了哪个变量值?

oracle中怎么将union all的字段值加在一起

SQL union all 排序问题

union 与 union all 的表现

SQL 中的 UNION 和UNION ALL 有啥区别?