2016-07-07 1 views
-3

enter image description hereIch mag die Zeilen in der Tabelle vergleichen und

ich die Abfrage will Mismatches finden, die letzten drei Zeilen mit allen Spalten und mit einer anderen Spalte, die currency..etc Art von Mismatch ziehen kann das heißt Buchhaltung angibt.

Kann mir jemand dabei helfen?

+3

Was haben Sie bisher versucht? Können Sie uns Ihren aktuellen Abfrageversuch und das erwartete Ergebnis zeigen? – jarlh

+0

fügen Sie bitte DDL für die Abfragen hinzu.Überprüfen Sie hier, wie Sie fragen können: spaghettiba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ – TheGameiswar

+0

SELECT gfd.ticker , gfd.segment, gfd.value1, gfd.value2, gfd.accounting, gfd.currency, gfd.multiplier (CASE WENN gfd.ticker = gfd.ticker und gfd.segment = gfd.segment und gfd.value1 = gfd .value1 und gfd.value2 = gfd.value2 und gfd.accounting <> gfd.accounting und gfd.currency = gfd.currency und gfd.multiplier = gfd.multiplier THEN 'accounting_diff' END) AS 'Mismatch_Type', gfd.user VON companies_financial_data gfd WHERE gfd.date_of_update ZWISCHEN '06/07/2016 'und '07/07/2016'; – Sasi

Antwort

0

Nicht sicher genau das, was Sie wollen, aber haben einen Blick auf diese

/* 
drop table t 
create table T (ticker VARCHAR(1), accounting varchar(1), currency varchar(1), multiplier varchar(1), dte date) 
truncate table t 
INSERT INTO T VALUES 
('a','p','u','m','2016-06-06'), 
('a','p','u','m','2016-06-07'), 
('b','g','c','m','2016-06-07'), 
('c','p','u','b','2016-06-07') 

*/ 

select u.ticker,u.accounting,u.currency,u.multiplier, 
     concat(diff,',',diff2,',',diff3) as diffs   
from 
(
select t.*, 
     case when t.accounting <> t.a2 or t.accounting <> t.a3 then 'Accounting' else '' end as diff, 
     case when t.currency <> t.c2 or t.currency <> t.c3 then 'Currency' else '' end as diff2, 
     case when t.multiplier <> t.m2 or t.multiplier <> t.m3 then 'Multiplier' else '' end as diff3 

from 
(

select s.*, 
     case when s.rn = 1 then lead(s.accounting, 1,0) OVER (ORDER BY s.rn) 
      when s.rn = 2 then lag(s.accounting, 1,0) OVER (ORDER BY s.rn) 
      when s.rn = 3 then lag(s.accounting, 1,0) OVER (ORDER BY s.rn) 
     end as a2, 

     case when s.rn = 1 then lead(s.accounting, 2,0) OVER (ORDER BY s.rn) 
      when s.rn = 2 then lead(s.accounting, 1,0) OVER (ORDER BY s.rn) 
      when s.rn = 3 then lag(s.accounting, 2,0) OVER (ORDER BY s.rn) 
     end as a3, 

     case when s.rn = 1 then lead(s.currency, 1,0) OVER (ORDER BY s.rn) 
      when s.rn = 2 then lag(s.currency, 1,0) OVER (ORDER BY s.rn) 
      when s.rn = 3 then lag(s.currency, 1,0) OVER (ORDER BY s.rn) 
     end as c2, 

     case when s.rn = 1 then lead(s.currency, 2,0) OVER (ORDER BY s.rn) 
      when s.rn = 2 then lead(s.currency, 1,0) OVER (ORDER BY s.rn) 
      when s.rn = 3 then lag(s.currency, 2,0) OVER (ORDER BY s.rn) 
     end as c3, 

     case when s.rn = 1 then lead(s.multiplier, 1,0) OVER (ORDER BY s.rn) 
      when s.rn = 2 then lag(s.multiplier, 1,0) OVER (ORDER BY s.rn) 
      when s.rn = 3 then lag(s.multiplier, 1,0) OVER (ORDER BY s.rn) 
     end as m2, 

     case when s.rn = 1 then lead(s.multiplier, 2,0) OVER (ORDER BY s.rn) 
      when s.rn = 2 then lead(s.multiplier, 1,0) OVER (ORDER BY s.rn) 
      when s.rn = 3 then lag(s.multiplier, 2,0) OVER (ORDER BY s.rn) 
     end as m3 

from 
(
select top 3 
     row_number() over (order by dte) rn, 
     t.ticker,t.accounting,t.currency,t.multiplier 
from t 
where dte = (select max(dte) from t) 
) s 
--order by s.rn 
) t 
) u