比较同一张表中的行值

时间:2018-10-22 23:22:28

标签: sql sql-server database syntax

我正在尝试生成查询。 我需要下表中TABLEOBJECTID列的值,其中 TABLENAME ='EMPLOYEE'并且保持了接口(CUSTFIELDNAME)=是(CUSTFIELDVALUE)并且 传输标志(CUSTFIELDNAME)=是(CUSTFIELDVALUE) 任何帮助表示赞赏。下面显示的SQL无法正常工作,无法获取数据,但也可以从其他字段获取值。

数据:

TABLEOBJECTID CUSTFIELDID TABLENAME CUSTFIELDNAME           CUSFIELDVALUE
2                    4    EMPLOYEE  INTERFACE MAINTAINED    No
2                    5    EMPLOYEE  TRANSFER HOURS TO AG    Yes
2                    7    EMPLOYEE  TRANSFER FLAG           Yes
3                    4    EMPLOYEE  INTERFACE MAINTAINED    Yes
3                    7    EMPLOYEE  TRANSFER FLAG           Yes
31                   4    EMPLOYEE  INTERFACE MAINTAINED    No
31                   5    EMPLOYEE  TRANSFER HOURS TO AG    No
31                   6    EMPLOYEE  DEFAULT JOB CODE    
94                   4    EMPLOYEE  INTERFACE MAINTAINED    No
94                   6    EMPLOYEE  DEFAULT JOB CODE        0325
94                   7    EMPLOYEE  TRANSFER FLAG           No
96                   4    EMPLOYEE  INTERFACE MAINTAINED    No
96                   6    EMPLOYEE  DEFAULT JOB CODE        0325
100                  4    EMPLOYEE  INTERFACE MAINTAINED    No
100                  5    EMPLOYEE  TRANSFER HOURS TO AG    No
100                  6    EMPLOYEE  DEFAULT JOB CODE    
100                  7    EMPLOYEE  TRANSFER FLAG           No
101                  4    EMPLOYEE  INTERFACE MAINTAINED    Yes
101                  6    EMPLOYEE  DEFAULT CODE            9760
101                  7    EMPLOYEE  TRANSFER FLAG           Yes

预期结果:

TABLEOBJECTID CUSTFIELDID TABLENAME CUSTFIELDNAME           CUSFIELDVALUE
3                    4    EMPLOYEE  INTERFACE MAINTAINED    Yes
3                    7    EMPLOYEE  TRANSFER FLAG           Yes
101                  4    EMPLOYEE  INTERFACE MAINTAINED    Yes   
101                  7    EMPLOYEE  TRANSFER FLAG           Yes

我一直在尝试此脚本,但是它也带来了其他CUSTFIELDNAME的结果:

select CWA.TABLEOBJECTID ,CWA.CUSTFIELDID ,CWA.CUSTFIELDNAME ,CWA.CUSTFIELDVALUE ,CWA.TABLENAME ,CWA.TABLEOBJECTID  from az.CWCUSTFIELD CWA 
    inner join  az.CWCUSTFIELD CWB on CWA.TABLEOBJECTID  =CWB.TABLEOBJECTID  and CWA.CUSTFIELDID <> CWB.CUSTFIELDID 
    where  CWA.tablename = 'EMPLOYEE' and CWB.TABLENAME ='EMPLOYEE' and (CWA.CUSTFIELDVALUE = 'YES' and CWB.CUSTFIELDVALUE = 'YES')

1 个答案:

答案 0 :(得分:0)

您仅在寻找接口维护(4)和转移标志(7)时错过了CUSTFIELDID上的过滤器,因此您应该在两个表的此字段上添加过滤器。

AND CWA.CUSTFIELDID(4,7) AND CWB.CUSTFIELDID(4,7)

或者您可以在CUSTFIELDNAME而不是CUSTFIELDID上应用过滤器,例如

AND CWA.CUSTFIELDNAME IN ('INTERFACE MAINTAINED', 'TRANSFER FLAG')
   AND CWB.CUSTFIELDNAME IN ('INTERFACE MAINTAINED', 'TRANSFER FLAG')

最终查询-

select CWA.TABLEOBJECTID ,
  CWA.CUSTFIELDID ,
  CWA.CUSTFIELDNAME ,
  CWA.CUSTFIELDVALUE ,
  CWA.TABLENAME ,
  CWA.TABLEOBJECTID  
from az.CWCUSTFIELD CWA 
inner join  az.CWCUSTFIELD CWB on CWA.TABLEOBJECTID  =CWB.TABLEOBJECTID  
            and CWA.CUSTFIELDID <> CWB.CUSTFIELDID 
where  CWA.tablename = 'EMPLOYEE' 
  and CWB.TABLENAME ='EMPLOYEE' 
  and CWA.CUSTFIELDVALUE = 'YES' 
  and CWB.CUSTFIELDVALUE = 'YES'
  and CWA.CUSTFIELDNAME IN ('INTERFACE MAINTAINED', 'TRANSFER FLAG')
  and CWB.CUSTFIELDNAME IN ('INTERFACE MAINTAINED', 'TRANSFER FLAG')