0

I have a table called settings in that there are two columns and data like following.

settings           value
-------            ------
depots             12
projectid          156
client             icici bank
clientvisits       2
DBname             icicibank
progress           true
displayvisits      2
visitsskipped      0

Here , the values of rows clientvisits and displayvisits should be same always. I want a query which display the only these two fields along with values if their values are different. That means if clientvisits=2 and displayvisits=4 then it should throw an error (I mean it should display the output along with values).

I tried by using self join like this but it is not working.

select * from settings TS,settings TS1
where TS.settings=TS1.settings and CONVERT(integer,ts.value)=1

select * from settings TS,settings TS1
where TS.settings=TS1.settings and ts.setting like 'visitstodisplay'=1 

Also, I tried to do some thing else but I didn't get. Please let me know how to do it.

4
  • 1
    How does this involve MySQL and SSIS? Commented Jul 24, 2014 at 5:07
  • it doesnt involves MySql and SSis. as i am new to this site.so i have tagged them so that the people in that may give the better answer or query.
    – user19858
    Commented Jul 24, 2014 at 5:32
  • @user19858 Can you show us exactly what you want your output to look like? I can't work out if you want one row with values in different columns in that one row, or you want just those 2 rows, but only if the values are different... Commented Jul 24, 2014 at 7:54
  • Hi Mark, yes you are right i just want those two rows, only if the values are different
    – user19858
    Commented Jul 24, 2014 at 9:23

3 Answers 3

3

You can do a join on Value <> Value for the settings you are looking for.

select S1.Settings as Settings1,
       S1.Value as Value1,
       S2.Settings as Settings2,
       S2.Value as Value2
from Settings as S1
  inner join Settings as S2
    on S1.Value <> S2.Value and
       S1.Settings = 'clientvisits' and
       S2.Settings = 'displayvisits' 

Result:

Settings1            Value1               Settings2            Value2
-------------------- -------------------- -------------------- --------------------
clientvisits         2                    displayvisits        4         
0
0
SELECT * FROM (SELECT * FROM Settings WHERE Settings = 'displayvisits') d
INNER JOIN (SELECT * FROM Settings WHERE Settings = 'clientvisits') c ON d.Value <> c.Value

Will return you both settings in a single row.

0
-1
select
    TS.clientvisits,
    TS.displayvisits 
from settings TS,settings TS1
where 
    TS.settings=TS1.settings 
    and TS.clientvisits!=TS.displayvisits
1
  • Hi maddy, thank you for your answer but i have tried your query but it is not working as here we have to comapare two rows as clentvisits and displayvists are rows. but the query u have provided is for columns. as there are only two columns in this table they are settings and value in table setting
    – user19858
    Commented Jul 24, 2014 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.