Solved

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

Posted on 2010-08-14
14
479 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:ScottPletcher
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

IT, Stop Being Called Into Every Meeting

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

759 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

21 Experts available now in Live!

Get 1:1 Help Now