Solved

Query syntax

Posted on 2016-08-23
4
112 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 13

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 13

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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…
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
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

860 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