Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
plsql:如果每一行都匹配,则返回匹配,否则显示不匹配字段_Sql_Oracle_Plsql - Fatal编程技术网

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

plsql:如果每一行都匹配,则返回匹配,否则显示不匹配字段,sql,oracle,plsql,Sql,Oracle,Plsql,我有一张这样的表格 客户|订单|日期|历史|编号| 匹配匹配匹配不匹配匹配匹配 这是一个大表,标题不会更改,但值可能会更改 我想要一张像这样的单张桌子 |总体上| |匹配| 如果表中的每个字段都匹配 或者返回一个仅包含不匹配字段的表。例如: |历史| |不匹配| 如何在pl/sql中实现这一点?提前谢谢。如果我理解正确,您可以做您想做的事情,但您需要列出所有列: select tc.* from testcompare tc where 'mismatch' not in (customer,

我有一张这样的表格

客户|订单|日期|历史|编号|

匹配匹配匹配不匹配匹配匹配

这是一个大表,标题不会更改,但值可能会更改

我想要一张像这样的单张桌子

|总体上|

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

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

|历史|

|不匹配|


如何在pl/sql中实现这一点?提前谢谢。

如果我理解正确,您可以做您想做的事情,但您需要列出所有列:

select tc.*
from testcompare tc
where 'mismatch' not in (customer, order, date, history, number, . . .);
你的第二个问题可以通过几种方式来完成。最简单的概念是:

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

我不完全确定您真正想要的是什么输出,因此这里有几种不同的方法来获取输出,这些方法可能对您有所帮助:

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      

至于你对戈登回答的评论,我不能列出所有的专栏,因为有太多。。。真正地你不能?还是说你不想?大多数gui都有一个表视图,您可以选择所有列,然后只需搜索和替换即可添加其他位和二进制位,如逗号等

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

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

一些示例数据和所需结果,这将更容易理解。@jarlh第一个表是示例数据,下两个表是所需结果。如果给定行有两个字段不匹配,是否希望在不匹配的字段数据集中显示两行,或者您想要一个带分隔符的列表的单行?@Boneist我想要两行不匹配的字段。我无法列出所有列,因为列太多了。。是否可以通过循环或其他方式执行?您可以设置动态SQL立即执行。但是,您可以从所有选项卡中获取列。我会将它们放入电子表格中,并在那里生成SQL。如果您需要在一段时间内工作,例如在存储过程中,那么您将需要与动态SQL相同的逻辑。