我需要通过处方将RXBatch(Test1)中的Qty和Dayssupply添加到另一个Rxbatch(Test2),其中patid,facid和ndc匹配。一旦将qty和dayssuply从test1添加到test2,我需要test1数量和dayssuply为零。 tables
以下是我一直在研究的内容,但是我的脚本只为两个批次添加了数量,并且一旦添加了数量就不会将数量归零。
UPDATE RXS
SET QTY=RXS_Totals.Qty_Total
FROM FWDB.RX.RXS RXS
INNER JOIN (
SELECT FacID, PatID, NDC, SUM(Qty) AS Qty_Total
FROM FWDB.RX.RXS
WHERE RxBatch='test1' OR RxBatch='test2'
GROUP BY FacID, PatID, NDC
HAVING MIN(RxBatch) <> MAX(RxBatch)
) AS RXS_Totals ON RXS_Totals.FacID = RXS.FacID AND
RXS_Totals.PatID = RXS.PatID AND
RXS_Totals.NDC = RXS.NDC
答案 0 :(得分:1)
我认为您正在进行自我加入,如果是这样,您可以这样做:
UPDATE rx
SET
Qty = (CASE WHEN rx.RxBatch = rx1.RxBatch THEN 0 ELSE rx1.Qty + rx2.Qty END),
DaysSupply = (CASE WHEN rx.RxBatch = rx1.RxBatch THEN 0 ELSE rx1.DaysSupply + rx2.DaysSupply END)
FROM RXS rx
LEFT JOIN RXS rx1 ON rx1.RxNo = rx.RxNo AND rx1.RxBatch = 'TEST1'
LEFT JOIN RXS rx2 ON rx2.RxNo = rx.RxNo AND rx2.RxBatch = 'TEST2'
WHERE
rx1.PatID = rx2.PatID
AND rx1.FacID = rx2.FacID
AND rx1.NDC = rx2.NDC
答案 1 :(得分:0)
我认为你只需要:
UPDATE RXS
SET QTY = (CASE WHEN RxBatch = 'test2' THEN RXS_Totals.Qty_Total ELSE 0 END)
FROM . . .
WHERE RxBatch IN ('test1', 'test2');