SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help in how to use CASE
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tlk072107
Starting Member

4 Posts

Posted - 06/27/2012 :  11:33:52  Show Profile  Reply with Quote
I am relatively new to sql, and I am trying to write a query that requires (I believe) the use of the CASE clause but I am not sure how to put this in.

What I am doing is returning from a table 3 columns, A, B and C. In Column C I will get back one of two values from the table.

I do not want my results to show these values, but rather two different values I assign.

How do I do this?

So I will have something like this:

SELECT [A], COUNT (A) as ColumnName
,[B] as ColumnName
,[C]
FROM [table]
WHERE OrderId = '##########'
Group By A, B, C

And I need to change each of the two possible integer values returning from column C into text values I assign.

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 06/27/2012 :  11:44:23  Show Profile  Reply with Quote
Something like this:
SELECT
	[A],
	COUNT(A) AS ColumnName,
	[B] AS ColumnName,
	CASE 
		WHEN [C] = 0 THEN 'Zero'
		WHEN [C] = 1 THEN 'One'
	END
FROM
	[table]
WHERE
	OrderId = '##########'
GROUP BY
	A,
	B,
	CASE 
		WHEN [C] = 0 THEN 'Zero'
		WHEN [C] = 1 THEN 'One'
	END
If there could be nulls or other values in column C, you would want to add an else condition to the CASE expression
Go to Top of Page

tlk072107
Starting Member

4 Posts

Posted - 06/27/2012 :  11:57:45  Show Profile  Reply with Quote
This works perfectly, thank you! I will always have one of those two values for C, so no need for ELSE.

Where do I name Column C, does this occur after "CASE"?

quote:
Originally posted by sunitabeck

Something like this:
SELECT
	[A],
	COUNT(A) AS ColumnName,
	[B] AS ColumnName,
	CASE 
		WHEN [C] = 0 THEN 'Zero'
		WHEN [C] = 1 THEN 'One'
	END
FROM
	[table]
WHERE
	OrderId = '##########'
GROUP BY
	A,
	B,
	CASE 
		WHEN [C] = 0 THEN 'Zero'
		WHEN [C] = 1 THEN 'One'
	END
If there could be nulls or other values in column C, you would want to add an else condition to the CASE expression

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 06/27/2012 :  12:56:02  Show Profile  Reply with Quote
You are very welcome .) To name the column, add an alias after the END keyword of the CASE EXPRESSION.
...
	[B] AS ColumnName,
	CASE 
		WHEN [C] = 0 THEN 'Zero'
		WHEN [C] = 1 THEN 'One'
	END  AS NewColumnName
FROM
....

Edited by - sunitabeck on 06/27/2012 12:57:26
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000