tSQL multiple date ranges in one query

Posted on 2011-03-11
Last Modified: 2012-05-11

I need to create table with 3 different time ranges

(ONE) Targets table had TargetID as PK
(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

Question by:Leo Torres
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 13

Assisted Solution

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...
LVL 51

Accepted Solution

Huseyin KAHRAMAN 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

Author Closing Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…

763 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