dbTalk Databases Forums  

Multiple aggregation in a single query

comp.databases.ingres comp.databases.ingres


Discuss Multiple aggregation in a single query in the comp.databases.ingres forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Simon Roust
 
Posts: n/a

Default Multiple aggregation in a single query - 11-03-2010 , 10:07 AM






Hi, I'm using Ingres 9.0.4 and have the following table (I've
simplified this to make it easier to explain):

create table students
(student_id char(6) not null with default,
module_id char(6) not null with default,
stud_status char(1) not null with default)

stud_status has values of A,B + C.

I want to compose a query to give me a count of students on the module
and how many at each status.
e.g.

Module Total Count A Count B Count C Count
T101 50 25 10 15
A444 120 65 44 11


I can get part way there by doing the following:

select module_id, count(*) as total_count
from students
group by module_id

Which gives the count of students for each module (first two columns
required).


I can also get the count of A,B+C statuses by module individually

select module_id, count(*) as a_count
from students
where stud_status='A'
group by module_id ;

select module_id, count(*) as b_count
from students
where stud_status='B'
group by module_id ;

select module_id, count(*) as c_count
from students
where stud_status='C'
group by module_id ;


Then I can programmatically merge all this data into the format I need
(or use some temporary tables).

But is there a more elegant way I can retrieve this data that I'm
missing? Can the case expression some how help me? Or maybe sub queries?

In an ideal world I'd run like to run a single query to get this data in
one hit.

Any thoughts welcome.

Cheers,

Simon

Reply With Quote
  #2  
Old   
Karl Schendel
 
Posts: n/a

Default Re: [Info-Ingres] Multiple aggregation in a single query - 11-03-2010 , 10:40 AM






On Nov 3, 2010, at 12:07 PM, Simon Roust wrote:

Quote:
Hi, I'm using Ingres 9.0.4 and have the following table (I've
simplified this to make it easier to explain):

create table students
(student_id char(6) not null with default,
module_id char(6) not null with default,
stud_status char(1) not null with default)

stud_status has values of A,B + C.

I want to compose a query to give me a count of students on the module
and how many at each status.
e.g.

Module Total Count A Count B Count C Count
T101 50 25 10 15
A444 120 65 44 11
select module_id, count(*) as total_count,
sum(case when stud_status = 'A' then 1 else 0 end) as a_count,
... repeat for b and c
from students
group by module_id

I haven't tried this but it ought to work.

Karl

Reply With Quote
  #3  
Old   
Simon Roust
 
Posts: n/a

Default Re: [Info-Ingres] Multiple aggregation in a single query - 11-03-2010 , 12:12 PM



On 03/11/2010 16:40, Karl Schendel wrote:

Quote:
select module_id, count(*) as total_count,
sum(case when stud_status = 'A' then 1 else 0 end) as a_count,
... repeat for b and c
from students
group by module_id

I haven't tried this but it ought to work.

Karl
It works extremely well and is most compact too. Thanks very much!

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.