Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query syntax

Posted on 2016-08-23
4
Medium Priority
?
129 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 14

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 14

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 1000 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 41

Accepted Solution

by:
Sharath earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

916 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