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/