简体   繁体   English

SQL 查询存在且不存在

[英]SQL query Exists AND NOT Exists

I have a table where I need to insert the data from a view which has 2 conditions : 1) Insert those data in a table where pk is unavailable in a transaction table but is available in the view 2) if pk is there but data is different from the view then insert those data also我有一个表,我需要从具有 2 个条件的视图中插入数据:1) 将这些数据插入到事务表中 pk 不可用但在视图中可用的表中 2) 如果 pk 存在但数据是与视图不同,然后也插入这些数据

        Insert into table A
    a,
    b,
    c,
    d
SELECT 
a,
b,
c,
d
from View sa
WHERE not Exists 
(Select * FROM table A q
    where SA.a = q.a )
    OR 
    CASE WHEN  Exists
    (Select * FROM table A q
    where SA.a = q.a 
    AND 


                        SA.b <> q.b
                        OR SA.c <> q.c
                        OR SA.d <> q.d
                    ) 

Any help appreciated!任何帮助表示赞赏!

I believe this is the proper format for your INSERT with SELECT for Netezza.我相信这是使用SELECT for Netezza INSERT的正确格式。 I removed the invalid CASE expression and there should be no other syntax errors, but not sure about the logic:我删除了无效的CASE表达式,应该没有其他语法错误,但不确定逻辑:

Insert into tableA (a,b,c,d) (
SELECT a,b,c,d
from View sa
WHERE not Exists 
   (Select * FROM tableA q
    where SA.a = q.a 
    )
 OR Exists
    (Select * FROM tableA q2
    where SA.a = q2.a 
    AND SA.b <> q2.b
     OR SA.c <> q2.c
     OR SA.d <> q2.d
    )
)

Edit: I think it may have been complaining about the re-use of the q table alias.编辑:我认为它可能一直在抱怨q表别名的重用。

As a general rule, correlated subqueries are not for Netezza.作为一般规则,相关子查询不适用于 Netezza。 Obviously you can use them in many cases, but often at the cost of turning an MPP platform into a serial processor.显然,您可以在许多情况下使用它们,但通常以将 MPP 平台转变为串行处理器为代价。 Bad.坏的。

You can rewrite your insert a number of ways, but this seems the clearest to me.您可以通过多种方式重写插入内容,但这对我来说似乎是最清楚的。 I have no idea what this logic is trying to do, but I replicated it nonetheless.我不知道这个逻辑想要做什么,但我还是复制了它。 You may have an issue if view contains duplicates;如果view包含重复项,您可能会遇到问题; this can be addressed with a little more knowledge of your data.这可以通过更多地了解您的数据来解决。

insert into tableA (
  a
  ,b
  ,c
  ,d
)
select
  viw.a
  ,viw.b
  ,viw.c
  ,viw.d
from
  view viw
  join tableA tba on
    (viw.a = tba.a)
    or (
      viw.a = tba.a
      and viw.b <> tba.b
      or viw.c <> tba.c
      or viw.d <> tba.d
    )

You could remove the possibility of view duplicates by inserting into tableA from an actual table and use the rowids found there.您可以通过从实际表中插入tableA并使用在那里找到的 rowid 来消除视图重复的可能性。 Perhaps something like this:也许是这样的:

create temporary table temp_view as
  select * from view
distribute on (some_key);

Then collect rowids to insert like so:然后收集rowids像这样插入:

insert into tableA (
  a
  ,b
  ,c
  ,d
)
with mar as ( --Match 'a' records.
  select
    viw.rowid insert_rowid
  from
    temp_view viw
    join tableA tba using (a)
), mnb as ( --Match against 'b'
  select
    viw.rowid
  from
    temp_view viw
    join tableA tba on 
      viw.a = tba.a
      and viw.b <> tba.b
      or viw.c <> tba.c
      or viw.d <> tba.d
), rws as ( --All rowids.
  select * from mar
  union select * from mnb
)
select
  a
  ,b
  ,c
  ,d
from
  temp_view viw
  join rws on rws.insert_rowid = viw.rowid;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM