Question

I must modify a sql statement on my work for an altered data information.

This is the old one code:

SELECT count(*) n,  max(pce.insert_ts) last_insert, pce.p_code, dpc.m_operation
from tcp_data.fw_p_erg pce inner join tcp_data.fw_dim_p_code dpc
                                       on (pce.p_code = dpc.p_code)
where dpc.show_class = 22
  and pce.p_code not in ('42282221')
group by pce.p_code, dpc.m_operation
order by last_insert desc, n desc, p_code
;

The modification is to exclude more data from this selection. I have some p_code combination under 1 tpc_id. This tpc_id is for one process but have more line in DB with more tpc_ids.

The exist sql statement show all exist data for existing show class level 22 and p_codes are not in '42282221'.
But I must exclude all p_code '46262255'
combined with '23040400', '23040401', '23040411', '23040412', '23040414', '23040496', '23040497' (under one tpc_id) and
p_code '42282241' combined with
'21041019', '21041015', '21041024'.

So I test with this statement:

SELECT count(*) n,  max(pce.insert_ts) last_insert, pce.p_code, dpc.m_operation
from tcp_data.fw_p_erg xce,
     tcp_data.fw_p_erg pce inner join tcp_data.fw_dim_p_code dpc
                                   on (pce.p_code = dpc.p_code)
where dpc.show_class = 22
  and pce.p_code not in ('42282221')
  and not(xce.tcp_id=pce.tcp_id an xce.p_code = '46262255' and pce.p_code not in ('23040400', '23040401', '23040411', '23040412', '23040414', '23040496', '23040497'))
  and not(xce.tcp_id=pce.tcp_id an xce.p_code = '42282241' and pce.p_code not in ('21041019', '21041015', '21041024'))
group by pce.p_code, dpc.m_operation
order by last_insert desc, n desc, p_code
;

But it doesnt work. I get only an empty table back. Have someone any idea why and how i can solve it ?

Was it helpful?

Solution

From your last comments, especially the last one on Hennes' answer, it seems obvious that you are looking for the EXISTS clause, which was what I assumed from the start. So please check, if the following query works for you.

SELECT 
  count(*) n,
  max(pce.insert_ts) last_insert, 
  pce.p_code, 
  dpc.m_operation
from tcp_data.fw_p_erg pce 
inner join tcp_data.fw_dim_p_code dpc on (pce.p_code = dpc.p_code)
where dpc.show_class = 22
  and pce.p_code not in ('42282221')
  and not
  (
    pce.p_code in ('23040400', '23040401', '23040411', '23040412', '23040414', '23040496', '23040497')
    and 
    exists (select * from tcp_data.fw_p_erg xce where xce.tcp_id = pce.tcp_id and xce.p_code = '46262255')
  )
  and not
  (
    pce.p_code in ('21041019', '21041015', '21041024')
    and 
    exists (select * from tcp_data.fw_p_erg xce where xce.tcp_id = pce.tcp_id and xce.p_code = '42282241')
  )
group by pce.p_code, dpc.m_operation
order by last_insert desc, n desc, p_code
;

OTHER TIPS

you included the table fw_p_erg twice and combine them on their id. Err, what ist that for?? Is that a typo?

Nevertheless, the join xce.tcp_id=pce.tcp_id should be outside the brackets like this:

SELECT count(*) n,  max(pce.insert_ts) last_insert, pce.p_code, dpc.m_operation
from tcp_data.fw_p_erg xce,
    tcp_data.fw_p_erg pce inner join tcp_data.fw_dim_p_code dpc
                               on (pce.p_code = dpc.p_code)
where dpc.show_class = 22
  and pce.p_code not in ('42282221')
  and xce.tcp_id=pce.tcp_id
  and not(xce.p_code = '46262255' and pce.p_code not in ('23040400',     '23040401', '23040411', '23040412', '23040414', '23040496', '23040497'))
  and not(xce.p_code = '42282241' and pce.p_code not in ('21041019', '21041015', '21041024'))
group by pce.p_code, dpc.m_operation
order by last_insert desc, n desc, p_code
;

Hennes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top