Updating a table using Case statements in SQLHow can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?Insert results of a stored procedure into a temporary tableHow can I do an UPDATE statement with JOIN in SQL?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL table

How to show a landlord what we have in savings?

In Bayesian inference, why are some terms dropped from the posterior predictive?

How does a dynamic QR code work?

What do you call someone who asks many questions?

How to travel to Japan while expressing milk?

Theorists sure want true answers to this!

What is the fastest integer factorization to break RSA?

What reasons are there for a Capitalist to oppose a 100% inheritance tax?

What exactly is ineptocracy?

How could indestructible materials be used in power generation?

Does the Idaho Potato Commission associate potato skins with healthy eating?

Why didn't Boeing produce its own regional jet?

How to prevent "they're falling in love" trope

Why was Sir Cadogan fired?

Can a virus destroy the BIOS of a modern computer?

How do conventional missiles fly?

How to install cross-compiler on Ubuntu 18.04?

Can I hook these wires up to find the connection to a dead outlet?

Is there a hemisphere-neutral way of specifying a season?

How to compactly explain secondary and tertiary characters without resorting to stereotypes?

Forgetting the musical notes while performing in concert

Bullying boss launched a smear campaign and made me unemployable

Processor speed limited at 0.4 Ghz

What Exploit Are These User Agents Trying to Use?



Updating a table using Case statements in SQL


How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?Insert results of a stored procedure into a temporary tableHow can I do an UPDATE statement with JOIN in SQL?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL table













1















I am trying to add a 0, 1, or null to a column in a specific category where a relativepersonid of a person has a diagdate up to a person's servicedate. Here are my tables:



 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');


Here is my update statement:



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;


This works on some values, but there are some that don't get updated. I know the issue is with my case statement and probably a join issue. What is a better way to write this? Here is an example query I used to check. The PTSD column should have a 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


I did ask this question the other day, but my sample tables were all messed up, so I've verified that they work this time.










share|improve this question
























  • "This works on some values, but there are some that don't get updated" Ok, which ones does it work on, and which ones doesn't it? When something doesn't get updated, what are you expecting to update and to what?

    – Tab Alleman
    Mar 8 at 19:03











  • Welcome to SO. Your query, at least the case statements, seem fine. If there is a matter of rows not updating, it has to do with the joins or the where. I notice some null values on diagdate; maybe you want isnull(diagdate,'19000101') instead?

    – George Menoutis
    Mar 8 at 19:10















1















I am trying to add a 0, 1, or null to a column in a specific category where a relativepersonid of a person has a diagdate up to a person's servicedate. Here are my tables:



 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');


Here is my update statement:



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;


This works on some values, but there are some that don't get updated. I know the issue is with my case statement and probably a join issue. What is a better way to write this? Here is an example query I used to check. The PTSD column should have a 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


I did ask this question the other day, but my sample tables were all messed up, so I've verified that they work this time.










share|improve this question
























  • "This works on some values, but there are some that don't get updated" Ok, which ones does it work on, and which ones doesn't it? When something doesn't get updated, what are you expecting to update and to what?

    – Tab Alleman
    Mar 8 at 19:03











  • Welcome to SO. Your query, at least the case statements, seem fine. If there is a matter of rows not updating, it has to do with the joins or the where. I notice some null values on diagdate; maybe you want isnull(diagdate,'19000101') instead?

    – George Menoutis
    Mar 8 at 19:10













1












1








1








I am trying to add a 0, 1, or null to a column in a specific category where a relativepersonid of a person has a diagdate up to a person's servicedate. Here are my tables:



 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');


Here is my update statement:



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;


This works on some values, but there are some that don't get updated. I know the issue is with my case statement and probably a join issue. What is a better way to write this? Here is an example query I used to check. The PTSD column should have a 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


I did ask this question the other day, but my sample tables were all messed up, so I've verified that they work this time.










share|improve this question
















I am trying to add a 0, 1, or null to a column in a specific category where a relativepersonid of a person has a diagdate up to a person's servicedate. Here are my tables:



 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');


Here is my update statement:



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;


This works on some values, but there are some that don't get updated. I know the issue is with my case statement and probably a join issue. What is a better way to write this? Here is an example query I used to check. The PTSD column should have a 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


I did ask this question the other day, but my sample tables were all messed up, so I've verified that they work this time.







sql sql-server sql-server-2012 ssms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 21:10









Maciej S.

6711019




6711019










asked Mar 8 at 18:57









EmilyEmily

114




114












  • "This works on some values, but there are some that don't get updated" Ok, which ones does it work on, and which ones doesn't it? When something doesn't get updated, what are you expecting to update and to what?

    – Tab Alleman
    Mar 8 at 19:03











  • Welcome to SO. Your query, at least the case statements, seem fine. If there is a matter of rows not updating, it has to do with the joins or the where. I notice some null values on diagdate; maybe you want isnull(diagdate,'19000101') instead?

    – George Menoutis
    Mar 8 at 19:10

















  • "This works on some values, but there are some that don't get updated" Ok, which ones does it work on, and which ones doesn't it? When something doesn't get updated, what are you expecting to update and to what?

    – Tab Alleman
    Mar 8 at 19:03











  • Welcome to SO. Your query, at least the case statements, seem fine. If there is a matter of rows not updating, it has to do with the joins or the where. I notice some null values on diagdate; maybe you want isnull(diagdate,'19000101') instead?

    – George Menoutis
    Mar 8 at 19:10
















"This works on some values, but there are some that don't get updated" Ok, which ones does it work on, and which ones doesn't it? When something doesn't get updated, what are you expecting to update and to what?

– Tab Alleman
Mar 8 at 19:03





"This works on some values, but there are some that don't get updated" Ok, which ones does it work on, and which ones doesn't it? When something doesn't get updated, what are you expecting to update and to what?

– Tab Alleman
Mar 8 at 19:03













Welcome to SO. Your query, at least the case statements, seem fine. If there is a matter of rows not updating, it has to do with the joins or the where. I notice some null values on diagdate; maybe you want isnull(diagdate,'19000101') instead?

– George Menoutis
Mar 8 at 19:10





Welcome to SO. Your query, at least the case statements, seem fine. If there is a matter of rows not updating, it has to do with the joins or the where. I notice some null values on diagdate; maybe you want isnull(diagdate,'19000101') instead?

– George Menoutis
Mar 8 at 19:10












1 Answer
1






active

oldest

votes


















0














At first glance, the problem with your query is that you update the target (PatientFlags_w) multiple times: once for each flag. In some cases you seem to be ending up with the correct result, but its just by luck.



It's hard to tell if you want one row per person in the flag table, or one row per flag.



Can you review these queries and let us know if they are close to your desired results:



-- 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);


If the diagnoses are not related to each other (I assumed since they are in the same table), you might want this instead:



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;





share|improve this answer

























  • Hi, thank you for this. I just want a flag to be '1' if the relativepersonid had the diagnosis EVER, so I would just want one row per flag. The first query is closer to what I am wanting, but when I run it, it looks like i'm still getting false positives and vice versa.

    – Emily
    Mar 8 at 21:04












  • The first query is one row per person. Help me zero in on a false positive in the first query.

    – Nathan Skerl
    Mar 8 at 21:06











  • I'm sorry, you are correct. I just want one row per person. So if the person had all three diagnoses flagged as '1', I would just want one row.

    – Emily
    Mar 8 at 21:20











  • SELECT a.relativepersonid, a.anxietydisorder, a.depressivedisorder, a.ptsd, b.icdcode 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 JOIN Project..ICDCodes_w i ON b.ICDCode = i.PTSD; --only relativepersonid = 686967 should have PTSD = 1

    – Emily
    Mar 8 at 21:20












  • I added a third query that might get us closer. I had assumed the ICDcodes were related to each other

    – Nathan Skerl
    Mar 8 at 21:23











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069379%2fupdating-a-table-using-case-statements-in-sql%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














At first glance, the problem with your query is that you update the target (PatientFlags_w) multiple times: once for each flag. In some cases you seem to be ending up with the correct result, but its just by luck.



It's hard to tell if you want one row per person in the flag table, or one row per flag.



Can you review these queries and let us know if they are close to your desired results:



-- 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);


If the diagnoses are not related to each other (I assumed since they are in the same table), you might want this instead:



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;





share|improve this answer

























  • Hi, thank you for this. I just want a flag to be '1' if the relativepersonid had the diagnosis EVER, so I would just want one row per flag. The first query is closer to what I am wanting, but when I run it, it looks like i'm still getting false positives and vice versa.

    – Emily
    Mar 8 at 21:04












  • The first query is one row per person. Help me zero in on a false positive in the first query.

    – Nathan Skerl
    Mar 8 at 21:06











  • I'm sorry, you are correct. I just want one row per person. So if the person had all three diagnoses flagged as '1', I would just want one row.

    – Emily
    Mar 8 at 21:20











  • SELECT a.relativepersonid, a.anxietydisorder, a.depressivedisorder, a.ptsd, b.icdcode 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 JOIN Project..ICDCodes_w i ON b.ICDCode = i.PTSD; --only relativepersonid = 686967 should have PTSD = 1

    – Emily
    Mar 8 at 21:20












  • I added a third query that might get us closer. I had assumed the ICDcodes were related to each other

    – Nathan Skerl
    Mar 8 at 21:23















0














At first glance, the problem with your query is that you update the target (PatientFlags_w) multiple times: once for each flag. In some cases you seem to be ending up with the correct result, but its just by luck.



It's hard to tell if you want one row per person in the flag table, or one row per flag.



Can you review these queries and let us know if they are close to your desired results:



-- 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);


If the diagnoses are not related to each other (I assumed since they are in the same table), you might want this instead:



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;





share|improve this answer

























  • Hi, thank you for this. I just want a flag to be '1' if the relativepersonid had the diagnosis EVER, so I would just want one row per flag. The first query is closer to what I am wanting, but when I run it, it looks like i'm still getting false positives and vice versa.

    – Emily
    Mar 8 at 21:04












  • The first query is one row per person. Help me zero in on a false positive in the first query.

    – Nathan Skerl
    Mar 8 at 21:06











  • I'm sorry, you are correct. I just want one row per person. So if the person had all three diagnoses flagged as '1', I would just want one row.

    – Emily
    Mar 8 at 21:20











  • SELECT a.relativepersonid, a.anxietydisorder, a.depressivedisorder, a.ptsd, b.icdcode 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 JOIN Project..ICDCodes_w i ON b.ICDCode = i.PTSD; --only relativepersonid = 686967 should have PTSD = 1

    – Emily
    Mar 8 at 21:20












  • I added a third query that might get us closer. I had assumed the ICDcodes were related to each other

    – Nathan Skerl
    Mar 8 at 21:23













0












0








0







At first glance, the problem with your query is that you update the target (PatientFlags_w) multiple times: once for each flag. In some cases you seem to be ending up with the correct result, but its just by luck.



It's hard to tell if you want one row per person in the flag table, or one row per flag.



Can you review these queries and let us know if they are close to your desired results:



-- 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);


If the diagnoses are not related to each other (I assumed since they are in the same table), you might want this instead:



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;





share|improve this answer















At first glance, the problem with your query is that you update the target (PatientFlags_w) multiple times: once for each flag. In some cases you seem to be ending up with the correct result, but its just by luck.



It's hard to tell if you want one row per person in the flag table, or one row per flag.



Can you review these queries and let us know if they are close to your desired results:



-- 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);


If the diagnoses are not related to each other (I assumed since they are in the same table), you might want this instead:



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;






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 21:28

























answered Mar 8 at 20:39









Nathan SkerlNathan Skerl

7,54223049




7,54223049












  • Hi, thank you for this. I just want a flag to be '1' if the relativepersonid had the diagnosis EVER, so I would just want one row per flag. The first query is closer to what I am wanting, but when I run it, it looks like i'm still getting false positives and vice versa.

    – Emily
    Mar 8 at 21:04












  • The first query is one row per person. Help me zero in on a false positive in the first query.

    – Nathan Skerl
    Mar 8 at 21:06











  • I'm sorry, you are correct. I just want one row per person. So if the person had all three diagnoses flagged as '1', I would just want one row.

    – Emily
    Mar 8 at 21:20











  • SELECT a.relativepersonid, a.anxietydisorder, a.depressivedisorder, a.ptsd, b.icdcode 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 JOIN Project..ICDCodes_w i ON b.ICDCode = i.PTSD; --only relativepersonid = 686967 should have PTSD = 1

    – Emily
    Mar 8 at 21:20












  • I added a third query that might get us closer. I had assumed the ICDcodes were related to each other

    – Nathan Skerl
    Mar 8 at 21:23

















  • Hi, thank you for this. I just want a flag to be '1' if the relativepersonid had the diagnosis EVER, so I would just want one row per flag. The first query is closer to what I am wanting, but when I run it, it looks like i'm still getting false positives and vice versa.

    – Emily
    Mar 8 at 21:04












  • The first query is one row per person. Help me zero in on a false positive in the first query.

    – Nathan Skerl
    Mar 8 at 21:06











  • I'm sorry, you are correct. I just want one row per person. So if the person had all three diagnoses flagged as '1', I would just want one row.

    – Emily
    Mar 8 at 21:20











  • SELECT a.relativepersonid, a.anxietydisorder, a.depressivedisorder, a.ptsd, b.icdcode 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 JOIN Project..ICDCodes_w i ON b.ICDCode = i.PTSD; --only relativepersonid = 686967 should have PTSD = 1

    – Emily
    Mar 8 at 21:20












  • I added a third query that might get us closer. I had assumed the ICDcodes were related to each other

    – Nathan Skerl
    Mar 8 at 21:23
















Hi, thank you for this. I just want a flag to be '1' if the relativepersonid had the diagnosis EVER, so I would just want one row per flag. The first query is closer to what I am wanting, but when I run it, it looks like i'm still getting false positives and vice versa.

– Emily
Mar 8 at 21:04






Hi, thank you for this. I just want a flag to be '1' if the relativepersonid had the diagnosis EVER, so I would just want one row per flag. The first query is closer to what I am wanting, but when I run it, it looks like i'm still getting false positives and vice versa.

– Emily
Mar 8 at 21:04














The first query is one row per person. Help me zero in on a false positive in the first query.

– Nathan Skerl
Mar 8 at 21:06





The first query is one row per person. Help me zero in on a false positive in the first query.

– Nathan Skerl
Mar 8 at 21:06













I'm sorry, you are correct. I just want one row per person. So if the person had all three diagnoses flagged as '1', I would just want one row.

– Emily
Mar 8 at 21:20





I'm sorry, you are correct. I just want one row per person. So if the person had all three diagnoses flagged as '1', I would just want one row.

– Emily
Mar 8 at 21:20













SELECT a.relativepersonid, a.anxietydisorder, a.depressivedisorder, a.ptsd, b.icdcode 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 JOIN Project..ICDCodes_w i ON b.ICDCode = i.PTSD; --only relativepersonid = 686967 should have PTSD = 1

– Emily
Mar 8 at 21:20






SELECT a.relativepersonid, a.anxietydisorder, a.depressivedisorder, a.ptsd, b.icdcode 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 JOIN Project..ICDCodes_w i ON b.ICDCode = i.PTSD; --only relativepersonid = 686967 should have PTSD = 1

– Emily
Mar 8 at 21:20














I added a third query that might get us closer. I had assumed the ICDcodes were related to each other

– Nathan Skerl
Mar 8 at 21:23





I added a third query that might get us closer. I had assumed the ICDcodes were related to each other

– Nathan Skerl
Mar 8 at 21:23



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069379%2fupdating-a-table-using-case-statements-in-sql%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived