Updating a table using Case statements in SQL Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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
old style "caution" boxes
Can anything be seen from the center of the Boötes void? How dark would it be?
Is "Reachable Object" really an NP-complete problem?
If a contract sometimes uses the wrong name, is it still valid?
How to tell that you are a giant?
How would a mousetrap for use in space work?
If my PI received research grants from a company to be able to pay my postdoc salary, did I have a potential conflict interest too?
Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?
Why are both D and D# fitting into my E minor key?
Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?
Is safe to use va_start macro with this as parameter?
How to find all the available tools in mac terminal?
If a VARCHAR(MAX) column is included in an index, is the entire value always stored in the index page(s)?
What do you call the main part of a joke?
Do square wave exist?
Most bit efficient text communication method?
Why aren't air breathing engines used as small first stages
How do I find out the mythology and history of my Fortress?
What is homebrew?
Do wooden building fires get hotter than 600°C?
Circuit to "zoom in" on mV fluctuations of a DC signal?
How to react to hostile behavior from a senior developer?
What is the meaning of the new sigil in Game of Thrones Season 8 intro?
How to convince students of the implication truth values?
Updating a table using Case statements in SQL
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
add a comment |
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
"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
add a comment |
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
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
sql sql-server sql-server-2012 ssms
edited Mar 8 at 21:10
Maciej S.
7021019
7021019
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
add a comment |
"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
add a comment |
1 Answer
1
active
oldest
votes
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;
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
|
show 3 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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
|
show 3 more comments
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;
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
|
show 3 more comments
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;
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;
edited Mar 8 at 21:28
answered Mar 8 at 20:39
Nathan SkerlNathan Skerl
7,56223049
7,56223049
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
|
show 3 more comments
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
|
show 3 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
"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