SunQuest
           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:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old June 9th, 2003, 11:01 AM
Statik Statik is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 129 Statik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
One query, multiple date ranges in SQL

Hi all,

We're using the Pear DBI to interface with multiple DBs, so I'm looking for a regular SQL query here. I want to query a timestamped table for the number of records in each week of a year. Presently, this takes 52 queries. Is there a way to do this in one query?

Format:
__________________
Log_ID int
Session_ID int
Page_ID int
DateStamp datetime



Thanks

Statik

Reply With Quote
  #2  
Old June 10th, 2003, 12:28 PM
Set Set is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Oslo, Norway
Posts: 11 Set User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Statik!

I doubt it is possible through straight SQL with only the fields you've stated. I think you would need to add WeekNo, either as a computed column (wonder how many databases support this?) or through defining a view. Using either of these, it should be easy to write the query.

Making 52 queries sounds like the most time consuming and error prone way to do this - and then you miss week 53 every 6 years or so.

Reply With Quote
  #3  
Old June 27th, 2003, 05:46 PM
smumey smumey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Canada
Posts: 1 smumey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,

Would something like this work:

select week, count(*)
from
(
select extract(week from timestamp) as week
from yourtable
) as temp
group by week;

Last edited by smumey : June 27th, 2003 at 05:55 PM.

Reply With Quote
  #4  
Old June 27th, 2003, 08:22 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
Code:
select i as weekno
     , count(Log_ID) as records
  from integers
left outer
  join yourtable
    on i = week(DateStamp)
 where i between 1 and 53
group
    by i

there's a wee problem here in that not every database supports a WEEK() function

you'll also need an integers table, but it's a snap to create and you only have to do it once

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > One query, multiple date ranges in SQL


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