简体   繁体   中英

Selecting last occurence of row from table

I want to select the details of patient discharged on 2015-03-16 from discharge table and their corresponding admission details from admission table.

discharge table

在此处输入图片说明

admission table

在此处输入图片说明

these are the two table which is used in query.the row marked with red must be selected using query.

Query used:

SELECT a.pat_id, a.pat_name, b.admit_date, b.admit_time, b.diagnosis,
       b.Dr_name, a.dis_date, a.dis_time
from  discharge_details a , admission_Details b
where dis_date = Convert(Date, DATEADD(day, -1, GETDATE()))
  and a.pat_id = b.patient_id
  and b.Admit_status = 'discharged'

Output obtained:

在此处输入图片说明

Required output:

在此处输入图片说明

Please... help me with your suggestion to modify my query.

You can use the ROW_NUMBER() to get what you want. Since your Admit_Date gives the reason to believe that which row might be first and which one is last that's why add order by that.

 SELECT
     data.*
 FROM
     (
       SELECT 
           *, ROW_NUMBER() OVER(PARTITION BY pat_id ORDER BY admit_Date desc) as rn
       FROM
           table
     ) as data
 WHERE 
      data.rn = 1
 ORDER BY data.pat_id 

CREATE TABLE #temp
(id int,pat_id varchar(200), admit_Date  date)

INSERT into #temp
values
(1,'pat_123','2015-03-12'),
(2,'pat_245','2015-03-16'),
(3,'pat_123 ','2015-03-16')

The above query gives me,

   id    pat_id      admit_date  rn
    3   pat_123     2015-03-16    1
    2   pat_245     2015-03-16    1

OUTER APPLY s are perfect in scenarios like this:

SELECT  a.pat_id ,
        a.pat_name ,
        b.admit_date ,
        b.admit_time ,
        b.diagnosis ,
        b.Dr_name ,
        a.dis_date ,
        a.dis_time
FROM    discharge_details a 
        OUTER APPLY (SELECT TOP 1 * FROM admission_Details d WHERE a.pat_id = d.patient_id AND d.Admit_status = 'discharged' ORDER BY admit_date desc )b
WHERE   a.dis_date = CONVERT(DATE, DATEADD(DAY, -1, GETDATE()))

A few suggestions, if I may:

  1. Combine the date and time fields into one datetime field. Separating them like that causes no end of problems. What if a patient was admitted and discharged and then readmitted and discharged on the same day? It happens.
  2. Don't have patient data repeated in the discharge table. In fact, it shouldn't even be in the admission table. There should be a Patients table, and for that matter, a Doctors table, which contains names and other such attributes.
  3. Give each admission a surrogate primary key. The discharge would then contain the same key value of the associated admission. That way this discharge is definitely connected to that admission. The way you have it, we are doing little more than a qualified guess. Not good in databases.
  4. Attributes like ward_number and ward_type should also go into a separate table. What happens when patients move from one ward to another? Do you update the Admission table? Poor form -- you lose track of where the patient has been. Do you issue a discharge and readmission to the new ward? Too much unnecessary flailing of the database. Especially when you consider you are doing all that just to lie.
  5. Same advice for the Admit_Status field. Is that updated every time the status changes? You should track important changes such as this and be able to replicate every status change from initial admission to final discharge -- not just the new status but when it was made, which doctor approved and other important info.

With all that in mind, the way the tables are designed, you have to match a discharge record with the latest admission record for the patient that preceded the discharge -- and hope there were not two admissions on the same day. If that happens, you would need to get the time field in play and that complicates the query.

SQL Fiddle

declare
    @DischargeDate datetime;
set @DischargeDate = '2015-03-16';

select  ad.Patient_ID, ad.Patient_Name, ad.Admit_Date, ad.Diagnosis,
        ad.Dr_Name, dd.Dis_Date
from    Discharge_Details dd
join    Admission_Details ad
    on  ad.Patient_ID = dd.Pat_ID
    and ad.Admit_Date =(
            select  Max( Admit_Date )
            from    Admission_Details
            where   Patient_ID = dd.Pat_ID
                and Admit_Date < dd.dis_date)
where   dd.Dis_Date >= @DischargeDate
    and dd.Dis_Date < DateAdd( day, 1, @DischargeDate );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM