473,382 Members | 1,107 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,382 software developers and data experts.

use of group by with union all

i hv a code like this

select code, sum(amount) aaaa, 0 bbbb, 0 cccc
from f1
where .....

union all

select code, 0 aaaa, sum(amount) bbbb, 0 cccc
from f1
where .....

union all

select code, 0 aaaa, 0 bbbb, sum(amount) cccc
from f1
where .....

the select must be group by code.

whwre do i put the group by clause
May 30 '07 #1
1 19960
pradeep kaltari
102 Expert 100+
i hv a code like this

select code, sum(amount) aaaa, 0 bbbb, 0 cccc
from f1
where .....

union all

select code, 0 aaaa, sum(amount) bbbb, 0 cccc
from f1
where .....

union all

select code, 0 aaaa, 0 bbbb, sum(amount) cccc
from f1
where .....

the select must be group by code.

whwre do i put the group by clause
Hi,
You can put the GROUP BY clause after each of the SELECT statements. Something like:
Expand|Select|Wrap|Line Numbers
  1.  
  2. select code, sum(amount) aaaa, 0 bbbb, 0 cccc
  3. from f1
  4. where .....
  5. group by code
  6. union all
  7.  
  8. select code, 0 aaaa, sum(amount) bbbb, 0 cccc
  9. from f1
  10. where .....
  11. group by code
  12.  
I hope this helps you.

Regards,
Pradeep
May 30 '07 #2

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

Similar topics

3
by: Rich Protzel | last post by:
Hello, So my table contains say 100,000 records, and I need to group the categories in fld1 by the highest count of subcategories. Say fld1 contains categories A, B, C, D, E. All of these...
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...
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)?
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.