?
Solved

tsql syntax

Posted on 2011-02-18
5
Medium Priority
?
315 Views
Last Modified: 2012-05-11
Hello,


I have a table that has patient-id, visit year, plus some other columns....

It is possible to write some sql that produces a simple frequency table that shows me year and number of visits for each patient?

I am particularly interested in seeing which years the patient did not make any visit, but also need to see how many visits per year in the other years.  i.e. i dont want to exclude the years the patients did not visit.

Also if it is possible I would like to go a step further an see the distribution of the number of visits.  What i mean by this is I would like to see for each year how many patients made zero visits, how many one, how many two, how many three etc.

Thanks.
0
Comment
Question by:soozh
[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
  • 2
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
devlab2012 earned 2000 total points
ID: 34931928
The following example will help you:

CREATE TABLE Patients
(
      PatientID      int,
      VisitYear      varchar(4)
)
GO

INSERT INTO Patients VALUES(1, 2010)
INSERT INTO Patients VALUES(2, 2010)
INSERT INTO Patients VALUES(2, 2011)
INSERT INTO Patients VALUES(1, 2009)
INSERT INTO Patients VALUES(1, 2009)
GO

SELECT PatientID, VisitYear, count(*) YearlyVisits FROM Patients
GROUP BY PatientID, VisitYear
GO

WITH PatientsCTE(PatientID, VisitYear, YearlyVisits)
AS
(
      SELECT PatientID, VisitYear, count(*) YearlyVisits FROM Patients
      GROUP BY PatientID, VisitYear
)
select PatientID, isNull([2008], 0) Year2008,  isNull([2009],0) Year2009,
isNull([2010],0) Year2010, isNull([2011], 0) Year2011 from PatientsCTE
pivot(sum(YearlyVisits) for VisitYear IN ([2008], [2009], [2010], [2011]))
AS PatientVisits
GO
0
 
LVL 41

Expert Comment

by:Sharath
ID: 34932590
Do you have fixed no. of years?
Do you want the result pivoted as mentioned above?
0
 

Author Comment

by:soozh
ID: 34932761
i dont know which years they are... i hoped that the years could be taken from the data.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 34944207
Can you provide some sample data with expected result?
0
 

Author Closing Comment

by:soozh
ID: 35068657
Not exactly the solution i wanted... but it worked..
0

Featured Post

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 14 hours left to enroll

762 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