2

How I can make the following query and delete in one query ?

select krps.kpi_results_fk from report.kpi_results_per_scene krps inner join report.kpi_results kr on kr.session_uid = '0000c2af-1fc8-4729-bb2a-d4516a63107a' 
and kr.pk = krps.kpi_results_fk

delete from report.kpi_results_per_scene where kpi_results_fk = 'answer from above query'
2
  • @sebastianbrosch int Commented Mar 22, 2016 at 10:09
  • Put the select query inside your delete query, just like this DELETE FROM ..... WHERE kpi_results_fk = (SELECT krps.kpi_results_fk FROM .....). Your select query must return only (1) result. Just incase it returns more than (1), put ANY like this DELETE FROM ..... WHERE kpi_results_fk = ANY(SELECT krps.kpi_results_fk FROM .....) Commented Mar 22, 2016 at 10:11

3 Answers 3

1

I think for your case, NO need to use inner join.

Following query could reduce the overhead of inner join

DELETE FROM report.kpi_results_per_scene 
WHERE kpi_results_fk IN 
        (SELECT kr.pk FROM report.kpi_results kr 
            WHERE kr.session_uid = '0000c2af-1fc8-4729-bb2a-d4516a63107a') 
Sign up to request clarification or add additional context in comments.

Comments

1

use IN operator:

delete from report.kpi_results_per_scene where kpi_results_fk in (
select krps.kpi_results_fk from report.kpi_results_per_scene krps inner join report.kpi_results kr on kr.session_uid = '0000c2af-1fc8-4729-bb2a-d4516a63107a' 
and kr.pk = krps.kpi_results_fk)

1 Comment

Since when in is a clause?
0

Try the code bellow:

delete from report.kpi_results_per_scene
where kpi_results_fk IN (select krps.kpi_results_fk
                        from report.kpi_results_per_scene krps inner join report.kpi_results kr
                        on kr.session_uid = '0000c2af-1fc8-4729-bb2a-d4516a63107a' 
                        and kr.pk = krps.kpi_results_fk)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.