sql深度选择没有给出解决方案

时间:2014-05-14 12:14:11

标签: sql oracle

我必须修改我工作中的sql语句以获取更改的数据信息。

这是旧代码:

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
;

修改是从此选择中排除更多数据。 我在1 tpc_id下有一些p_code组合。 这个tpc_id用于一个进程,但在DB中有更多行,并且tpc_ids更多。

现有的sql语句显示现有show class level 22和p_codes的所有现有数据不在' 42282221'。
但我必须排除所有p_code' 46262255'
结合&#;; 23040400' 23040401',#230; 23040411',' 23040412',' 23040414' ,' 23040496',' 23040497' (在一个tpc_id下) 和
p_code' 42282241'加上
' 21041019',' 21041015',' 21041024'。

所以我用这句话测试:

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
;

但它不起作用。 我只拿回一张空桌子。有人知道为什么以及如何解决它?

2 个答案:

答案 0 :(得分:1)

从你上次的评论中,尤其是关于Hennes'回答,很明显你正在寻找EXISTS条款,这是我从一开始就假设的。因此,请检查以下查询是否适合您。

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
;

答案 1 :(得分:0)

你将表fw_p_erg包含两次并将它们组合在一起。呃,那是什么?这是一个错字吗?

尽管如此,join xce.tcp_id = pce.tcp_id应该在括号之外,如下所示:

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
;

亨尼斯