简体   繁体   中英

How to set UNION inside UNION ALL?

I'm trying to execute this code :

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

But I got error :

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

So, I guess that either I am using wrong syntax or I am trying to do something that can not be done. Maybe most likely experienced people will see right away what is wrong.

Can someone help me, please ? Thanks

Because you have Quantity1 Quantity2 as columns not Qty

Maybe try this

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

You have more problems than just wrong aliasses:

This is the correct code(code without errors):

    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';
  • I have replaced Qty, Qty2 and Qty3 with 'Quantity 1', 'Quantity 2', 'Quantity 3'
  • Also replaced ON A2.ItCode = B3.ItCode with ON A3.ItCode = B3.ItCode where you can see that your table alias is wrong for this query.
  • I have added a coulmn ItName in every group by.

Here is my demo: DEMO

For three different A.ItCode you don't need three different select query to combine with union all, rather you can use in clause to get three different products with single select query.

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'

I've tried this and it worked just like I want

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'

But I think this code can be shorter. But I don't know how to short it. Any advice ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM