繁体   English   中英

在SQL中使用Case语句更新表

[英]Updating a table using Case statements in SQL

我正在尝试将0、1或null添加到特定类别的列中,其中某personrelativepersonid person具有与该personservicedate diagdate的日期。 这是我的桌子:

 DROP TABLE ICDCodes_w;
 GO
 CREATE TABLE ICDCodes_w
(
    AnxietyDisorder VARCHAR(6),
    DepressiveDisorder VARCHAR(6),
    PTSD VARCHAR(6)
);

INSERT INTO ICDCodes_w
(
    AnxietyDisorder,
    DepressiveDisorder,
    PTSD
)
VALUES
('293.84', '296.2', '309.81'),
('300', '296.21', 'F43.1'),
('305.42', 'F11.28', 'F31.76'),
('305.81', 'F43.8', 'F31.78'),
('F40.00', 'F43.10', '305.52');
GO
DROP TABLE DiagHX_w;
GO
CREATE TABLE DiagHX_w
(
    ArchiveID VARCHAR(10),
    RelativePersonID VARCHAR(10),
    ICDCode VARCHAR(6), 
    DiagDate DATE 
);

INSERT INTO DiagHX_w
(
    ArchiveID,
    RelativePersonID,
    ICDCode, 
    DiagDate
)
VALUES
('1275741', '754241', '293.84', '1989-01-03'),
('2154872', '754241', '293.84', '1995-04-07'),
('4587215', '754241', '998.4', '1999-12-07'),
('4588775', '711121', 'F11.28', '2001-02-07'),
('3545455', '711121', NULL, NULL),
('9876352', '323668', '400.02', '1988-04-09'),
('3211514', '112101', 'F31.78', '2005-09-09'),
('3254548', '686967', 'F40.00', '1999-12-31'),
('4411144', '686967', '305.52', '2000-01-01'),
('6548785', '99999999','F40.00', '2000-02-03');
GO
DROP TABLE PatientFlags_w;
GO
CREATE TABLE PatientFlags_w
(
    PersonID VARCHAR(10),
    RelativePersonID VARCHAR(10),
    AnxietyDisorder VARCHAR(2),
    DepressiveDisorder VARCHAR(2),
    PTSD VARCHAR(2),
);

INSERT INTO PatientFlags_w
(
    PersonID,
    RelativePersonID
)
VALUES
('99999999', '754241'),
('88888888', '754241'),
('77777777', '754241'),
('66666666', '711121'),
('55555555', '711121'),
('44444444', '323668'),
('33333333', '112101'),
('22222222', '686967'),
('11111111', '686967'),
('32151111', '887878'),
('78746954', '771125'),
('54621333', '333114'),
('55648888', '333114');
GO
DROP TABLE Person_w;
GO
CREATE TABLE Person_w
(
    PersonID VARCHAR(10),
    ServiceDate date
);

INSERT INTO Person_w
(
    PersonID,
    ServiceDate
)
VALUES
('99999999', '2000-12-31'),
('88888888', '2000-11-01'),
('69876541', '2000-09-04'),
('66666666', '2000-01-15'),
('55555555', '2000-07-22'),
('44444444', '2000-07-20'),
('65498711', '2000-11-17'),
('22222222', '2000-09-02'),
('11111111', '2000-02-04'),
('32151111', '2000-02-17'),
('78746954', '2000-03-29'),
('54621333', '2000-08-22'),
('55648888', '2000-10-20');

这是我的更新声明:

UPDATE a
SET AnxietyDisorder = CASE
                      WHEN ICDCode IN
                           (
                               SELECT AnxietyDisorder FROM 
                               Project..ICDCodes_w
                           ) THEN
                          1
                      ELSE
                          0
                  END,
DepressiveDisorder = CASE
                         WHEN ICDCode IN
                              (
                                  SELECT DepressiveDisorder FROM 
                                  Project..ICDCodes_w
                              ) THEN
                             1
                         ELSE
                             0
                     END,
PTSD = CASE
           WHEN ICDCode IN
                (
                    SELECT PTSD FROM Project..ICDCodes_w
                ) THEN
               1
           ELSE
               0
       END
FROM PatientFlags_w a
JOIN DiagHX_w b
    ON a.relativepersonid = b.RelativePersonID
JOIN Person_w p 
ON a.personid = p.PersonID
WHERE diagdate <= p.servicedate; 

这适用于某些值,但有些值不会更新。 我知道问题出在我的案例陈述中,并且可能是联接问题。 有什么更好的写方法? 这是我用来检查的示例查询。 PTSD列应为1。

SELECT * FROM project..patientflags_w a 
JOIN project..diaghx_w b 
ON a.relativepersonid = b.RelativePersonID
JOIN project..person_w p 
ON a.personid = p.personid 
WHERE b.icdcode IN (SELECT PTSD FROM Project..ICDCodes_w) 
AND b.diagdate <= p.servicedate 

前几天我确实问了这个问题,但是我的样本表都弄糟了,所以我已经证实它们这次可以工作了。

乍一看,查询的问题是您多次更新目标(PatientFlags_w):每个标志一次。 在某些情况下,您似乎最终会获得正确的结果,但这只是运气。

很难说您是要在标志表中每人一行,还是要每个标志一行。

您能否查看这些查询,并让我们知道它们是否接近您的期望结果:

-- If you want one row per Person:
select  RelativePersonID, 
        [AnxietyDisorder] = max(case when c.AnxietyDisorder is not null then 1 else 0 end),
        [DepressiveDisorder] = max(case when c.DepressiveDisorder is not null then 1 else 0 end),
        [PTSD] = max(case when c.PTSD is not null then 1 else 0 end)
from    DiagHX_w d
left 
join    ICDCodes_w c on d.ICDCode in (c.AnxietyDisorder, c.DepressiveDisorder, c.PTSD)
group
by      RelativePersonID;


-- If you want one row per Flag:
select  RelativePersonID, 
        d.ICDCode,
        [AnxietyDisorder] = case when c.AnxietyDisorder is not null then 1 else 0 end,
        [DepressiveDisorder] = case when c.DepressiveDisorder is not null then 1 else 0 end,
        [PTSD] = case when c.PTSD is not null then 1 else 0 end
from    DiagHX_w d
left 
join    ICDCodes_w c on d.ICDCode in (c.AnxietyDisorder, c.DepressiveDisorder, c.PTSD);

如果诊断彼此不相关(我猜因为它们在同一张表中,所以我认为是),您可能需要这样做:

select  RelativePersonID, 
        [AnxietyDisorder] = max(case when c.AnxietyDisorder = d.ICDCode then 1 else 0 end),
        [DepressiveDisorder] = max(case when c.DepressiveDisorder = d.ICDCode then 1 else 0 end),
        [PTSD] = max(case when c.PTSD = d.ICDCode then 1 else 0 end)
from    DiagHX_w d
left 
join    ICDCodes_w c on d.ICDCode in (c.AnxietyDisorder, c.DepressiveDisorder, c.PTSD)
group
by      RelativePersonID;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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