CASE Statement in SELECT clause to convert into the where clause

  • ???

    MCSE SQL Server 2012\2014\2016

  • worker bee (11/14/2012)


    Okay, I have a CASE stmt that is in the SELECT clause. I basically want to get rid of the CASE stmt and place the information in the where clause. I have done this but I get two of everything in my results. Please help!

    --OLD

    SELECT DISTINCT

    CASE a.COL1

    WHEN '26' THEN b.COLB

    END AS OUTPUT1,

    CASE a.COL1

    WHEN '27' THEN b.COL2

    END AS OUTPUT2

    FROM

    TBL1 a, TBL2 b

    WHERE

    a.COL1 = b.COL1 and

    a.COL2 = b.COL2 and

    a.COL1 > 1

    --NEW

    SELECT DISTINCT

    b.COL2 AS OUTPUT1

    b.COL2 AS OUTPUT2

    FROM

    TBL1 a, TBL2 b

    a.COL1 = b.COL1 and

    a.COL2 = b.COL2 and

    a.COL1 > 1 AND

    b.COL2 IN ('26', '27')

    Remember that we can't see your screen from here and we don't know what your tables or data look like. That being said those two queries are not doing anywhere near the same thing. Why do you think you need to move this to the where clause?

    I would recommend you use the newer ANSI style of join instead of the old style. They will produce the same result but is less error prone and more legible. Your first query would become something like this:

    SELECT DISTINCT

    CASE a.COL1 WHEN '26' THEN b.COLB END AS OUTPUT1

    ,CASE a.COL1 WHEN '27' THEN b.COL2 END AS OUTPUT2

    FROM TBL1 a

    inner join TBL2 b on a.COL1 = b.COL1 AND a.COL2 = b.COL2

    where a.COL1 > 1

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • ???

    MCSE SQL Server 2012\2014\2016

  • worker bee (11/14/2012)


    I updated with no results that are different. I still get two rows, 26 is the TAXABLE, TAX-DEF and the other is 27 SOLE, JOINT, OTHER. I had tried this before. Here is the query and the results;

    select distinct

    CASE aor.optional_category_cd

    WHEN '26' THEN aoc.optional_class_nm

    END AS AccountType,

    CASE aor.optional_category_cd

    WHEN '27' THEN aoc.optional_class_nm

    END AS AccountType

    from

    ACCOUNT_OPT_REF aor inner join ACCOUNT_OPT_CLASS aoc ON aor.Optional_Category_Cd = aoc.Optional_Category_Cd and

    aor.Optional_Class_Cd = aoc.Optional_Class_Cd

    where

    aor.Optional_Class_Cd > 1

    AccountType AccountType

    TAXABLE NULL

    NULL SOLE

    TAX-DEFERRED/NOT TAX ON WDRAWL NULL

    NULL JOINT

    TAX-DEFERRED & TAX ON WDRAWL NULL

    NULL OTHER

    NULL NULL

    As I said before I can't see your screen, I have no idea what your ddl is like, I have no sample data to work with and I have no idea what you want as output. Please see the first article in my signature about best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Using case statement is the best option in your scenario..you said that you always get more result? you should check your grouping maybe that was the problem.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply