Solved

TSQL: Counting the number of visits base on year of visit

Posted on 2010-08-14
14
487 Views
Last Modified: 2012-08-14
Hello All,

I need some help with the count feature.  I want to know if it is possible to use the count function to determine how many times a person has came in for a year up to a particular date.  For example if a person has came in 10 times starting from dec 2009 ( 3 times)  to feb 2010 ( 7 times).  So if i put in a jan date, it will count all the visits up to that date, but the entire of 2010.

I hope that makes sense

thanks in advance!!
0
Comment
Question by:ptemo
  • 7
  • 3
  • 3
  • +1
14 Comments
 
LVL 16

Expert Comment

by:McExp
ID: 33436335
the question more specifically relates to yous where clause rather than the count.

If you get the where clause correct you'll be fine

The simplest solution would be to use your input parameter as the max date and construct a new min date using the Year() function

Would need more info on the actual code to help further
0
 

Author Comment

by:ptemo
ID: 33436370
Hello McExp

Thanks for your help thus far. I'm still new to Tsql, so i'm a bit confused.  I apoligize for not adding the code earlier.  This is what I have so far.

SELECT COUNT(sched_date) AS Expr1
FROM     Schedule
WHERE  (sched_status = 'Yes')

So where exactly would I put the Year funcion?

thank you!!
0
 
LVL 16

Expert Comment

by:McExp
ID: 33436400
I don't have a SQL server to test right now but assuming you have the cutoff date in a variable something like the query below should do it for you

declare @cutoffDate
--Need to assign cutoffDate to something

SELECT COUNT(sched_date) AS Expr1
FROM     Schedule
WHERE  (sched_status = 'Yes') AND
(sched_date >= Year(@cutoffDate)+'-01-01') AND
(sched_date <= @cutoffDate)
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:ptemo
ID: 33436460
McExp,

Thanks for your speedy replies.  I tried your code, with minor modification.

declare @cutoffDate datetime
set @cutoffDate = 5/1/10
SELECT COUNT(sched_date) AS Expr1
FROM     Schedule
WHERE  (sched_status = 'Yes') AND (sched_date >= YEAR(@cutoffDate) + '-01-01') AND (sched_date <= @cutoffDate)



Howevver i get an error message "coversion failed when converting the varchar value '-01 -01' to data type in
0
 
LVL 16

Expert Comment

by:McExp
ID: 33436619
add a STR() function arround your Year, it's having some problems doing the data type conversion

e.g. STR(YEAR(@cutoffDate)) + '-01-01'
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33436924
You need to wrap dates in quotes, this:
declare @cutoffDate datetime
set @cutoffDate = 5/1/10

Resolves to: 1900-01-01 00:00:00.000 which I am sure is not what you expected/wanted.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33436953
If all you want to do is get the YTD shedule visits based on a final date then try it this way (this makes the assumption that sched_date is date data type or if it is datetime it is an "exact" date: the time is set to midnight.  If it is not we will need to make adjustments):
DECLARE @cutoffDate date

SET @cutoffDate = '20100501'

SELECT  COUNT(*) ScheduleCount
FROM    Schedule
WHERE   sched_status = 'Yes'
        AND sched_date BETWEEN DATEADD(DAY, 1 - DAY(@cutoffDate), @cutoffDate) AND @cutoffDate

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33436963
The above will get you the count for all the persons scheduled.

Now supposing you want to now want "to use the count function to determine how many times a person has came in for a year up to a particular date" then assuming there is something like a person attribute in Schedule (PersonID) you can do something like this:
DECLARE @cutoffDate date,
        @PersonID integer

SELECT  @cutoffDate = '20100501',
        @PersonID = 123456

SELECT  COUNT(*) ScheduleCount
FROM    Schedule
WHERE   sched_status = 'Yes'
        AND sched_date BETWEEN DATEADD(DAY, 1 - DAY(@cutoffDate), @cutoffDate) AND @cutoffDate
        AND PersonID = @PersonID

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33436980
Finally you can take it one more step.  This will give you the count for each person in Schedule (again assuming that there is a Person attribute called PersonID) sorted by person:
DECLARE @cutoffDate date

SET @cutoffDate = '20100501'

SELECT  PersonID,
        COUNT(*) ScheduleCount
FROM    Schedule
WHERE   sched_status = 'Yes'
        AND sched_date BETWEEN DATEADD(DAY, 1 - DAY(@cutoffDate), @cutoffDate) AND @cutoffDate
GROUP BY
        PersonID
ORDER BY
        PersonID

Open in new window

0
 

Author Comment

by:ptemo
ID: 33437022
Thank you to everyone for there help so far.

How do I set the date to a column, i tried this and it didnt work
set @cutoffdate = sched_date

what appears to be the problem

thank you again
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33437080
The syntax is as follows:
SELECT @cutoffdate = sched_date
FROM SomeTableNameGoesHere
WHERE SomeConditionGoeshere
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 33450024
Don't think this:

BETWEEN DATEADD(DAY, 1 - DAY(@cutoffDate), @cutoffDate) AND @cutoffDate

will give you a year's range.  Did you mean this?:

BETWEEN DATEADD(DAY, 1 - DATEPART(DAYOFYEAR, @cutoffDate), @cutoffDate) AND @cutoffDate


I'm not sure that properly considers leap years, and seems somewhat unclear to me.  

Why not jus?t:

BETWEEN DATEADD(DAY, 1, DATEADD(YEAR, -1, @cutoffDate)) AND @cutoffDate
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33450799
>>will give you a year's range.  Did you mean this?:<<
You are right.  I did mean:
BETWEEN DATEADD(DAY, 1 - DATEPART(DAYOFYEAR, @cutoffDate), @cutoffDate) AND @cutoffDate

Thanks.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 33450807
>>I'm not sure that properly considers leap years<<
It seems to be fine for leap years (see below).

>>Why not jus?t:
BETWEEN DATEADD(DAY, 1, DATEADD(YEAR, -1, @cutoffDate)) AND @cutoffDate<<
That would depend on the intent of the author.  I interpreted it as they wanted YTD.  This solution provides a full year.

ptemo,

See below for the two results.  Pick your poison:

DECLARE @cutoffDate date
SET @cutoffDate = '20120630'
SELECT	DATEADD(DAY, 1 - DATEPART(DAYOFYEAR, @cutoffDate), @cutoffDate),
		DATEADD(DAY, 1, DATEADD(YEAR, -1, @cutoffDate))

Open in new window

0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

803 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