Question : Update Query with subquery

Hello experts!

In my update statement I want to set "str_gewertet" to the result of the subquery for each record where tbl_matrix_intermediate_result = z.pk.

Is it even possible to use a column of an inner query as an identifier for the records of the table to be updated?

Many thanks for your help!

Brgds,

Seb
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
update tbl_matrix_intermediate_result
      set
        (str_gewertet)
        = 
(select gewertete_ba from (
with w as (select cnt_tbl_matrix_intermediate pk, lng_baumart, str_ba_lang, str_ba_kat, int_prozent, str_gewertet, str_lrt_class from tbl_matrix_intermediate_result
          where str_ba_kat in ('H', 'N', 'B', 'P')
          and lng_gebiet    = :p_cnt_gebiet
          and str_lrt_class = :p_str_lrt
          and int_wg        = :p_wg
          and int_be        = :p_be
          and flag not     in (1,2,3,4,5,6,7,8,9,10)
          and eval_type     = 1 
         )

(select pk, str_ba_lang, prozent, lng_baumart, psumme, case when lng_baumart = 33  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (28,31) and w.int_prozent is not null) = 0 then 'Ja' 
when lng_baumart = 53  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (28,29,30,31,32,33) and w.int_prozent is not null) = 0 then 'Ja'
when lng_baumart = 54  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (13,14,15) and w.int_prozent is not null) = 0 then 'Ja' 
when lng_baumart = 72  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (22,23,25,26,27) and w.int_prozent is not null) = 0 then 'Ja' 
when lng_baumart = 74  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (5,6) and w.int_prozent is not null) = 0 then 'Ja' 
when lng_baumart = 79  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (28,29,30,31,32) and w.int_prozent is not null) = 0 then 'Ja'
when lng_baumart not in (33,53,54,72,74,79)  and prozent is not null and str_gewertet = 'Ja' then 'Ja'
else 'Nein'
end as gewertete_ba
from (select pk, lng_baumart, str_ba_lang, str_ba_kat ba_kat, str_gewertet, int_prozent prozent, sum(int_prozent) over (partition by str_lrt_class) psumme from w)))z)
where cnt_matrix_intermediate_result = z.pk

Answer : Update Query with subquery

ok.. can you try this :

update tbl_matrix_intermediate_result m1
      set
        (m1.str_gewertet)
        =
(select gewertete_ba from (
with w as (select cnt_tbl_matrix_intermediate pk, lng_baumart, str_ba_lang, str_ba_kat, int_prozent, str_gewertet, str_lrt_class
          from tbl_matrix_intermediate_result      
          where str_ba_kat in ('H', 'N', 'B', 'P')
          and lng_gebiet    = :p_cnt_gebiet          
          and str_lrt_class = :p_str_lrt          
          and int_wg        = :p_wg          
          and int_be        = :p_be
          and flag not     in (1,2,3,4,5,6,7,8,9,10)      
          and eval_type     = 1 )
(select pk, str_ba_lang, prozent, lng_baumart, psumme,
 case when lng_baumart = 33  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (28,31) and w.int_prozent is not null) = 0 then 'Ja'
when lng_baumart = 53  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (28,29,30,31,32,33) and w.int_prozent is not null) = 0 then 'Ja'
when lng_baumart = 54  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (13,14,15) and w.int_prozent is not null) = 0 then 'Ja'
when lng_baumart = 72  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (22,23,25,26,27) and w.int_prozent is not null) = 0 then 'Ja'
when lng_baumart = 74  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (5,6) and w.int_prozent is not null) = 0 then 'Ja'
when lng_baumart = 79  and str_gewertet = 'Ja' and prozent is not null and (select count(*) from w where w.lng_baumart in (28,29,30,31,32) and w.int_prozent is not null) = 0 then 'Ja'
when lng_baumart not in (33,53,54,72,74,79)  and prozent is not null and str_gewertet = 'Ja' then 'Ja'
else 'Nein'
end as gewertete_ba
from (select pk, lng_baumart, str_ba_lang, str_ba_kat ba_kat, str_gewertet, int_prozent prozent, sum(int_prozent) over (partition by str_lrt_class) psumme from w)))z
where m1.cnt_matrix_intermediate_result = z.pk )
where exists ( select 1
          from tbl_matrix_intermediate_result xx
          where xx.str_ba_kat in ('H', 'N', 'B', 'P')
          and xx.lng_gebiet    = :p_cnt_gebiet
          and xx.str_lrt_class = :p_str_lrt
          and xx.int_wg        = :p_wg
          and xx.int_be        = :p_be
          and xx.flag not     in (1,2,3,4,5,6,7,8,9,10)
          and xx.eval_type     = 1
          and xx.cnt_tbl_matrix_intermediate = m1.cnt_tbl_matrix_intermediate  ) ;

I have added a where condition to the update command as well so that only those records which need to get updated will updated and also moved that condition which gave error to inside so that it can work.

Test it well enough to see if that is working fine because i cannot test this at my end because of no tables/data.

Thanks,
Random Solutions  
 
programming4us programming4us