Select max from several columns

Mrniceguy

I'm writing code to select patients that die within 30 days of a hospital discharge, my issue is that when I have a patient with multiple discharges within that 30 day tolerance it pulls back multiple rows! I've tried to solve this using max discharge date, which worked, yet when I add extra columns it seems to pull certain elements from other rows. Here is my code:

    SELECT  MAX(IPS.disch_dttm)               [Discharge Datetime]
            ,MAX(IPS.IP_SPELL_ID)             [Spell ID]    
            ,pat.PAS_ID                       [K Number]
            ,MAX(IPS.DIS_WARD_ID)             [Ward ID]
            ,DSSU.SU_DESCRIPTION              [Discharging Ward]

    FROM Pat_spell AS IPS
    LEFT JOIN PATIENT PAT       WITH (NOLOCK) ON PAT.DIM_PATIENT_ID = IPS.DIM_PATIENT_ID
    LEFT JOIN SPECIALTY SPEC    WITH (NOLOCK) ON SPEC.DIM_SPECIALTY_ID = IPS.DIM_DIS_SPECT_ID
    LEFT JOIN SERVICE_UNIT DSSU WITH (NOLOCK) ON IPS.DIM_DIS_WARD_ID = DSSU.DIM_SSU_ID

    WHERE (IPS.DISCH_DTTM <= PAT.DEATH_DTTM + 30)
    AND IPS.DIM_DIS_SPECT_ID = '7195'
    AND IPS.DISCH_DTTM BETWEEN '01/01/2014' AND '30/06/2014'

    GROUP BY pat.PAS_ID
            ,pat.DEATH_DTTM
            ,IPS.DIM_PATIENT_ID
            ,DSSU.SSU_DESCRIPTION

    ORDER BY pat.PAS_ID        

Here is output from the above code for a single row that I've been using to debug:

Disch Date  Event_ID   Unique ID  Ward ID   Discharging Ward
2014-06-14  8366113    A123456   77085       WardA 

The above gets the ward ID correct, but the "Discharging Ward" is wrong. Also the Event_ID corresponds with a previous attendance. What I'm trying to achieve is to pull only the most recent event within 30 days of a time of death, with 'Event ID' as my unique ID. This is what the output would look like if I wanted multiple rows:

  Disch Date  Event ID  Unique ID    Ward ID    Discharging Ward
1 2014-06-14    8208846  A123456       77085     Ward B       
2 2014-05-16    8366113  A123456       77036     Ward A

This is what my output should look like:

  Disch Date  Event_ID   Unique ID  Ward ID   Discharging Ward
2014-06-14  8208846    A123456   77085      Ward B 

So to sum up, my code pulls the correct "discharge date", the correct "Ward ID" but seems to pull the rest from other rows in the table. Apologies for the huge ask - any help would be appreciated, or if this has been explored to death, please point me in the right direction.

laughsloudly

A simplified version of what you need looks like this...

SELECT  [DETAIL INFO, no need to MAX or GROUP BY]
FROM Pat_spell AS IPS
LEFT JOIN PATIENT PAT       WITH (NOLOCK) ON PAT.DIM_PATIENT_ID = IPS.DIM_PATIENT_ID
LEFT JOIN SPECIALTY SPEC    WITH (NOLOCK) ON SPEC.DIM_SPECIALTY_ID = IPS.DIM_DIS_SPECT_ID
LEFT JOIN SERVICE_UNIT DSSU WITH (NOLOCK) ON IPS.DIM_DIS_WARD_ID = DSSU.DIM_SSU_ID
INNER JOIN (
    SELECT PatientID, MAX(IPS.disch_dttm) AS DischargeDt
    FROM [AllMyTables]
    WHERE (IPS.DISCH_DTTM <= PAT.DEATH_DTTM + 30)
    AND IPS.DIM_DIS_SPECT_ID = '7195'
    AND IPS.DISCH_DTTM BETWEEN '01/01/2014' AND '30/06/2014'
) t1 ON PAT.PatientID = t1.PatientID AND IPS.disch_dttm = t1.DischargeDt
ORDER BY pat.PAS_ID

Since the INNER SQL is returning 1 row per patient, there is no need to group on the OUTER SQL.

If I had more data to work with, I might be able to stitch together the full SQL, but maybe this points you in the right direction.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL SELECT id of row where GREATEST of MAX entries of several columns

From Dev

selecting average value from group of same with max on another for several columns

From Dev

MYSQL: Select MAX date from multiple columns

From Dev

MYSQL SELECT MAX from two columns

From Dev

select multiple columns with max from one column and distinct on another

From Dev

Select max value from column for every value in other two columns

From Dev

select max version over several fields

From Dev

How can I select unique values from several columns in Oracle SQL?

From Dev

Select rows from dataframe with same values on several columns but different value on another

From Dev

Pandas: Select Rows with condition for several columns

From Dev

Select from several partitions at once

From Dev

Max of a column (column 2) from several dfs

From Dev

How to select rows from MySQL based on max value of a one column and grouping two other columns?

From Dev

LINQ query to select records from table where 2 columns have max values

From Dev

How to select the max date with additional columns

From Dev

How to select row values for max(2 columns)

From Dev

SQL select max year/month by separate columns

From Dev

Laravel Eloquent - Select MAX with other columns

From Dev

SQL Select max value by grouping two columns

From Dev

select max sum of records in two columns

From Dev

Select min and max for each row with multiple columns

From Dev

SQL select max year/month by separate columns

From Dev

Select MAX, but want to show the other columns

From Dev

Select records using max values for two columns

From Dev

MySQL select distinct and max with all columns

From Dev

Select max date and max time from query

From Dev

Select IN from multiple columns

From Dev

Select several max types for each datatype per distinct value in mysql

From Dev

Generating Rows from Values in Several Columns

Related Related

  1. 1

    MySQL SELECT id of row where GREATEST of MAX entries of several columns

  2. 2

    selecting average value from group of same with max on another for several columns

  3. 3

    MYSQL: Select MAX date from multiple columns

  4. 4

    MYSQL SELECT MAX from two columns

  5. 5

    select multiple columns with max from one column and distinct on another

  6. 6

    Select max value from column for every value in other two columns

  7. 7

    select max version over several fields

  8. 8

    How can I select unique values from several columns in Oracle SQL?

  9. 9

    Select rows from dataframe with same values on several columns but different value on another

  10. 10

    Pandas: Select Rows with condition for several columns

  11. 11

    Select from several partitions at once

  12. 12

    Max of a column (column 2) from several dfs

  13. 13

    How to select rows from MySQL based on max value of a one column and grouping two other columns?

  14. 14

    LINQ query to select records from table where 2 columns have max values

  15. 15

    How to select the max date with additional columns

  16. 16

    How to select row values for max(2 columns)

  17. 17

    SQL select max year/month by separate columns

  18. 18

    Laravel Eloquent - Select MAX with other columns

  19. 19

    SQL Select max value by grouping two columns

  20. 20

    select max sum of records in two columns

  21. 21

    Select min and max for each row with multiple columns

  22. 22

    SQL select max year/month by separate columns

  23. 23

    Select MAX, but want to show the other columns

  24. 24

    Select records using max values for two columns

  25. 25

    MySQL select distinct and max with all columns

  26. 26

    Select max date and max time from query

  27. 27

    Select IN from multiple columns

  28. 28

    Select several max types for each datatype per distinct value in mysql

  29. 29

    Generating Rows from Values in Several Columns

HotTag

Archive