Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-14
14
Medium Priority
?
495 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
[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
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

730 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