x

Select and set in column

I got a simple select case with where condition, where Conditions are different values.
How Can I put those condition values into columns.
For exemple:

select fld1, fld, fld3 from tbl where val in (13, 14, 17)

I want to display those data in table configured like:

fld1| fld2|fld3| 13| 14| 17|
12 12 23 val val val
more ▼

asked Jun 09, 2011 at 07:55 AM in Default

Gogolo gravatar image

Gogolo
323 22 26 27

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

Would this do it or have I completely missed the point.

   SELECT
          fld1, fld2, fld3,
          CASE WHEN val = 13 THEN val ELSE 0 END AS [13],
          CASE WHEN val = 14 THEN val ELSE 0 END AS [14],
          CASE WHEN val = 17 THEN val ELSE 0 END AS [17]
FROM #Temp AS T
more ▼

answered Jun 09, 2011 at 08:08 AM

Mrs_Fatherjack gravatar image

Mrs_Fatherjack
4.7k 60 62 66

Works good... Thank you
Jun 09, 2011 at 11:22 PM Gogolo
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x108

asked: Jun 09, 2011 at 07:55 AM

Seen: 722 times

Last Updated: Jun 09, 2011 at 08:01 AM