两个表之间的多列匹配计数

时间:2019-07-06 10:42:21

标签: sql

我想根据以下内容检查两个表之间的以下列的计数:  (COMPANY_CODE + AGENT + AGENT_LEVEL + MARKET_CODE + START_DATE + STOP_DATE)

02           1000000    101           60       BASE     20110303    20170303
02           1000001    101           60       BASE     20110303    20170303
02           1000002    101           60       BASE     20110303    20170303
02           1000003    101           60      BASE      20110303    20170303

COMPANY_COD POLICY_NUMBER AGENT AGENT_LEVEL MARKET_CODE START_DATE STOP_DATE
01           0000001    100           80       BASE     20100303    20180303
01           0000002    100           80       BASE     20100303    20180303
01           0000003    100           80       BASE     20100303    20180303
01           0000004    100           80       BASE     20100303    20180303

1 个答案:

答案 0 :(得分:0)

如果要比较两个表中的行,请使用union allgroup by

select COMPANY_CODE, AGENT, AGENT_LEVEL, MARKET_CODE, START_DATE, STOP_DATE, sum(in_1), sum_in_2)
from ((select COMPANY_CODE, AGENT, AGENT_LEVEL, MARKET_CODE, START_DATE, STOP_DATE, 1 as in_1, 0 as in_2
       from t1
      ) union all
      (select COMPANY_CODE, AGENT, AGENT_LEVEL, MARKET_CODE, START_DATE, STOP_DATE, 2 as in_1, 1 as in_2
       from t2
      ) 
     ) t
group by COMPANY_CODE, AGENT, AGENT_LEVEL, MARKET_CODE, START_DATE, STOP_DATE
having sum(in_1) <> sum(in_2);

having子句返回一个值或另一个值。或者这两个表中的次数不同。