postgresql case statement get another column value












0















I have three tables: intrusion, alarms and CCTVs and I would like to use a case statement to obtain the registration_number of the object and its location, depending on whether it was a CCTV or an alarm that triggered an intrusion event in the intrusion table.



This is the intrusion table:



enter image description here



This is the alarm table:
enter image description here



The CCTV table is similar to the alarm table and this is my code:



SELECT
ALARM_ID
, CCTV_ID
, CASE
WHEN
ALARM_ID IS NULL
AND CCTV_ID IS NOT NULL
THEN
(
SELECT
REGISTRATION_NUMBER, LOCATION
FROM
REMOTE_SECURITY.ALARMS
WHERE
REMOTE_SECURITY.INTRUSIONS.ALARM_ID = REMOTE_SECURITY.ALARMS.ALARM_ID
)
WHEN
ALARM_ID IS NOT NULL
AND CCTV_ID IS NULL
THEN
(
SELECT
REGISTRATION_NUMBER, LOCATION
FROM
REMOTE_SECURITY.CCTVS
WHERE
REMOTE_SECURITY.INTRUSIONS.cctv_id = REMOTE_SECURITY.CCTVS.CCTV_ID
)
ELSE
'not running'
END
FROM
REMOTE_SECURITY.INTRUSIONS


I would like the final output to be a table with 2 columns: Location and Registration_number. (I dont need to know whether its a cctv or an alarm)



Any help would be greatly appreciated!










share|improve this question




















  • 1





    some sample data and expected output for this data would be good.

    – Radim Bača
    Nov 22 '18 at 21:15











  • Check your answer

    – Peter Krauss
    Nov 22 '18 at 23:03
















0















I have three tables: intrusion, alarms and CCTVs and I would like to use a case statement to obtain the registration_number of the object and its location, depending on whether it was a CCTV or an alarm that triggered an intrusion event in the intrusion table.



This is the intrusion table:



enter image description here



This is the alarm table:
enter image description here



The CCTV table is similar to the alarm table and this is my code:



SELECT
ALARM_ID
, CCTV_ID
, CASE
WHEN
ALARM_ID IS NULL
AND CCTV_ID IS NOT NULL
THEN
(
SELECT
REGISTRATION_NUMBER, LOCATION
FROM
REMOTE_SECURITY.ALARMS
WHERE
REMOTE_SECURITY.INTRUSIONS.ALARM_ID = REMOTE_SECURITY.ALARMS.ALARM_ID
)
WHEN
ALARM_ID IS NOT NULL
AND CCTV_ID IS NULL
THEN
(
SELECT
REGISTRATION_NUMBER, LOCATION
FROM
REMOTE_SECURITY.CCTVS
WHERE
REMOTE_SECURITY.INTRUSIONS.cctv_id = REMOTE_SECURITY.CCTVS.CCTV_ID
)
ELSE
'not running'
END
FROM
REMOTE_SECURITY.INTRUSIONS


I would like the final output to be a table with 2 columns: Location and Registration_number. (I dont need to know whether its a cctv or an alarm)



Any help would be greatly appreciated!










share|improve this question




















  • 1





    some sample data and expected output for this data would be good.

    – Radim Bača
    Nov 22 '18 at 21:15











  • Check your answer

    – Peter Krauss
    Nov 22 '18 at 23:03














0












0








0








I have three tables: intrusion, alarms and CCTVs and I would like to use a case statement to obtain the registration_number of the object and its location, depending on whether it was a CCTV or an alarm that triggered an intrusion event in the intrusion table.



This is the intrusion table:



enter image description here



This is the alarm table:
enter image description here



The CCTV table is similar to the alarm table and this is my code:



SELECT
ALARM_ID
, CCTV_ID
, CASE
WHEN
ALARM_ID IS NULL
AND CCTV_ID IS NOT NULL
THEN
(
SELECT
REGISTRATION_NUMBER, LOCATION
FROM
REMOTE_SECURITY.ALARMS
WHERE
REMOTE_SECURITY.INTRUSIONS.ALARM_ID = REMOTE_SECURITY.ALARMS.ALARM_ID
)
WHEN
ALARM_ID IS NOT NULL
AND CCTV_ID IS NULL
THEN
(
SELECT
REGISTRATION_NUMBER, LOCATION
FROM
REMOTE_SECURITY.CCTVS
WHERE
REMOTE_SECURITY.INTRUSIONS.cctv_id = REMOTE_SECURITY.CCTVS.CCTV_ID
)
ELSE
'not running'
END
FROM
REMOTE_SECURITY.INTRUSIONS


I would like the final output to be a table with 2 columns: Location and Registration_number. (I dont need to know whether its a cctv or an alarm)



Any help would be greatly appreciated!










share|improve this question
















I have three tables: intrusion, alarms and CCTVs and I would like to use a case statement to obtain the registration_number of the object and its location, depending on whether it was a CCTV or an alarm that triggered an intrusion event in the intrusion table.



This is the intrusion table:



enter image description here



This is the alarm table:
enter image description here



The CCTV table is similar to the alarm table and this is my code:



SELECT
ALARM_ID
, CCTV_ID
, CASE
WHEN
ALARM_ID IS NULL
AND CCTV_ID IS NOT NULL
THEN
(
SELECT
REGISTRATION_NUMBER, LOCATION
FROM
REMOTE_SECURITY.ALARMS
WHERE
REMOTE_SECURITY.INTRUSIONS.ALARM_ID = REMOTE_SECURITY.ALARMS.ALARM_ID
)
WHEN
ALARM_ID IS NOT NULL
AND CCTV_ID IS NULL
THEN
(
SELECT
REGISTRATION_NUMBER, LOCATION
FROM
REMOTE_SECURITY.CCTVS
WHERE
REMOTE_SECURITY.INTRUSIONS.cctv_id = REMOTE_SECURITY.CCTVS.CCTV_ID
)
ELSE
'not running'
END
FROM
REMOTE_SECURITY.INTRUSIONS


I would like the final output to be a table with 2 columns: Location and Registration_number. (I dont need to know whether its a cctv or an alarm)



Any help would be greatly appreciated!







postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 21:13







Amoroso

















asked Nov 22 '18 at 21:05









AmorosoAmoroso

1591212




1591212








  • 1





    some sample data and expected output for this data would be good.

    – Radim Bača
    Nov 22 '18 at 21:15











  • Check your answer

    – Peter Krauss
    Nov 22 '18 at 23:03














  • 1





    some sample data and expected output for this data would be good.

    – Radim Bača
    Nov 22 '18 at 21:15











  • Check your answer

    – Peter Krauss
    Nov 22 '18 at 23:03








1




1





some sample data and expected output for this data would be good.

– Radim Bača
Nov 22 '18 at 21:15





some sample data and expected output for this data would be good.

– Radim Bača
Nov 22 '18 at 21:15













Check your answer

– Peter Krauss
Nov 22 '18 at 23:03





Check your answer

– Peter Krauss
Nov 22 '18 at 23:03












1 Answer
1






active

oldest

votes


















0














then why not just do:



SELECT location, registration_number FROM alarms
WHERE alarm_id IN (SELECT alarm_id FROM intrusions)
UNION
SELECT location, registration_number FROM cctvs
WHERE cctv_id IN (SELECT cctv_id FROM intrusions)


this should be mostly equivalent to your code, just much more terse and closer to your stated intent



if you do intend to get information from the intrusions table, then maybe something like:



SELECT intrusion_id, datetime_occurred,
COALESCE(a.location, c.location) AS location,
COALESCE(a.registration_number, c.registration_number) AS registration_number
FROM intrusions i
LEFT JOIN alarms a ON i.alarm_id = a.alarm_id
LEFT JOIN cctvs c ON i.cctv_id = c.cctv_id


not sure what you're doing to get a syntax error, it works for me after I do:



create temp table intrusions (intrusion_id serial primary key, datetime_occurred timestamp, alarm_id int, cctv_id int);
create temp table alarms (alarm_id serial primary key, location text, registration_number text);
create temp table cctvs (cctv_id serial primary key, location text, registration_number text);


It helps if you include this sort of code in the question! You'll get many more answers if you make it easier for other people…






share|improve this answer


























  • thank you! your first suggestion works but I ran into an error for your second suggestion and I just want to find out why! I got this error: ERROR: invalid reference to FROM-clause entry for table "intrusions" Hint: There is an entry for table "i", but it cannot be referenced from this part of the query. Position: 298

    – Amoroso
    Nov 22 '18 at 23:33











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%2f53437974%2fpostgresql-case-statement-get-another-column-value%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














then why not just do:



SELECT location, registration_number FROM alarms
WHERE alarm_id IN (SELECT alarm_id FROM intrusions)
UNION
SELECT location, registration_number FROM cctvs
WHERE cctv_id IN (SELECT cctv_id FROM intrusions)


this should be mostly equivalent to your code, just much more terse and closer to your stated intent



if you do intend to get information from the intrusions table, then maybe something like:



SELECT intrusion_id, datetime_occurred,
COALESCE(a.location, c.location) AS location,
COALESCE(a.registration_number, c.registration_number) AS registration_number
FROM intrusions i
LEFT JOIN alarms a ON i.alarm_id = a.alarm_id
LEFT JOIN cctvs c ON i.cctv_id = c.cctv_id


not sure what you're doing to get a syntax error, it works for me after I do:



create temp table intrusions (intrusion_id serial primary key, datetime_occurred timestamp, alarm_id int, cctv_id int);
create temp table alarms (alarm_id serial primary key, location text, registration_number text);
create temp table cctvs (cctv_id serial primary key, location text, registration_number text);


It helps if you include this sort of code in the question! You'll get many more answers if you make it easier for other people…






share|improve this answer


























  • thank you! your first suggestion works but I ran into an error for your second suggestion and I just want to find out why! I got this error: ERROR: invalid reference to FROM-clause entry for table "intrusions" Hint: There is an entry for table "i", but it cannot be referenced from this part of the query. Position: 298

    – Amoroso
    Nov 22 '18 at 23:33
















0














then why not just do:



SELECT location, registration_number FROM alarms
WHERE alarm_id IN (SELECT alarm_id FROM intrusions)
UNION
SELECT location, registration_number FROM cctvs
WHERE cctv_id IN (SELECT cctv_id FROM intrusions)


this should be mostly equivalent to your code, just much more terse and closer to your stated intent



if you do intend to get information from the intrusions table, then maybe something like:



SELECT intrusion_id, datetime_occurred,
COALESCE(a.location, c.location) AS location,
COALESCE(a.registration_number, c.registration_number) AS registration_number
FROM intrusions i
LEFT JOIN alarms a ON i.alarm_id = a.alarm_id
LEFT JOIN cctvs c ON i.cctv_id = c.cctv_id


not sure what you're doing to get a syntax error, it works for me after I do:



create temp table intrusions (intrusion_id serial primary key, datetime_occurred timestamp, alarm_id int, cctv_id int);
create temp table alarms (alarm_id serial primary key, location text, registration_number text);
create temp table cctvs (cctv_id serial primary key, location text, registration_number text);


It helps if you include this sort of code in the question! You'll get many more answers if you make it easier for other people…






share|improve this answer


























  • thank you! your first suggestion works but I ran into an error for your second suggestion and I just want to find out why! I got this error: ERROR: invalid reference to FROM-clause entry for table "intrusions" Hint: There is an entry for table "i", but it cannot be referenced from this part of the query. Position: 298

    – Amoroso
    Nov 22 '18 at 23:33














0












0








0







then why not just do:



SELECT location, registration_number FROM alarms
WHERE alarm_id IN (SELECT alarm_id FROM intrusions)
UNION
SELECT location, registration_number FROM cctvs
WHERE cctv_id IN (SELECT cctv_id FROM intrusions)


this should be mostly equivalent to your code, just much more terse and closer to your stated intent



if you do intend to get information from the intrusions table, then maybe something like:



SELECT intrusion_id, datetime_occurred,
COALESCE(a.location, c.location) AS location,
COALESCE(a.registration_number, c.registration_number) AS registration_number
FROM intrusions i
LEFT JOIN alarms a ON i.alarm_id = a.alarm_id
LEFT JOIN cctvs c ON i.cctv_id = c.cctv_id


not sure what you're doing to get a syntax error, it works for me after I do:



create temp table intrusions (intrusion_id serial primary key, datetime_occurred timestamp, alarm_id int, cctv_id int);
create temp table alarms (alarm_id serial primary key, location text, registration_number text);
create temp table cctvs (cctv_id serial primary key, location text, registration_number text);


It helps if you include this sort of code in the question! You'll get many more answers if you make it easier for other people…






share|improve this answer















then why not just do:



SELECT location, registration_number FROM alarms
WHERE alarm_id IN (SELECT alarm_id FROM intrusions)
UNION
SELECT location, registration_number FROM cctvs
WHERE cctv_id IN (SELECT cctv_id FROM intrusions)


this should be mostly equivalent to your code, just much more terse and closer to your stated intent



if you do intend to get information from the intrusions table, then maybe something like:



SELECT intrusion_id, datetime_occurred,
COALESCE(a.location, c.location) AS location,
COALESCE(a.registration_number, c.registration_number) AS registration_number
FROM intrusions i
LEFT JOIN alarms a ON i.alarm_id = a.alarm_id
LEFT JOIN cctvs c ON i.cctv_id = c.cctv_id


not sure what you're doing to get a syntax error, it works for me after I do:



create temp table intrusions (intrusion_id serial primary key, datetime_occurred timestamp, alarm_id int, cctv_id int);
create temp table alarms (alarm_id serial primary key, location text, registration_number text);
create temp table cctvs (cctv_id serial primary key, location text, registration_number text);


It helps if you include this sort of code in the question! You'll get many more answers if you make it easier for other people…







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 9:28

























answered Nov 22 '18 at 22:41









Sam MasonSam Mason

3,34711331




3,34711331













  • thank you! your first suggestion works but I ran into an error for your second suggestion and I just want to find out why! I got this error: ERROR: invalid reference to FROM-clause entry for table "intrusions" Hint: There is an entry for table "i", but it cannot be referenced from this part of the query. Position: 298

    – Amoroso
    Nov 22 '18 at 23:33



















  • thank you! your first suggestion works but I ran into an error for your second suggestion and I just want to find out why! I got this error: ERROR: invalid reference to FROM-clause entry for table "intrusions" Hint: There is an entry for table "i", but it cannot be referenced from this part of the query. Position: 298

    – Amoroso
    Nov 22 '18 at 23:33

















thank you! your first suggestion works but I ran into an error for your second suggestion and I just want to find out why! I got this error: ERROR: invalid reference to FROM-clause entry for table "intrusions" Hint: There is an entry for table "i", but it cannot be referenced from this part of the query. Position: 298

– Amoroso
Nov 22 '18 at 23:33





thank you! your first suggestion works but I ran into an error for your second suggestion and I just want to find out why! I got this error: ERROR: invalid reference to FROM-clause entry for table "intrusions" Hint: There is an entry for table "i", but it cannot be referenced from this part of the query. Position: 298

– Amoroso
Nov 22 '18 at 23:33




















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%2f53437974%2fpostgresql-case-statement-get-another-column-value%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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy