plsql:如果每一行匹配则返回匹配,否则显示dismatch字段

时间:2015-10-01 14:13:51

标签: sql oracle plsql

我有一个像这样的表TESTCOMPARE

SELECT t1.* FROM tools t1 JOIN integrations i ON (t1.id = i.tool_a_id) JOIN tools t2 ON (t2.id = i.tool_b_id) WHERE t2.id = x

customer | order | date | history | number| .....

(它是一个大表,标题不会改变,但值可能会改变)

我想返回一个像

这样的表

match | match | match| mismatch| match | .....

|overall| 如果我表中的每个字段都匹配。

或者返回仅包含不匹配字段的单个表。例如:

|match|

|history|

如何在pl / sql中执行此操作?提前谢谢。

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以按照自己的意愿行事,但需要列出所有列:

select 'customer' from testcompare where customer = 'mismatch' and rownum = 1
union all
select 'order' from testcompare where order = 'mismatch' and rownum = 1
union all
. . .

您的第二个问题可以通过多种方式完成。最简单的概念是:

<body class="{{$state.current.name.replace('.','-')}}">
    <div ui-view>
        <div ng-include src="'partials/menu.html'"></div>
        <!-- <div ng-menu class="ui top blue sidebar menu active"></div> -->

        <div class="view-height-100"></div>
    </div>

 ...
</body>

答案 1 :(得分:0)

我不完全确定你实际输出的是什么,所以这里有不同的输出方式可能对你有用:

with testcompare as (select 1 id, 'match' col1, 'match' col2, 'match' col3 from dual union all
                     select 2 id, 'match' col1, 'match' col2, 'mismatch' col3 from dual union all
                     select 3 id, 'match' col1, 'mismatch' col2, 'mismatch' col3 from dual union all
                     select 4 id, 'mismatch' col1, 'mismatch' col2, 'match' col3 from dual)
select id,
       case when col1 = 'match'
                 and col2 = 'match'
                 and col3 = 'match'
                 then 'match'
            else 'mismatch'
       end overall,
       case when col1 = 'match'
                 and col2 = 'match'
                 and col3 = 'match'
                 then 'match'
            else rtrim(decode(col1, 'mismatch', 'col1,')||
                       decode(col2, 'mismatch', 'col2,')||
                       decode(col3, 'mismatch', 'col3'), ',')
       end overall_with_mismatched_cols
from   testcompare;

        ID OVERALL  OVERALL_WITH_MISMATCHED_COLS
---------- -------- ----------------------------
         1 match    match                       
         2 mismatch col3                        
         3 mismatch col2,col3                   
         4 mismatch col1,col2     

with testcompare as (select 1 id, 'match' col1, 'match' col2, 'match' col3 from dual union all
                     select 2 id, 'match' col1, 'match' col2, 'mismatch' col3 from dual union all
                     select 3 id, 'match' col1, 'mismatch' col2, 'mismatch' col3 from dual union all
                     select 4 id, 'mismatch' col1, 'mismatch' col2, 'match' col3 from dual)
select id,
       mismatch_col_name
from   testcompare
unpivot (col_val for mismatch_col_name in (col1, col2, col3))
where  col_val = 'mismatch';

        ID MISMATCH_COL_NAME
---------- -----------------
         2 COL3             
         3 COL2             
         3 COL3             
         4 COL1             
         4 COL2      

至于你对Gordon回答“我不能列出所有专栏因为太多了”的评论......真的吗?你“不能”?或者你的意思是你不想?大多数gui都有一个表视图,您可以选择所有列,然后只需要搜索和替换以添加其他位和bobs等等。

或者您可以编写一个sql语句来生成列列表,然后您可以根据需要进行修改以适合您的查询。例如。从我上面的第一个查询中获取“整体”列,我会编写一个查询来生成when子句,如下所示:

select '                 and '||column_name||' = ''match''' col1
from   user_table_columns
where  table_name = 'TESTCOMPARE';