简体   繁体   中英

SQL Server join statement

I want to look at every row in Examinations , and for Batra , I want to update that column in Patients IF the value is 1 or 2. A row for each patient already exists in Patients .

Examinations :

|ExaminationId | PatientId |    Batra |
---------------------------------------
|        12345 |       123 |        2 |
|        54321 |       123 |        1 |
|        98765 |       123 |        0 |

Patients :

|PatientId |    Batra |
-----------------------
|      123 |        0 |

The following is the result I want after performing this operation.

|PatientId | Batra|
-------------------
|      123 |    1 |

I have tried to produce some JOIN statement but I fail.

Edit: Something like this

UPDATE Patients
SET Patients.Batra = Examinations.Batra
WHERE Examinations.Batra = 1
OR Examinations.Batra = 2

Edit 2: It does not have to be set to 1. If 2 were the last value, it would be set to 2. Like this:

foreach(row in Examinations){
   if(row.Batra == 1 || row.Batra == 2){
      Patients.Batra = row.Batra
   }
}

If I understand you correctly, you want something like this:

UPDATE
    Patients
SET
    Batra=em.batra
FROM
    Patients AS p
    INNER JOIN (
        SELECT
            e.patientid,
            MAX(e.batra) AS batra
        FROM
            Examinations AS e
        GROUP BY
            e.patientid
    ) AS em ON
        em.patientid=p.patientid;        

I see you already got an answer, but this will update Patients with the latest (according to ExaminationId) Batra from examinations;

WITH cte AS (
  SELECT patientid, batra, 
    ROW_NUMBER() OVER (PARTITION BY PatientId ORDER BY ExaminationId DESC) rn
  FROM Examinations
  WHERE Batra IN (1,2)
)
UPDATE p
SET p.Batra = c.Batra
from patients p
JOIN cte c
  ON p.patientid = c.patientid
 AND c.rn = 1

The common table expression will rank the examinations with a 1 or 2 value for batra per patient by id, and the update will just use the latest value for each patient to update patients .

You should use exists() function -

UPDATE Patients p
set batra = 1
where exists(select * from examinations e where e.patientid = p.patientid and e.barta in(1,2))

This checks if a batra with value 1 or 2 exists in examinations table and update the record in patient if there is.

Go with simple join and fix where condition / updates ... but this is something you want

Update pat
    set Batra = exam.Batra
from patients pat
join Examinations exam on pat.patientID = exam.patientID
where exam.Batra in (1,2)

Try something like this Query

UPDATE p
SET p.Batra = e.Batra
FROM Patients p
INNER JOIN Examinition e ON p.PatientId = e.PatientId 
WHERE e.Batra IN (1,2)

This solution will find and update with the Batra value which is different from 0 and has the highest ExaminationId.

UPDATE
  p
SET
    Batra=em.batra
FROM
   Patients AS p
CROSS APPLY 
  (
    SELECT top 1
      e.batra
    FROM
      Examinations e
    WHERE
      e.patientid=p.patientid
      AND e.batra in (1,2)
    ORDER BY ExaminationId DESC
  ) AS em

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