Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

SQL View Compare Columns

  • 30-06-2005 2:29pm
    #1
    Registered Users, Registered Users 2 Posts: 604 ✭✭✭


    Howdy,

    I have a view that has 2 float columns. Lets call them colA and colB. I want to add another column that has 1 as its value if colA is greater than colB, and has 0 as its value if colA is less than colB.

    Ive tried IIF and if else statements but its always returned errors.

    The closest thing i have is
    SELECT dbo.table1.colA
    dbo.table1.Sales_Rate, 
    dbo.table2.colB,
    (
    Select 1 where (dbo.table1.colA > table2.colB)
    ) AS isAboveTarget
    FROM dbo.table1 CROSS JOIN
    dbo.table2
    

    table2 has only one row. This is on SQL Server 2000 by the way. The above code returns 1 if its greater and NULL if its not.


    Anyone got any ideas, im sure the answer is staring me in the face but everything ive tried so far doesnt work.


Comments

  • Registered Users, Registered Users 2 Posts: 1,423 ✭✭✭Merrion


    Haven't tried it but would this work:-
    SELECT dbo.table1.colA
    dbo.table1.Sales_Rate, 
    dbo.table2.colB,
    (
    Select Isnull(1,0) where (dbo.table1.colA > table2.colB)
    ) AS isAboveTarget
    FROM dbo.table1 CROSS JOIN
    dbo.table2
    
    


  • Registered Users, Registered Users 2 Posts: 604 ✭✭✭Kai


    Well that seems to be returning the same as my query, 1 where its greater and NULL when its less.
    I need something that will return 0 when its less. Thanks for the reply though.


  • Registered Users, Registered Users 2 Posts: 604 ✭✭✭Kai


    Just got it :
    (
    Select Isnull((Select 1 where (dbo.table1.colA > dbo.table2.colB)),0)
    ) AS isAboveTarget2

    thanks for the ponter :)


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Kai wrote:
    I have a view that has 2 float columns. Lets call them colA and colB. I want to add another column that has 1 as its value if colA is greater than colB, and has 0 as its value if colA is less than colB.

    ...
    Anyone got any ideas, im sure the answer is staring me in the face but everything ive tried so far doesnt work.

    Kai...

    read up on case statements. Would make a much easier solution.
    SELECT ColA,
    	   ColB,
    	   CASE
    		 WHEN ColA > ColB THEN 1
    		 WHEN ColB > ColA THEN 0
    		 ELSE null
    	     END as myComparisonColumn
    FROM   myView
    ...
    

    I threw in a third case to cater for when neither is greater (i.e. they are equal, or at least one of ColA and ColB is Null).

    CASE statements are your friend in MSSQL. Even if you're happy with your solution, I strongly recommend you learn them for this type of thing.

    jc


Advertisement