Union Query

K

Keke Lee

I have 2 queries to return below results:

resultABC:[codeABC], [qtyABC]
resultDEF:[codeDEF], [qtyDEF]

And I want to create a new query to show above results in below columns:

Code:
, [qtyABC], [qtyDEF]

whereAs [code] is a Union Select query to combine [codeABC] & [codeDEF] and
to take out the duplicates.

I am only able to create a SQL to query their combined results like:

SELECT [codeABC] AS [code], [qtyABC] AS [qty]
FROM [resultABC]
UNION SELECT [codeDEF] AS [code], [qtyDEF] AS [qty]
FROM [resultDEF]

whereas it returns combined value of [code] & [qty].

How can I rewrite the query that I can show the results
([code],[qtyABC],[qtyDEF]) I want?

Thanks

Keke
 
K

Klatuu

If both of your queries are based on the same table, there is another way:
SELECT
Code:
, Sum(IIf([code] = "ABC",[qty],0)) AS qtyABC, Sum(IIf([code] =
"ABC",[qty],0)) as qtyDEF FROM MyTable GROUP BY [code];
 
K

Keke Lee

No, my Union Query is to return values from other 2 query tables


Klatuu said:
If both of your queries are based on the same table, there is another way:
SELECT
Code:
, Sum(IIf([code] = "ABC",[qty],0)) AS qtyABC, Sum(IIf([code] =
"ABC",[qty],0)) as qtyDEF FROM MyTable GROUP BY [code];

--
Dave Hargis, Microsoft Access MVP


[QUOTE="Keke Lee"]
I have 2 queries to return below results:

resultABC:[codeABC], [qtyABC]
resultDEF:[codeDEF], [qtyDEF]

And I want to create a new query to show above results in below columns:

[code], [qtyABC], [qtyDEF]

whereAs [code] is a Union Select query to combine [codeABC] & [codeDEF] and
to take out the duplicates.

I am only able to create a SQL to query their combined results like:

SELECT [codeABC] AS [code], [qtyABC] AS [qty]
FROM [resultABC]
UNION SELECT [codeDEF] AS [code], [qtyDEF] AS [qty]
FROM [resultDEF]

whereas it returns combined value of [code] & [qty].

How can I rewrite the query that I can show the results
([code],[qtyABC],[qtyDEF]) I want?

Thanks

Keke
[/QUOTE][/QUOTE]
 
K

Klatuu

SELECT codeABC, qtYABC, 0 AS qtyDEF FROM resultABC UNION SELECT codeDEF, 0
As qtyABC, qtyDEF FROM resultDEF;
--
Dave Hargis, Microsoft Access MVP


Keke Lee said:
No, my Union Query is to return values from other 2 query tables


Klatuu said:
If both of your queries are based on the same table, there is another way:
SELECT
Code:
, Sum(IIf([code] = "ABC",[qty],0)) AS qtyABC, Sum(IIf([code] =
"ABC",[qty],0)) as qtyDEF FROM MyTable GROUP BY [code];

--
Dave Hargis, Microsoft Access MVP


[QUOTE="Keke Lee"]
I have 2 queries to return below results:

resultABC:[codeABC], [qtyABC]
resultDEF:[codeDEF], [qtyDEF]

And I want to create a new query to show above results in below columns:

[code], [qtyABC], [qtyDEF]

whereAs [code] is a Union Select query to combine [codeABC] & [codeDEF] and
to take out the duplicates.

I am only able to create a SQL to query their combined results like:

SELECT [codeABC] AS [code], [qtyABC] AS [qty]
FROM [resultABC]
UNION SELECT [codeDEF] AS [code], [qtyDEF] AS [qty]
FROM [resultDEF]

whereas it returns combined value of [code] & [qty].

How can I rewrite the query that I can show the results
([code],[qtyABC],[qtyDEF]) I want?

Thanks

Keke
[/QUOTE][/QUOTE][/QUOTE]
 
K

Keke Lee

Thanks for the script.
I tried to query by this script, and it has retured 2 rows for a same code
if both qtyABC & qtyDEF <> 0.
I was inspired by some other posts here and have made used the 'left join' &
'right join' query to get my results.
Many thanks of your work anyway
Cheers
Keke

Klatuu said:
SELECT codeABC, qtYABC, 0 AS qtyDEF FROM resultABC UNION SELECT codeDEF, 0
As qtyABC, qtyDEF FROM resultDEF;
--
Dave Hargis, Microsoft Access MVP


Keke Lee said:
No, my Union Query is to return values from other 2 query tables


Klatuu said:
If both of your queries are based on the same table, there is another way:
SELECT
Code:
, Sum(IIf([code] = "ABC",[qty],0)) AS qtyABC, Sum(IIf([code] =
"ABC",[qty],0)) as qtyDEF FROM MyTable GROUP BY [code];

--
Dave Hargis, Microsoft Access MVP


:

I have 2 queries to return below results:

resultABC:[codeABC], [qtyABC]
resultDEF:[codeDEF], [qtyDEF]

And I want to create a new query to show above results in below columns:

[code], [qtyABC], [qtyDEF]

whereAs [code] is a Union Select query to combine [codeABC] & [codeDEF] and
to take out the duplicates.

I am only able to create a SQL to query their combined results like:

SELECT [codeABC] AS [code], [qtyABC] AS [qty]
FROM [resultABC]
UNION SELECT [codeDEF] AS [code], [qtyDEF] AS [qty]
FROM [resultDEF]

whereas it returns combined value of [code] & [qty].

How can I rewrite the query that I can show the results
([code],[qtyABC],[qtyDEF]) I want?

Thanks

Keke
[/QUOTE][/QUOTE][/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Union Query 1
dsum sytax error 5
Graphical Union-Query Builder? 5
combining queries into 1 8
#Error in calculated field 3
Union Query 2
3 queries into 1 8
Duplicates in union query 3

Top