Solved

tSQL multiple date ranges in one query

Posted on 2011-03-11
3
394 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
[X]
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
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 53

Accepted Solution

by:
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
0
 
LVL 8

Author Closing Comment

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

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

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…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…

734 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