Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

CASE Statement in SELECT clause to convert into the where clause Expand / Collapse
Author
Message
Posted Wednesday, November 14, 2012 10:32 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, July 8, 2014 10:09 AM
Points: 273, Visits: 550
???

MCSA SQL Server 2012
Post #1384766
Posted Wednesday, November 14, 2012 11:35 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:51 PM
Points: 13,028, Visits: 11,838
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1384783
Posted Wednesday, November 14, 2012 12:01 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, July 8, 2014 10:09 AM
Points: 273, Visits: 550
???

MCSA SQL Server 2012
Post #1384800
Posted Wednesday, November 14, 2012 12:21 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:51 PM
Points: 13,028, Visits: 11,838
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1384806
Posted Monday, November 19, 2012 11:26 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, December 19, 2013 12:26 AM
Points: 14, Visits: 52
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.
Post #1386689
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse