General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 2nd, 2005, 06:04 PM
jerdean101 jerdean101 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 1 jerdean101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 2 sec
Reputation Power: 0
Counts for date ranges.

Hi all,

I appreciate your taking a moment to help a brother out.

I have a table similar to this:

----------------------------------------- -------- ----------------------------
CUSTOMER_NUMBER NOT NULL NUMBER(18)
CLIENT_ID NOT NULL VARCHAR(18)
ORDER_DATE NOT NULL DATE


in my DB a new line is created in this table for every order processed.

What I need to do is to build a query that will provide me a count by client id
broken out by last 7 days, 8-14 days and 15-21 days.

I would like the output to look something like this.

CLIENT_ID Last 7 Days 8-14 Days 15-21 Days
Hostess 15 21 4
Orowheat 8 25 19


I've tried writing this query 100 different times and have gotten nowhere. Any Help
is appreciated.

Reply With Quote
  #2  
Old May 2nd, 2005, 09:24 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 4
[Edit]
Here's a much nicer query than my original:

Code:
  SELECT
   client_id,
   SUM(IF(order_date > DATE_SUB(CURDATE(),INTERVAL 7 DAY),1,0)) as week_1,
   SUM(IF(order_date BETWEEN DATE_SUB(CURDATE(),INTERVAL 14 DAY) AND DATE_SUB(CURDATE(),INTERVAL 8 DAY),1,0)) as week_2,
   SUM(IF(order_date BETWEEN DATE_SUB(CURDATE(),INTERVAL 21 DAY) AND DATE_SUB(CURDATE(),INTERVAL 15 DAY),1,0)) as week_3
  FROM
   table
  GROUP BY
   client_id
  


Note that the above has been tested in MySQL only, so it may need some tweaking for your system.
[/Edit]
__________________
"A pawn is the most important piece on the chessboard -- to a pawn"


Last edited by Madpawn : May 3rd, 2005 at 12:54 AM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > Counts for date ranges.


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 3 hosted by Hostway