mysql - How do I Compare columns of records from the same table? -
here testing table data:
testing
id name payment_date fee amt 1 banka 2016-04-01 100 20000 2 bankb 2016-04-02 200 10000 3 banka 2016-04-03 100 20000 4 bankb 2016-04-04 300 20000
i trying compare fields name, fee , amt of each data records see whether there same values or not. if got same value, i'd mark 'y' record. here expected result
id name payment_date fee amt samedataexistyn 1 banka 2016-04-01 100 20000 y 2 bankb 2016-04-02 200 10000 n 3 banka 2016-04-03 100 20000 y 4 bankb 2016-04-04 300 20000 n
i have tried these 2 methods below. looking other solutions can pick out best 1 work.
method 1.
select t.*, iif((select count(*) testing name=t.name , fee=t.fee , amt=t.amt)=1,'n','y') samedataexistyn testing t
method 2.
select t.*, case when ((b.name = t.name) , (b.fee = t.fee) , (b.amt = t.amt)) 'y' else 'n' end samedataexistyn testing t left join ( select name, fee, amt testing group name, fee, amt having count(*)>1 ) b on b.name = t.name , b.fee = t.fee , b.amt = t.amt
here method, think have run tests on data find out best:
select t.*, case when exists( select * testing id <> t.id , name = t.name , fee = t.fee , amt = t.amt ) 'y' else 'n' end samedataexistyn testing t ;
Comments
Post a Comment