Solved

tSQL multiple date ranges in one query

Posted on 2011-03-11
3
385 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 250 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 51

Accepted Solution

by:
HainKurt earned 250 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now