SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CASE Statement in SELECT clause to convert into the where clause


CASE Statement in SELECT clause to convert into the where clause

Author
Message
lkennedy76
lkennedy76
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1700 Visits: 919
???

MCSA SQL Server 2012
Sean Lange
Sean Lange
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33258 Visits: 17678
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.

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)
lkennedy76
lkennedy76
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1700 Visits: 919
???

MCSA SQL Server 2012
Sean Lange
Sean Lange
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33258 Visits: 17678
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.

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)
math martinez
math martinez
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 62
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search