Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

tSQL multiple date ranges in one query

Posted on 2011-03-11
3
Medium Priority
?
400 Views
Last Modified: 2012-05-11

I need to create table with 3 different time ranges

(ONE) Targets table had TargetID as PK
to
(MANY) Visits table has TargetID

I am getting counts of targets and Business in the visit table..
How could I create it so there is a column for 30 days 60 days and 90 Days


The code below works for 30 days but i want it to have 2 more columns for 60 days and 90 days

any suggestions would be greatly appreciated

Select t.BusinessUnitID, Count(1) as Days30
from dbo.Targets t (nolock)
inner join dbo.VIEW_Visits vv (nolock) 
on vv.CreationTime > getdate()-30
and t.TargetID = vv.TargetID  
and t.businessUnitID in (9,18,1)
Group by  t.BusinessUnitID

Open in new window

0
Comment
Question by:Leo Torres
3 Comments
 
LVL 13

Assisted Solution

by:LIONKING
LIONKING earned 1000 total points
ID: 35111358
You could try something along the lines of:

Select t.BusinessUnitID,
c30=SUM(CASE WHEN DATEDIFF(d,vv.CreationTime, getdate()) BETWEEN 0 AND 30 THEN 1 ELSE 0 END),
c60=SUM(CASE WHEN DATEDIFF(d,vv.CreationTime, getdate()) BETWEEN 30 AND 60 THEN 1 ELSE 0 END),
c90=SUM(CASE WHEN DATEDIFF(d,vv.CreationTime, getdate()) BETWEEN 60 AND 90 THEN 1 ELSE 0 END)
from dbo.Targets t (nolock)
inner join dbo.VIEW_Visits vv (nolock)
on t.TargetID = vv.TargetID  
and t.businessUnitID in (9,18,1)
Group by  t.BusinessUnitID

Let us know how it goes...
0
 
LVL 61

Accepted Solution

by:
HainKurt earned 1000 total points
ID: 35111644
also try this:

Select t.BusinessUnitID,
       c30=SUM(CASE when vv.CreationTime-getdate() between 0 and 30 THEN 1 ELSE 0 END),
       c60=SUM(CASE when vv.CreationTime-getdate() between 31 and 60 THEN 1 ELSE 0 END),
       c90=SUM(CASE when vv.CreationTime-getdate() between 61 and 90 THEN 1 ELSE 0 END),
  from dbo.Targets t (nolock)
       inner join dbo.VIEW_Visits vv (nolock) on t.TargetID = vv.TargetID
 where  t.businessUnitID in (9,18,1) and vv.CreationTime > getdate()-90
 Group by  t.BusinessUnitID
0
 
LVL 8

Author Closing Comment

by:Leo Torres
ID: 35111778
haincurt is better it had a time constriant
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question