soozh
asked on
Query syntax
Hello,
I have an sql statement that returns data regarding patient visits. Like this:
Select pat_id, VisitDate, ….. etc from PatientData order by pat_id asc, VisitDate asc
A patient can have more than one visit so it returns data that looks like:
I guess I need to have some sub select over each patients records but I can’t get my head round it.
Anyone who can help?
I have an sql statement that returns data regarding patient visits. Like this:
Select pat_id, VisitDate, ….. etc from PatientData order by pat_id asc, VisitDate asc
A patient can have more than one visit so it returns data that looks like:
pat_id visitdate,
23, 2012-03-01, …….
23, 2012-07-22, …….
23, 2014-08-03, ……
25, 2012-05-01, …….
25, 2012-09-20, …….
26, 2012-11-19, …….
26, 2013-02-22, …….
26, 2015-02-03, ……
Now I would like to add a flag which indicates the first visit for each patient. Likepat_id, visitdate, firstvisit,
23, 2012-03-01, 1, …….
23, 2012-07-22, 0, …….
23, 2014-08-03, 0, ……
25, 2012-05-01, 1, …….
25, 2012-09-20, 0, …….
26, 2012-11-19, 1, …….
26, 2013-02-22, 0, …….
26, 2015-02-03, 0,……
I guess I need to have some sub select over each patients records but I can’t get my head round it.
Anyone who can help?
ASKER
Hello,
Well maybe i should have told you the sql server version.
It is SQL Server 2008.
So i wonder if the order by works in the partition statement because i am getting a syntax error.
/richard
Well maybe i should have told you the sql server version.
It is SQL Server 2008.
So i wonder if the order by works in the partition statement because i am getting a syntax error.
/richard
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
If it does not help, can you please share the version of SQL Server that you are using?