473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Group by the union

Can I Group by the Union in one query or must I write two queries for
this (one for union and second for group by)?

May 20 '07 #1
4 11632
On May 20, 7:28 pm, janko.klemen...@gmail.com wrote:
Can I Group by the Union in one query or must I write two queries for
this (one for union and second for group by)?

you can sub-query the union query:

select * from
(select a, b, c from tblx
union all
select a, b, c from tbly) as qryUnion
group by a, b, c

however, if the point of your grouping is to remove duplicate rows,
then use "union" rather then "union all", and this will be automatic.

May 21 '07 #2
I have one big query:

SELECT ID_Artikel, Kolicina

FROM

(SELECT tblDokumentiDetails.ID_Artikel, tblDokumentiDetails.Kolicina
FROM (tblDokumenti INNER JOIN tblDokumentiDetails ON
tblDokumenti.ID_Dokument = tblDokumentiDetails.ID_Dokument) INNER JOIN
[SELECT tblDokumentiDetails.ID_Artikel, Max(tblDokumenti.Datum) AS
ZadnjiDatum
FROM tblDokumenti INNER JOIN tblDokumentiDetails ON
tblDokumenti.ID_Dokument = tblDokumentiDetails.ID_Dokument
WHERE tblDokumenti.ID_Vrsta=3 And tblDokumenti.Storniran=false And
tblDokumenti.Zakljucen=true
GROUP BY tblDokumentiDetails.ID_Artikel]. AS ZadnjaInventura ON
(tblDokumentiDetails.ID_Artikel = ZadnjaInventura.ID_Artikel) AND
(ZadnjaInventura.ZadnjiDatum = tblDokumenti.Datum))

UNION ALL

(SELECT tblDokumentiDetails.ID_Artikel, tblDokumentiDetails.Kolicina
FROM (tblDokumenti INNER JOIN tblDokumentiDetails ON
tblDokumenti.ID_Dokument = tblDokumentiDetails.ID_Dokument) INNER JOIN
[SELECT tblDokumentiDetails.ID_Artikel, Max(tblDokumenti.Datum) AS
ZadnjiDatum
FROM tblDokumenti INNER JOIN tblDokumentiDetails ON
tblDokumenti.ID_Dokument = tblDokumentiDetails.ID_Dokument
WHERE (((tblDokumenti.ID_Vrsta)=3))
GROUP BY tblDokumentiDetails.ID_Artikel]. AS ZadnjaInventura ON
(ZadnjaInventura.ZadnjiDatum < tblDokumenti.Datum) AND
(tblDokumentiDetails.ID_Artikel = ZadnjaInventura.ID_Artikel)
WHERE tblDokumenti.ID_Vrsta = 1 And tblDokumenti.Storniran=false And
tblDokumenti.Zakljucen=true)

UNION ALL

(SELECT tblDokumentiDetails.ID_Artikel, - tblDokumentiDetails.Kolicina
AS Kolicina
FROM (tblDokumenti INNER JOIN tblDokumentiDetails ON
tblDokumenti.ID_Dokument = tblDokumentiDetails.ID_Dokument) INNER JOIN
[SELECT tblDokumentiDetails.ID_Artikel, Max(tblDokumenti.Datum) AS
ZadnjiDatum
FROM tblDokumenti INNER JOIN tblDokumentiDetails ON
tblDokumenti.ID_Dokument = tblDokumentiDetails.ID_Dokument
WHERE (((tblDokumenti.ID_Vrsta)=3))
GROUP BY tblDokumentiDetails.ID_Artikel]. AS ZadnjaInventura ON
(ZadnjaInventura.ZadnjiDatum < tblDokumenti.Datum) AND
(tblDokumentiDetails.ID_Artikel = ZadnjaInventura.ID_Artikel)
WHERE tblDokumenti.ID_Vrsta = 2 And tblDokumenti.Storniran=false And
tblDokumenti.Zakljucen=true);

That works OK. But If I want to group this result like this:

SELECT ID_Artikel

FROM

( BIG QUERY AFTER FROM) AS test
GROUP BY test.ID_Artikel

I get Syntax error in JOIN operation :(

BillCo je napisal:
On May 20, 7:28 pm, janko.klemen...@gmail.com wrote:
Can I Group by the Union in one query or must I write two queries for
this (one for union and second for group by)?


you can sub-query the union query:

select * from
(select a, b, c from tblx
union all
select a, b, c from tbly) as qryUnion
group by a, b, c

however, if the point of your grouping is to remove duplicate rows,
then use "union" rather then "union all", and this will be automatic.
May 21 '07 #3

i would have expected a different error there: Kolicina is not part of
an expression or a group by clause
....try including it in the group by section. failing that, i don't
know. when you write the query like you described there without the
group by, you should be able to view the resulting union sub-query as
a table in the query editor. try adding the grouping here and see if
it works.
Personally, I like to save the queries seperately and join them in a
master query - it makes debugging and editing easier
May 22 '07 #4
tnx for advice and your time

BillCo je napisal:
i would have expected a different error there: Kolicina is not part of
an expression or a group by clause
...try including it in the group by section. failing that, i don't
know. when you write the query like you described there without the
group by, you should be able to view the resulting union sub-query as
a table in the query editor. try adding the grouping here and see if
it works.
Personally, I like to save the queries seperately and join them in a
master query - it makes debugging and editing easier
May 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jaidev Paruchuri | last post by:
I have a table(work_order) with time as varchar(5). The values in table looks like this work_order_id rtim 1 08:15 2 08:45 3 10:13 4 ...
3
by: Abhi | last post by:
Hi! I am wondering if this query is possible somehow: I have a table with many fields that all can have a value from 1 to 5. if I wanna see the count of each value from one field, then this...
1
by: Dámaso Velázquez Álvarez | last post by:
I have: SELECT x, count(x) FROM table1 WHERE ... UNION ALL SELECT x, count(x) FROM table2
15
by: edouard.spooner | last post by:
Hi, I have a tricky SQL query problem that I'm having probs with. I have a table which resembles something like this Date | Price1 | Price2 | Price3 01 Jan 2006 | 100 | 100 | 100 02 Jan...
2
by: Jonathan Woods | last post by:
Hi there, I have no idea why the following TSQL does not work.Any Idea? I am using SQL 2000 Server CREATE PROCEDURE RptDailySummary2 @FromTxDate DATETIME, @ToTxDate DATETIME AS BEGIN
0
by: RSN | last post by:
I hav a query that uses a union of 16 tables to get some counts and i need to get the end result as a Group by. This is inside a sub-query that has the count(*) ans the attribute for the group-by in...
7
sharijl
by: sharijl | last post by:
I have a SQL query which returns the total amount of issues: SELECT issue,Count(issue) as total FROM mytable Group by issue This works but I need the total of the same catagory in three tables...
2
by: benhaynes | last post by:
I am writing a query to create a tree menu, it pulls from a table of music "tracks". In this database there are four "sub_genre" fields for each track, and I need to create a list of all used...
3
by: pjewett | last post by:
Hi All, New to the forum and to SQL. I'm running a query to pull sales records for a specific customer that returns several dates (for each transaction). I only need the most recent date from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.