Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<big>patientno	VisitDate	NextVisitDate</big>
210	1/1/2001	5/1/2001
210	5/1/2001	9/1/2001
210	9/1/2001	NotFound
211	1/1/2001	5/1/2001
211	5/1/2001	9/1/2001
211	9/1/2001	NotFound

i want to Show next visit date for each patient with current visitdate

i have only two columns 1,patientno , 2nd visitdate how show nextvisitdate

Select Patientno,visitdate (nextvisitdate) from hospital
Posted
Updated 23-Dec-13 20:13pm
v2
Comments
Ganesh Raja 24-Dec-13 2:31am    
If u have 2 columns then what you need to show in nextvisitdate?
its a current(getdate()) date or some other?
Christian Graus 24-Dec-13 3:47am    
What you need to be clear on is, where does next visit date come from ? Does the system assume that someone only has ONE future visit date ? If so, this is clearly homework and not a real world system. Either way, you should not design it like this, the design is broken. All visits should be visits, with a flag to say if the person showed up. Appointments don't work on the basis of the format you're showing, not anywhere.

Test it:
SQL
DECLARE @tbl TABLE (PatientNo INT, VisitDate DATETIME)

INSERT INTO @tbl (PatientNo, VisitDate)
VALUES(210, '1/1/2001'),
(210, '5/1/2001'),
(210, '9/1/2001'),
(211, '1/1/2001'),
(211, '5/1/2001'),
(211, '9/1/2001')


SELECT t1.PatientNo, t1.Rowno, MAX(t1.VisitDate) AS VisitDate, MAX(t2.VisitDate) AS NextVisitDate
FROM (
    SELECT ROW_NUMBER() OVER(PARTITION BY PatientNo ORDER BY PatientNo, VisitDate) AS RowNo, PatientNo, VisitDate
    FROM @tbl
    ) AS t1 LEFT JOIN (
        SELECT ROW_NUMBER() OVER(PARTITION BY PatientNo ORDER BY PatientNo, VisitDate) -1 AS RowNo, PatientNo, VisitDate
        FROM @tbl
        ) AS t2 ON t1.RowNo = t2.RowNo
GROUP BY t1.PatientNo, t1.RowNo
ORDER BY t1.PatientNo, t1.RowNo
 
Share this answer
 
Comments
saimm 24-Dec-13 2:36am    
its really working

so much thanks
Maciej Los 24-Dec-13 2:39am    
You're welcome ;)
Member 10434230 24-Dec-13 2:42am    
the values being added in temp table, please make it dynamic and get it from hospital table
Maciej Los 24-Dec-13 2:43am    
It's just an example ;)
I would imagine you want to do a select where the NextVisitDate > getdate(), to ensure you don't get back 'next visit dates' that are in the past, because people don't have a future visit date.

It makes no sense to order by patientno if you're partitioning by patientno, the order should just be the date. I also don't see why you need a windowing function here:

select patientno, max(visitdate), case when max(visitdate) > getdate() then max(visitdate) else null end
from myTable group by patientNo

is what i think would do it ( but I am booted to Mac right now, so I can't test it ). If that did not work, I'd use CTEs to find the two values I need and go from there.
 
Share this answer
 
Comments
Maciej Los 24-Dec-13 3:44am    
Christian, i think OP wants to get existing 'visit dates' and to arrange them in a tabular form, eg.:
visit date 1 | visit date 2
visit date 2 | visit date 3

etc.
Please, see my comment to Solution 1.
Christian Graus 24-Dec-13 3:46am    
The OP marked solution 2 as correct, so I think they just want the max values. One other issue, is that people may have one more future visit booked. Really, the business rules need to be laid out clearer for a solution to be offered. I suspect what's needed is a CTE that pulls out visits in the future, and THEN gets the min, not the max, for the NEXT visit.
Maciej Los 24-Dec-13 3:56am    
It's quite different requirement, i think ;)
Is it possible to book more than one visit in a future?
I know you're working for medicine bussines ;)
Christian Graus 24-Dec-13 3:58am    
I don't work for vets anymore, actually, but I know my wife sometimes books her specialist visits a year at a time. It's not uncommon for people who have to visit regularly, to do that, and people who don't need to, just make ad hoc appointments. Why would the DB need to store my next appt next to my last one, when they are 2 years apart, and after this, it might turn out I don't come back for 3 1/2 ?
Maciej Los 24-Dec-13 4:16am    
The difference is that the OP wants to work on "dates in a past", but you want to operate on "dates in a future"...
Sorry, my English isn't perfect.
If you are using SQL Server 2012, this is much nicer and works fine:

SQL
SELECT patientno, visitdate, lead(visitdate, 1, null) over (PARTITION BY patientno ORDER BY visitdate)
FROM visits v
 
Share this answer
 
Try this:
//I have taken 4th day from visit date as next date, you can change as per your need
Select Patientno,visitdate, DATEADD(day,4,visitdate) as nextvisitdate from hospital
 
Share this answer
 
Comments
saimm 24-Dec-13 2:35am    
Thanks, But there is not fix
Maciej Los 24-Dec-13 2:36am    
What???
Please, read question carefully. The question is: How to get visit date and next visit date from visitdate field?
Member 10434230 24-Dec-13 2:40am    
Ooops.... sorry i missed it. Thanks for bringing this up.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900