Solved

Query syntax

Posted on 2016-08-23
4
100 Views
Last Modified: 2016-09-09
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:
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, ……

Open in new window

Now I would like to add a flag which indicates the first visit for each patient. Like
pat_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,……

Open in new window


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?
0
Comment
Question by:soozh
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Nakul Vachhrajani
ID: 41766959
Does this help? (Works on SQL Server 2012 and above).

DECLARE @patientTable TABLE (pat_id INT, visitdate DATE)

INSERT INTO @patientTable (pat_id, visitdate)
VALUES (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');

SELECT pt.pat_id,
       pt.visitdate,
       CASE WHEN pt.visitdate = MIN(pt.visitdate) OVER (PARTITION BY pt.pat_id ORDER BY pt.visitdate ASC)
            THEN 1
            ELSE 0
       END AS IsFirstvisit
FROM @patientTable AS pt;

Open in new window


If it does not help, can you please share the version of SQL Server that you are using?
0
 

Author Comment

by:soozh
ID: 41767060
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
0
 
LVL 12

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 250 total points
ID: 41767086
Yes, the version is important because a lower version restricts the ability to use better and more readable syntax alternatives :)

A CTE can be used to implement this in SQL 2008. See below.

DECLARE @patientTable TABLE (pat_id INT, visitdate DATE)

INSERT INTO @patientTable (pat_id, visitdate)
VALUES (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');

----SQL 2012 and beyond
--SELECT pt.pat_id,
--       pt.visitdate,
--       CASE WHEN pt.visitdate = MIN(pt.visitdate) OVER (PARTITION BY pt.pat_id ORDER BY pt.visitdate ASC)
--            THEN 1
--            ELSE 0
--       END AS IsFirstvisit
--FROM @patientTable AS pt;

--SQL 2008
;WITH PatientInitialVisits (PatientId, InitialVisitDate)
AS (SELECT pt.pat_id AS PatientId,
           MIN(pt.visitdate) AS InitialVisitDate
    FROM @patientTable AS pt
    GROUP BY pt.pat_id
   )
SELECT outerPt.pat_id,
       outerPt.visitdate,
       CASE WHEN piv.InitialVisitDate IS NOT NULL THEN 1
            ELSE 0
            END AS IsFirstVisit
FROM @patientTable AS outerPt
LEFT OUTER JOIN PatientInitialVisits AS piv ON outerPt.pat_id = piv.PatientId
                                           AND outerPt.visitdate = piv.InitialVisitDate;

Open in new window

0
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 41767144
Select pat_id, visit_date,
Case rn when 1 then 1 else 0 end as FirstVisit
From (
Select *, row_number() over (partition by pat_id order by visit_date) rn 
From PatientData) t1

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

863 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

18 Experts available now in Live!

Get 1:1 Help Now