SQL Server: Compare two columns in two tables -
i have 2 tables (from 2 different systems) track employees' hours. in both tables, employees enter date , hours. need create audit report shows discrepancy. report needs show columns , display null/no match if there mismatch. 1 table might have more/less entries other table or duplicate entries , need catch (two entries on same day same amount of hours in 1 table). both tables have userid can joined on.
if there match based on date , hours, show values. if there mismatch based on hours, show null or no match when there mismatch. if there duplicate entry, image below, match first entry , report second 1 null or no match.
i tried joining tables based userid, date, , hours not able tell mismatch came from.
table a:
table b:
left join on userid, date, , hours
set nocount on; declare @table1 table ( userid int, entry_date date, [hours] varchar(10) ) declare @table2 table ( userid int, entry_date datetime, [hours] varchar(10) ) insert @table1 select 1,'8/14/2017','10:00' insert @table1 select 2,'8/14/2017','5:00' insert @table1 select 2,'8/14/2017','5:00' insert @table1 select 2,'8/14/2017','5:00' insert @table1 select 2,'8/14/2017','5:00' insert @table1 select 3,'8/14/2017','5:00' insert @table1 select 3,'8/14/2017','6:00' insert @table1 select 3,'8/14/2017','6:00' insert @table1 select 3,'8/14/2017','6:00' insert @table2 select 1,'8/14/2017','10:00' insert @table2 select 2,'8/14/2017','8:00' insert @table2 select 3,'8/14/2017','6:00' insert @table2 select 4,'8/14/2017','2:00' insert @table2 select 1,'8/14/2017','10:00' insert @table2 select 3,'8/14/2017','6:00' ;with cte_table1 ( select t.userid userid, cast(t.entry_date date) entry_date, t.[hours] [hours], row_number() over(partition t.userid, t.entry_date, t.[hours] order t.[hours]) rnk @table1 t ), cte_table2 ( select t.userid userid, cast(t.entry_date date) entry_date, t.[hours] [hours], row_number() over(partition t.userid, t.entry_date, t.[hours] order t.[hours]) rnk @table2 t ), cte_matches ( select t1.userid userid, t1.entry_date entry_date, t1.[hours] [hours], t1.rnk cte_table1 t1 inner join cte_table2 t2 on (t1.userid = t2.userid , t1.entry_date = t2.entry_date , t1.[hours] = t2.[hours] , t1.rnk = t2.rnk) ),cte_match_duplicates ( select 'table1matchduplicate' errortype, * ( select t.* (select userid, entry_date, [hours], max(rnk) rnk cte_matches group userid, entry_date, [hours]) m inner join cte_table1 t on (t.userid = m.userid , t.entry_date = m.entry_date , t.[hours] = m.[hours] , t.rnk > m.rnk) )q union select 'table2matchduplicate' errortype, * ( select t.* (select userid, entry_date, [hours], max(rnk) rnk cte_matches group userid, entry_date, [hours]) m inner join cte_table2 t on (t.userid = m.userid , t.entry_date = m.entry_date , t.[hours] = m.[hours] , t.rnk > m.rnk) )q ) , cte_table1_unmatched ( select t.userid, t.entry_date, t.[hours] @table1 t left outer join cte_matches m on (t.userid = m.userid , cast(t.entry_date date) = m.entry_date , t.[hours] = m.[hours]) m.userid null ), cte_table2_unmatched ( select t.userid, t.entry_date, t.[hours] @table2 t left outer join cte_matches m on (t.userid = m.userid , cast(t.entry_date date) = m.entry_date , t.[hours] = m.[hours]) m.userid null ) select null errortype, userid, entry_date, [hours] cte_matches union select 'table1mismatch' errortype, userid, entry_date, [hours] cte_table1_unmatched union select 'table2mismatch' errortype, userid, entry_date, [hours] cte_table2_unmatched union select errortype, userid, entry_date, [hours] cte_match_duplicates order errortype
if need find duplicates had no matches well:
,cte_table1_unmatched_duplicates ( select userid, entry_date, [hours] cte_table1_unmatched group userid, entry_date, [hours] having count(*) > 1 ),cte_table2_unmatched_duplicates ( select userid, entry_date, [hours] cte_table2_unmatched group userid, entry_date, [hours] having count(*) > 1 ) ... union select 'table1unmatchedduplicates' errortype, userid, entry_date, [hours] cte_table1_unmatched_duplicates union select 'table2unmatchedduplicates' errortype, userid, entry_date, [hours] cte_table2_unmatched_duplicates
Comments
Post a Comment