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 December 8th, 2003, 04:45 PM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Query Refinement: count and group by

Sorry for the subject, I couldn't really come up with a good/short description for what I'm trying to do.

I am refining a report for our ER department that uses a Postgres database. The managers would like a report which lists the population of our ER department by hour.

The relevant table setup looks like (excluding fields that aren't necessary).

inc_id | evttype_id | evtdate
Primary key is a composite of inc_id, evttype_id.

The inc_id is an incident id which is a foreign key to an incidents table with additional incident information, the evttype_id is an id code for the specific event (arrival, departure, triage, er, etc.) and the evtdate is a timestamp of when the event occurred. For each action that happens for a particular incident an entry is placed into this incident_event table.

I also have a view (call it all_report)which groups everything the info together so via joins and subselects it looks like:
inc_id | arrivaltime | triagetime | ertime | departuretime | other times... | patient info.... | other info.

Would it be possible to write a population by hour summary in one query?

For instance, if I wanted to do a query for each hour I could do something like:

SELECT count(*) FROM all_report WHERE arrivaltime BETWEEN '12-8-2003 02:00'::timestamp AND '12-8-2003 03:00'::timestamp AND (departuretime IS NULL OR departuretime > '12-8-2003 03:00'::timestamp)

for each hour in the day, and repeat (if necessary), however I was wondering if I could somehow come up with a more elegant solution (either with the view, or just by querying the incident_event table itself.

Thanks,
-b

PS I can provide the full table schemas I just chose to leave out unrelated portions to avoid confusion.
__________________
PostgreSQL, it's what's for dinner...

Reply With Quote
  #2  
Old December 8th, 2003, 05:11 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 6 sec
Reputation Power: 1018
generate the hours using an integers table

for an example of generating months, see
http://searchdatabase.techtarget.co...x285649,00.html

let me know if you need further help

rudy
http://r937.com/

Reply With Quote
  #3  
Old December 9th, 2003, 09:05 AM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Thanks!
The minimalist in me wants to do it without an extra table, but this looks to be the most efficient way of doing it.

-b

Reply With Quote
  #4  
Old December 9th, 2003, 09:24 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 6 sec
Reputation Power: 1018
i don't know postgresql very well, but in sql server you can generate a derived table "out of thin air" as it were

so since you only need integers 1 through 24, perhaps you could use

select stuff from
(select 1 union select 2 union ... select 24) dt
left outer join ...

Reply With Quote
  #5  
Old December 9th, 2003, 10:08 AM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
You can do:
SELECT * FROM (SELECT 1 UNION SELECT 2 ...) AS tempint

but I figure I might as well just keep the int table around in case I need to do more date related stuff or any other random items that require left joins and an integer lookup stuff.

Also, just for clarification purposes for others that would like to apply this to postgres, in postgres hours are specified from 0 to 23.

Here's what the select query ended up looking like:

SELECT i AS hr, count(inc_id) AS hrcount FROM integers LEFT JOIN all_report ON i BETWEEN EXTRACT('hour' FROM arrivaltime) AND EXTRACT('hour' FROM departure) AND arrivaltime > NOW() - '1 day'::interval WHERE i BETWEEN 0 AND 23 GROUP BY hr

Thanks again r937.

-b

Reply With Quote
  #6  
Old December 9th, 2003, 10:17 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 6 sec
Reputation Power: 1018
dontcha just *heart* the integers table?

your query looks mighty spiffy

glad i could help

get it? "i" could help? oh, never mind


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Query Refinement: count and group by


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 5 hosted by Hostway
Stay green...Green IT