简体   繁体   中英

How do I get the value associated with a MIN or MAX

I'm in the middle of creating a query and have it where I need the other values, however I am pulling a MIN and MAX date for individual patient_id . I'm wondering how I would go about how I would pull a value associated with that MIN or MAX date as well? I'm looking for a value the column provider_id which will show which doctor they saw on that MIN or MAX date. Here is what I have so far:

WITH test AS (
SELECT  patient_id, 
        clinic, 
        SUM(amount) AS production,
        MIN(tran_date) AS first_visit, 
        MAX(tran_date) AS last_visit
FROM transactions
WHERE impacts='P'
GROUP BY patient_id, clinic)

SELECT  w.patient_id, 
        w.clinic, 
        p.city, 
        p.state, 
        p.zipcode, 
        p.sex, 
        w.production,
        w.first_visit, 
        w.last_visit
FROM test w
LEFT JOIN patient p
    ON (w.patient_id=p.patient_id AND w.clinic=p.clinic)

I believe that this will get what you're looking for:

;WITH CTE_Transactions AS (
    SELECT DISTINCT
        patient_id, 
        clinic, 
        SUM(amount) OVER (PARTITION BY patient_id, clinic) AS production,
        FIRST_VALUE(tran_date) OVER (PARTITION BY patient_id, clinic ORDER BY tran_date) AS first_visit,
        FIRST_VALUE(provider_id) OVER (PARTITION BY patient_id, clinic ORDER BY tran_date) AS first_provider_id,
        LAST_VALUE(tran_date) OVER (PARTITION BY patient_id, clinic ORDER BY tran_date) AS last_visit,
        LAST_VALUE(provider_id) OVER (PARTITION BY patient_id, clinic ORDER BY tran_date) AS last_provider_id,
        ROW_NUMBER() OVER (PARTITION BY patient_id, clinic ORDER BY tran_date) AS row_num
FROM Transactions
WHERE impacts='P'
)
SELECT
    w.patient_id, 
    w.clinic, 
    p.city, 
    p.state, 
    p.zipcode, 
    p.sex, 
    w.production,
    w.first_visit, 
    w.last_visit
FROM
    CTE_Transactions W
LEFT JOIN Patient P ON
    W.patient_id = P.patient_id AND
    W.clinic = P.clinic
INNER JOIN Provider FIRST_PROV ON
    FIRST_PROV.provider_id = W.first_provider_id
INNER JOIN Provider LAST_PROV ON
    LAST_PROV.provider_id = W.last_provider_id
WHERE
    W.row_num = 1

I assume you are referring to the CTE. You can use conditional aggregation along with window functions. For instance, to get the amount for the first visit:

WITH test AS (
      SELECT patient_id, clinic, 
             SUM(amount) AS production,
             MIN(tran_date) AS first_visit, 
             MAX(tran_date) AS last_visit,
             SUM(CASE WHEN tran_date = min_tran_date THEN amount END) as first_amount
     FROM (SELECT t.*,
                  MIN(trans_date) OVER (PARTITION BY patient_id, clinic) as min_tran_date
           FROM transactions
           WHERE impacts = 'P'
          ) t
     GROUP BY patient_id, clinic
    )

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