472,325 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

use count & group by with union

sharijl
I have a SQL query which returns the total amount of issues:

[HTML]SELECT issue,Count(issue) as total FROM mytable Group by issue[/HTML]

This works but I need the total of the same catagory in three tables so I assumed a union would work so tried something like this:

[HTML]SELECT issue,Count(issue) as total FROM mytable Group by issue
Union
SELECT issue,Count(issue) as totalFROM mytable2 Group by issue
union
SELECT issue,Count(issue) as total FROM mytable3 Group by issue[/HTML]

the problem is that the issue are duplicated so I will not get the total count I am looking for.

current results:
[HTML]
issue total
headache 4
headache 30
headache 2
cough 5
cough 55
cough 5[/HTML]

wanted result:
[HTML]
issue total
headache 36
cough 65[/HTML]


I assumed I could simply add sum(total) but did not work. What is the correct way to do this?
Jul 31 '07 #1
7 19750
hariharanmca
1,977 1GB
I have a SQL query which returns the total amount of issues:

[HTML]SELECT issue,Count(issue) as total FROM mytable Group by issue[/HTML]

This works but I need the total of the same catagory in three tables so I assumed a union would work so tried something like this:

[HTML]SELECT issue,Count(issue) as total FROM mytable Group by issue
Union
SELECT issue,Count(issue) as totalFROM mytable2 Group by issue
union
SELECT issue,Count(issue) as total FROM mytable3 Group by issue[/HTML]

the problem is that the issue are duplicated so I will not get the total count I am looking for.

current results:
[HTML]
issue total
headache 4
headache 30
headache 2
cough 5
cough 55
cough 5[/HTML]

wanted result:
[HTML]
issue total
headache 36
cough 65[/HTML]


I assumed I could simply add sum(total) but did not work. What is the correct way to do this?

You can use like

Expand|Select|Wrap|Line Numbers
  1. SELECT issue,Count(issue) as total, 
  2. (
  3. SELECT Count(issue) as total1 FROM mytable2 
  4. Where mytable2.issue = mytable.issue
  5. ),
  6. (
  7. SELECT Count(issue) as total2 FROM mytable3
  8. Where mytable3.issue = mytable.issue
  9. )
  10.  FROM mytable Group by mytable.issue
Jul 31 '07 #2
azimmer
200 Expert 100+
If my understanding of your problem is correct, you can do it the other way round:
Expand|Select|Wrap|Line Numbers
  1. select issue, Count(issue) as total
  2. from
  3. (
  4. select issue from mytable1
  5. union
  6. select issue from mytable2
  7. union
  8. select issue from mytable3
  9. )
  10. group by issue
  11.  
Jul 31 '07 #3
I will try your suggestion azimmer and post the results. I really hope that will work because the other suggestions will not give the results I wanted. with this I have four columns displayed.

issue
total (from table one only)
unnamed( count from table 2)
unnamed (count from table 3)

I need to somehow compile total + unnamed + unnamed

Thanks all
Jul 31 '07 #4
Still no luck, I got a MS SQL server syntax error (near keywork group).

Expand|Select|Wrap|Line Numbers
  1. select issue, Count(issue) as total
  2.  
  3. from
  4.  
  5.       (
  6.      select issue from mytable1
  7.  
  8.       union
  9.  
  10.       select issue from mytable2
  11.  
  12.       union
  13.  
  14.       select issue from mytable3
  15.       )
  16.       group by issue
Any other suggestions? I know it has to be possible.
Aug 1 '07 #5
hariharanmca
1,977 1GB
Still no luck, I got a MS SQL server syntax error (near keywork group).

Expand|Select|Wrap|Line Numbers
  1. select issue, Count(issue) as total
  2.  
  3. from
  4.  
  5.       (
  6.      select issue from mytable1
  7.  
  8.       union
  9.  
  10.       select issue from mytable2
  11.  
  12.       union
  13.  
  14.       select issue from mytable3
  15.       )
  16.       group by issue
Any other suggestions? I know it has to be possible.
just try Group By myTable1.issue
Aug 1 '07 #6
azimmer
200 Expert 100+
Still no luck, I got a MS SQL server syntax error (near keywork group).

Expand|Select|Wrap|Line Numbers
  1. select issue, Count(issue) as total
  2.  
  3. from
  4.  
  5.       (
  6.      select issue from mytable1
  7.  
  8.       union
  9.  
  10.       select issue from mytable2
  11.  
  12.       union
  13.  
  14.       select issue from mytable3
  15.       )
  16.       group by issue
Any other suggestions? I know it has to be possible.
Sorry, my fault, now I copy it from Query Analyzer (:-)):

Expand|Select|Wrap|Line Numbers
  1. select x.issue, count(x.issue)
  2. from (
  3. select issue from mytable1
  4. union all
  5. select issue from mytable2
  6. union all
  7. select issue from mytable3
  8. ) as x
  9. group by x.issue
  10.  
Aug 1 '07 #7
Thank you. have not tried it yet but I have a good feeling about that query because I can see the logic. :-)
Aug 1 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Son KwonNam | last post by:
When I try <xsl:value-of select="count('/GROUPS/GROUP')"/> on JSTL <x:transform> tag, I got the following error message. ...
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...
2
by: Greg Strong | last post by:
Hello All, Is there a simpler way to count text boxes with data in them when the 4 text boxes are in the header of a form? I've written the...
1
by: sammy | last post by:
If you have a select with 2 attributes where you group by one attribute and do a count() for the second attribute, if the count() is 0 then that...
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...
10
by: marting | last post by:
Before I throw my new expensive laptop out of the window! I'm stuck on getting my joins right with the correct amount in a column count. After...
3
by: weirdguy | last post by:
Hello all, Before you read my question, I would advise you to visit the Image-Link. The image-link shows 4Tables - its field, Primary, Foreign...
0
by: ryangsh | last post by:
I'm trying to consolidate inventory records in db. (I'm displaying the result in a table form but for this post I'm putting it simple) table_name=...
8
by: Amy Badgett | last post by:
The purpose of this query is to return seven different Counts for these 14 different EIDs (employee IDs). The problem is that only 8 of the EIDs are...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.