Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 17th, 2003, 03:50 PM
vidigiani vidigiani is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 6 vidigiani User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Selecting counts of values in the same column for each entity

I have two tables, application_group and resource_group. An application_group may have multiple resource_groups. Each resource_group has its own state.

Visually, this looks like:

application_group
-----------------------
id VARCHAR(255)
display_name VARCHAR(255)

resource_group
--------------------
id VARCHAR(255)
app_group_id VARCHAR(255)
state VARCHAR(255)

Now, resource_group has a finite number of states. What I would like is a result set like so:

application_group.id
application_group.display_name
count(resource_groups where state = 'Online')
count(resource_groups where state = 'Offline')
count(resource_groups where state = 'Faulted')

There would be one row for each application_group.id. I can't quite seem to get this to work though. I've been working with nested queries, but I always get 1 row with all of the totals.

I have three questions:

- Is there a way to prepare the kind of result set I am looking for?
- Is it expensive? (I can always test this one if I can get the first issue worked out)
- Without putting state counts in the application_groups table, is there a better way to do this?

I don't want to put the counts in the applications_group table, because it will require me to maintain that state and it is no longer normalized.

Any help is appreciated!

Reply With Quote
  #2  
Old November 18th, 2003, 11:07 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,954 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 15 h 39 m 21 sec
Reputation Power: 1024
- Is there a way to prepare the kind of result set I am looking for?

yes


- Is it expensive? (I can always test this one if I can get the first issue worked out)

expensive??


- Without putting state counts in the application_groups table, is there a better way to do this?

you almost had it yourself:
Code:
select application_group.id
     , application_group.display_name
     , sum(case when state = 'Online' 
                then 1 else 0 end) as cntOnline 
     , sum(case when state = 'Offline' 
                then 1 else 0 end) as cntOffline 
     , sum(case when state = 'Faulted' 
                then 1 else 0 end) as cntFaulted
  from application_group
inner
  join resource_group
    on application_group.id 
     = resource_group.app_group_id
group
    by application_group.id
     , application_group.display_name


rudy
http://r937.com/

Reply With Quote
  #3  
Old November 18th, 2003, 11:41 AM
vidigiani vidigiani is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 6 vidigiani User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks Rudy! I did end up solving it earlier today, but it a much less elegant way. I ended up having something like 6 joins!

I tried the syntax you gave me in HSQLDB, which is the DB I have been using for dev testing, but it doesn't like it. Time to switch to something a little more functional.

Thanks again!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Selecting counts of values in the same column for each entity


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT