简体   繁体   English

根据SQL中两个表的条​​件查询以合并来自2个表的数据

[英]Query to combine data from 2 tables based on a condition for both the tables in SQL

I have 2 tables. 我有2张桌子。 ProfileInfo (ProfileID - PK) and EmployeeRole (EmpID - PK, ProfileID - FK). ProfileInfo(ProfileID-PK)和EmployeeRole(EmpID-PK,ProfileID-FK)。 Both are connected using a ProfileID and both tables have LastUpdatedTimestamp Column. 两者都使用ProfileID连接,并且两个表都具有LastUpdatedTimestamp列。 I need to fetch data from both the tables combined, using from and to lastupdated timestamp. 我需要使用from和to lastupdated时间戳从合并的两个表中获取数据。

Sometimes both the tables get updated at the same time and most times only one get updated 有时两个表都同时更新,大多数情况下只有一个更新

. Here is what i have tried but it bring up data which is updated on both tables. 这是我尝试过的方法,但是它调出了两个表上更新的数据。 Firstly, I tried join but it didn't work as much as i thought it would 首先,我尝试加入,但效果不如我想的那样

     select emp.emp_id as EmpId from EmployeeRole emp 
     FULL OUTER JOIN ProfileInfo pi on emp.profile_id = pi.profile_id
     where emp.LST_UPDT_TS between '2017-09-18' and '2017-09-20' and 
     pi.LST_UPDT_TS between '2017-09-18' and '2017-09-20';

This brought emp ids that had changes on both the tables alone. 这带来的emp id仅在两个表上都发生了变化。

Table Details: 表格详情:
EmployeeRole Emp_ID PK, Profile_id FK, LST_UPDT_TS TIMESTAMP EmployeeRole Emp_ID PK,Profile_id FK,LST_UPDT_TS TIMESTAMP

ProfileInfo Profile_Id PK, Profile_name, LST_UPDT_TS TIMESTAMP ProfileInfo Profile_Id PK,Profile_name,LST_UPDT_TS TIMESTAMP

Example: If 2 records of ProfileInfo gets updated and 1 record of EmployeeRole gets updated. 示例:如果更新了ProfileInfo的2条记录,并且更新了EmployeeRole的1条记录。 I need to get 3 emp_id considering both the records from ProfileInfo is not related to EmployeeRole record. 考虑到ProfileInfo的两个记录都与EmployeeRole记录无关,我需要获取3 emp_id。 If in case one of the record is related then I have to get 2 emp_id only. 如果万一其中一条记录是相关的,那么我只需要获得2 emp_id。

I searched for similar answers for a short period but nothing worked. 我在短时间内搜索了类似的答案,但没有任何效果。 Please help. 请帮忙。

This is just an example, your conditions may vary 这只是一个例子,您的情况可能会有所不同

   SELECT
       -- common data
       COALESCE(emp.profile_id, pi.profile_id) as profile_id
      ,COALESCE(emp.LST_UPDT_TS, pi.LST_UPDT_TS) as LST_UPDT_TS
       -- emp role
      ,emp.emp_id as EmpId
       -- profile
      , pi.Profile_name
  FROM (SELECT * 
        FROM EmployeeRole 
        WHERE LST_UPDT_TS between '2017-09-18' and '2017-09-20') emp 
  FULL OUTER JOIN (
        SELECT * 
        FROM  ProfileInfo 
        WHERE LST_UPDT_TS between '2017-09-18' and '2017-09-20') pi
     -- rows matching predicate
      ON emp.profile_id = pi.profile_id
     AND emp.LST_UPDT_TS = pi.LST_UPDT_TS 

This worked with some little tweeks from the initial query I changed. 从我更改的初始查询开始,这花了t几个星期的时间。

select emp.emp_id as EmpId from EmployeeRole emp  
 JOIN ProfileInfo pi on emp.profile_id = pi.profile_id and
 ((emp.LST_UPDT_TS between '2017-09-18' and '2017-09-20') or 
 (pi.LST_UPDT_TS between '2017-09-18' and '2017-09-20'));

Thanks a lot @Serg and @Phil and @wildplasser 非常感谢@Serg和@Phil和@wildplasser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM