Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Hep with case statement please...

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2012-03-20 : 09:12:54
I want to group certain DDSCodes. So if the doc is s67, s41 or s66 I want a column to be created called DDSGroup and put V01 next to them and if not just leave it as null.

This is what I want:



Doc Dist Reg Typecode DDSState DDSGroup
001 001 A 13 ME NULL
002 002 A 13 ME NULL
S67 Null C 7 ME V01
S41 Null C 7 PA V01
S66 Null C 7 PA V01



I want to add it to this query below. When I did I'm getting invlaid column name 'V01'


SELECT distinct(c.OfficeCode) AS DOC, CASE WHEN c.officecode in ('s67', 's41', 's66') then VO1 else Null end as DDSGroup, case WHEN TypeCode IN (1, 18) THEN ReportsTo WHEN TypeCode IN (13) THEN c.OfficeCode ELSE NULL END AS DIST, c.Region AS Reg,
r.RegionAcronym, CASE WHEN Area < 10 THEN '0' + CAST(Area AS Char(1)) ELSE CAST(Area AS Char(2)) END AS Area, REPLACE(c.Name, ',', '') AS DOC_NAME,
r.RegionNumber AS REGION_NUM, r.RegionName, c.TypeCode, a.addressstate as DDSState
FROM Offices.dbo.OfficeCodes AS c left JOIN
Offices.dbo.Regions AS r ON c.Region = r.RegionLetter
join offices.dbo.addr as a on a.officecode = c.officecode
WHERE (c.TypeCode IN (1, 18, 13, 42, 7))



What am I missing?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-20 : 09:23:33
Put single quotes around V01, otherwise SQL thinks you are referring to a column named V01.
WHEN c.officecode IN ('s67', 's41', 's66') THEN 'VO1'
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2012-03-20 : 09:41:50
Thanks!!!!
Go to Top of Page
   

- Advertisement -