Solved

tsql syntax

Posted on 2011-02-18
5
308 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
  • 2
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
devlab2012 earned 500 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 40

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 40

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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