x
login about faq Site discussion (meta-askssc)

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 '11 at 07:55 AM in Default

Gogolo gravatar image

Gogolo
313 12 21 25

(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 '11 at 08:08 AM

Mrs_Fatherjack gravatar image

Mrs_Fatherjack
4.6k 44 53 60

Works good... Thank you

Jun 09 '11 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.

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



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x87

asked: Jun 09 '11 at 07:55 AM

Seen: 446 times

Last Updated: Jun 09 '11 at 08:01 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.