473,399 Members | 3,656 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,399 software developers and data experts.

UNION ALL GROUP BY

I have:

SELECT x, count(x)
FROM table1
WHERE ...

UNION ALL

SELECT x, count(x)
FROM table2
WHERE...

UNION ALL

SELECT x, count(x)
FROM table3
WHERE ...

UNION ALL
....
....

and I need to do GROUP BY with all the sentences.

is it possible? Thanks.

Feb 1 '06 #1
1 24706
"Dámaso Velázquez Álvarez" <dv********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I have:

SELECT x, count(x)
FROM table1
WHERE ...

UNION ALL

SELECT x, count(x)
FROM table2
WHERE...

UNION ALL

SELECT x, count(x)
FROM table3
WHERE ...

UNION ALL
...
...

and I need to do GROUP BY with all the sentences.

is it possible? Thanks.


One easier way to do this would be to use a derived table in a subquery in
the FROM clause:

SELECT x, COUNT(x)
FROM (
SELECT x FROM table1
UNION ALL
SELECT x FROM table1
UNION ALL
SELECT x FROM table1
)
GROUP BY x

This requires MySQL 4.1 or later, for support of subqueries.

If you cannot use subqueries, I think you need to do the SQL query without
the group by or the count(), and instead calculate the aggregate counting in
your application code.

Regards,
Bill K.
Feb 1 '06 #2

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

Similar topics

9
by: (Pete Cresswell) | last post by:
I've got some SQL that works as far as returning a recordset from a series of UNION statements. viz: SELECT whatever UNION this UNION that UNION other
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: Edward | last post by:
I am having conceptual trouble with the following query: select r.ServiceID,r.ContractID,sum(Total) from ( select csc.ServiceID,c.ContractID, sum(csc.ContainerMovement) as Total from...
1
by: jtwright | last post by:
I've got a view that creates a parent child relationship, this view is used in Analysis Services to create a dimension in a datastore. This query tends to deadlock after about 10 days of running...
5
by: Alicia | last post by:
Hello everyone based on the data, I created a union query which produces this. SELECT ,,, 0 As ClosedCount FROM UNION SELECT ,, 0 AS OpenedCount, FROM ORDER BY , ;
4
by: Mark | last post by:
Hi all, I am currently in the design stages of a database for work. I have come up with a way to get informaion I need using a union query but was wandering what effects this will have on...
3
by: NoodNutt | last post by:
G'day ppl. Can anyone assist me with the correct structure of the following in a Union Query. tblBookings.FinYear tblBookings.DepPrefPay tblBookings.IntPrefPay tblBookingsFinPrefPay
4
by: janko.klemensek | last post by:
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)?
3
by: paulquinlan100 | last post by:
Hi I'm trying to tweak the query below so that the second SELECT only pulls out one record per "Site_Ref", i.e. the record that has the maximum "appraisal_date" out of the 4 Appraisal_types. Is...
1
by: Brad Blankenshi | last post by:
I am creating a query that has multiples unions and a GROUP BY. If I remove the GROUP BY, it works, just doesn't provide the results I'm looking for. Can someone provide assistance to help me get...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.