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

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!!
ptemoAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Anthony PerkinsConnect With a Mentor Commented:
>>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
 
McExpCommented:
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
 
ptemoAuthor Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
McExpCommented:
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
 
ptemoAuthor Commented:
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
 
McExpCommented:
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
 
Anthony PerkinsCommented:
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
 
Anthony PerkinsCommented:
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
 
Anthony PerkinsCommented:
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
 
Anthony PerkinsCommented:
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
 
ptemoAuthor Commented:
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
 
Anthony PerkinsCommented:
The syntax is as follows:
SELECT @cutoffdate = sched_date
FROM SomeTableNameGoesHere
WHERE SomeConditionGoeshere
0
 
Scott PletcherSenior DBACommented:
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
 
Anthony PerkinsCommented:
>>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
All Courses

From novice to tech pro — start learning today.