| Author |
Topic |
|
trader1012
Starting Member
1 Post |
Posted - 2009-09-24 : 11:53:27
|
| I want to compare two tables daily. Each table has 10 columns...and about 2000 rows - trades (which can differ daily). My query can compare both the table with the help of Primary key, means the query will return any additional trade from one of the tables...but it does not return if two fields are different...for example let's say if cell G2 in Table 1 is different from cell G2 in Table 2...here is my queryMQ_SEQ_NO is the primary key.....which is basically trade ID and must be same for both the tables..Table 1 and Table 2declare @START_DATE DATETIME, @END_DATE DATETIMESELECT @START_DATE = '09/23/2009'select @END_dATE = '09/24/2009'SELECT 'Table1' , [MQ_SEQ_NO] ,[TRANS_SEQ_NO] ,[FIRM] ,[JJDDD] ,[ORIG_REF] ,[XCL_REV_CD] ,[OFFSET_CD] ,[USER_REF] ,[CXLD_COMBINED_REF] ,[STREET] FROM Table1 where CONVERT(DATETIME,create_dt) >= @START_DATE and CONVERT(DATETIME,create_dt) < @END_DATE AND NOT EXISTS ( SELECT [MQ_SEQ_NO] ,[TRANS_SEQ_NO] ,[FIRM] ,[JJDDD] ,[ORIG_REF] ,[XCL_REV_CD] ,[OFFSET_CD] ,[USER_REF] ,[CXLD_COMBINED_REF] ,[STREET] FROM Table2where CONVERT(DATETIME,create_dt) >= @START_DATE and CONVERT(DATETIME,create_dt) < @END_DATE AND Table1.[MQ_SEQ_NO] = Table2.[MQ_SEQ_NO] )UNIONSELECT 'Table2' ,[MQ_SEQ_NO] ,[TRANS_SEQ_NO] ,[FIRM] ,[JJDDD] ,[ORIG_REF] ,[XCL_REV_CD] ,[OFFSET_CD] ,[USER_REF] ,[CXLD_COMBINED_REF] ,[STREET] FROM Table2where CONVERT(DATETIME,create_dt) >= @START_DATE and CONVERT(DATETIME,create_dt) < @END_DATEAND NOT EXISTS ( SELECT [MQ_SEQ_NO] ,[TRANS_SEQ_NO] ,[FIRM] ,[JJDDD] ,[ORIG_REF] ,[XCL_REV_CD] ,[OFFSET_CD] ,[USER_REF] ,[CXLD_COMBINED_REF] ,[STREET] FROM Table1 where CONVERT(DATETIME,create_dt) >= @START_DATE and CONVERT(DATETIME,create_dt) < @END_DATE AND Table2.[MQ_SEQ_NO] = Table1.[MQ_SEQ_NO]) |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-09-24 : 14:02:48
|
Here are a couple techniques to show differences between similar tables:declare @t1 table (pk int primary key clustered, v1 int, v2 int)declare @t2 table (pk int primary key clustered, v1 int, v2 int)set nocount oninsert @t1select 1,1,1 union allselect 2,1,1 union allselect 3,1,1 union allselect 5,1,1insert @t2select 1,1,1 union allselect 2,10,1 union allselect 3,1,1 union allselect 4,1,1print 'all rows in @t1 that don''t exist in @t2'select t1.*from @t1 t1left outer join @t2 t2 on t2.pk = t1.pkwhere t2.pk is nullprint 'all rows in @t2 that don''t exist in @t1'select t2.*from @t2 t2left outer join @t1 t1 on t1.pk = t2.pkwhere t1.pk is nullprint 'Show all differences'--If the PK is repeated that means that there is a difference in at least one of the columns.--If the PK appears only once then that row exists only in the table: "source"select min(src) as Source ,d.pk ,d.v1 ,d.v2from ( select src = 't1', pk,v1,v2 from @t1 union all select src = 't2', pk,v1,v2 from @t2 ) as dgroup by d.pk ,d.v1 ,d.v2having count(*) = 1order by d.pk ,min(src)OUTPUT:all rows in @t1 that don't exist in @t2pk v1 v2----------- ----------- -----------5 1 1all rows in @t2 that don't exist in @t1pk v1 v2----------- ----------- -----------4 1 1Show all differencesSource pk v1 v2------ ----------- ----------- -----------t1 2 1 1t2 2 10 1t2 4 1 1t1 5 1 1 Be One with the OptimizerTG |
 |
|
|
Dance Doll
Yak Posting Veteran
54 Posts |
Posted - 2009-09-24 : 15:46:32
|
| You also can download tablediff.exe.This is the cool tool to compare 2 tables. |
 |
|
|
|
|
|