2016-07-07 14 views
-3

enter image description hereは、私は、テーブル内の行を比較し、不一致

私はすべての列とし、不一致すなわち会計、currency..etcの種類を指定する別の列と最後の3行を引くことができ、問合せをしたいを見つけたいです。

これを手伝ってもらえますか?

+3

を見ていますか?現在のクエリの試行と期待される結果を表示できますか? – jarlh

+0

質問にDDLを追加してください。質問するにはここをクリックしてください:spaghettidba.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 (ケース )gfd.ticker = gfd.tickerおよびgfd.segment = gfd.segmentおよびgfd.value1 = gfd .value1とgfd.value2 = gfd.value2とgfd.accounting <> gfd.accountingとgfd.currency = gfd.currencyとgfd.multiplier = gfd.multiplier THEN 'accounting_diff' END)AS 'mismatch_type'、gfd.user FROM companies_financial_data gfd ここでgfd.date_of_update '06/07/2016'と '07/07/2016'の間。 – Sasi

答えて

0

ない正確に何をしたいことを確認しかし、あなたがこれまでに試してみました何この

/* 
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  
関連する問題