查询以根据输入和优先级过滤记录,如果不匹配,则拉空值记录

问题描述

Myrequirement是根据输入参数编写一个查询来显示描述,操作。在设置Test2中,itm优先于pm,优先于pf。如果没有匹配项,则默认设置为空值。

我有一张桌子Test1

GRP                  OP                  
----- --------------------
GRP1                 A                   
GRP1                 B                   
GRP1                 C     

表Test2

OP            ITM                  PM                   PF                         CUST DESCRIP             
------ -------------------- -------------------- -------------------- ---------- --------------------
A                                                                                   123 AB                  
C                                                                                   123 BC                  
B             I1                                                                    123 CD                  
B             I2                                                                    123 DE                  
B                                  PM1                                              123 EF                  
B                                                       PF1                         123 FG                  
B                                                                                   123 FGH   

我的输入参数是test1.grp,test2.pm,test2.itm,test.pf

如果我的输入是GRP = GRP1,Itm = I1,PM = PM1,PF = PF1,则我的输出应该是

op   descrip
A     AB  
C     BC  
B     CD   (because itm takes precedence over others)

如果我的输入是GRP = GRP1,Itm = I3,PM = PM1,PF = PF1,则我的输出应该是

op   descrip  
A     AB  
C     BC  
B     EF   (because itm I3 does not exist,so next precedence is pm)

如果我的输入是GRP = GRP1,Itm = I3,PM = PM2,PF = PF2,则我的输出应该是

op   descrip  
A     AB  
C     BC  
B     FGH   (because the three conditions do not match,so null is the generic setup)  

感谢您能帮上忙,因为我被困在按优先顺序比较多条记录。

解决方法

我能够根据需要编写查询。

`select coalesce(itmd,pmd,pfd,nulld),op from ( select a.op,max(decode( itm,'I3',descrip)) itmd,max(decode( pm,'PM2',descrip)) pmd,max(decode( pf,'PF2',descrip)) pfd,max(decode(itm,null,decode(pm,decode(pf,descrip)))) nulld
  from test1 a,test2 b where a.grp='GRP1' and a.op=b.op
  group by a.op);`

谢谢

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...