仅显示与控制表不匹配的行

时间:2015-05-22 05:07:40

标签: sql teradata

我目前遇到了一个问题,因为我无法用语言解释它,如果我在这里放一个测试表会更好。

表1:

Source_DB   Source_TBL   Source_Col   Target_DB   Target_TBL   Target_Col   Metric   Source_VAL   Target_Val
____________________________________________________________________________________________________________
Source_D    Source_T     Col1         Target_D    Target_T     Col1         Index    1            1
Source_D    Source_T     Col1         Target_D    Target_T     Col1         Length   10           10
Source_D    Source_T     Col1         Target_D    Target_T     Col1         Scale    4            4

控制表:

DB_NM      TBL_NM    COL_NM    METRIC    INCLUDE_FLAG
_____________________________________________________
Source_D   Source_T  Col1      Length    N

现在在控制表中,由于include标志为N,因此它不应包含与Table1中的DB_NM,TBL_NM,COL_NM匹配的行。我尝试使用NOT IN(子查询)但是因为我比较了很多列而失败了。也不允许使用EXISTS,因为控制表中的列与Table1中的列不匹配。这个是否有任何解决方法? 结果应该是:

 Source_DB   Source_TBL   Source_Col   Target_DB   Target_TBL   Target_Col   Metric   Source_VAL   Target_Val
____________________________________________________________________________________________________________
Source_D    Source_T     Col1         Target_D    Target_T     Col1         Index    1            1
Source_D    Source_T     Col1         Target_D    Target_T     Col1         Scale    4            4

2 个答案:

答案 0 :(得分:1)

SQL> SELECT * FROM t; ID EMP_ DEP EFFORTS ACTIVITY_ -------- ---- --- ---------- --------- 633 ALEX XYZ 30 13-May-15 633 ALEX XYZ 30 14-May-15 633 ALEX ABC 0 13-May-15 633 ALEX XYZ 0 15-May-15 SQL> 解决方案。您可能需要在子查询NOT EXISTS中添加一些条件:

WHERE

(即select t1.* from table1 t1 where NOT EXISTS (select 1 from ControlTable ct where t1.Source_DB = ct.DB_NM and t1.Source_TBL = ct.Source_TBL ... and ct.INCLUDE_FLAG = 'N') 将被删除,或替换为附加条件。)

答案 1 :(得分:1)

select SOURCE_DB, SOURCE_TBL, SOURCE_COL, TARGET_DB, Target_TBL, Target_Col, Metric, Source_VAL, Target_Val
from dummy_temp
where metric not in (select metric from dummy_control);