x

Pivoting data excluding Null

Hi All, I have data as below: create table #t(product varchar(50),market varchar(50))
insert into #t
select 'Abilify','NEUROSCIENCE'
union
select 'Plavix','CV/MET'
union
select 'Avalide','CV/MET'

I wanta the following pivoted output:
[CV/MET] NEUROSCIENCE
Plavix Abilify
Avapro

If I use Case or Pivot it looks following where Null I don’t want
select case when market='NEUROSCIENCE' then product end NEUROSCIENCE,
case market when 'CV/MET' then product end [CV/MET]
from #t

NEUROSCIENCE [CV/MET]
Abilify NULL
NULL Avalide
NULL Plavix

thanks, ranjan

more ▼

asked Nov 03 '10 at 03:30 AM in Default

rkalita gravatar image

rkalita
21 2 2 3

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

1 answer: sort voted first

Try This Code i hope its work

create table #t(product varchar(50),market varchar(50)) 
insert into #t 
select 'Abilify','NEUROSCIENCE' 
union
select 'Plavix','CV/MET'
union
select 'Avalide','CV/MET'
SELECT V1.product AS [CV/MET], V2.product AS Neuroscience FROM
(SELECT ROW_NUMBER() OVER (ORDER BY product) AS x , product FROM #t WHERE market = 'CV/MET') AS V1
FULL OUTER JOIN (SELECT ROW_NUMBER() OVER (ORDER BY product) AS x , product FROM #t WHERE market = 'Neuroscience') AS V2
ON V1.X = V2.X
more ▼

answered Nov 03 '10 at 04:11 AM

Baligh gravatar image

Baligh
21

Thanks Baligh for your help.
Nov 03 '10 at 04:28 AM rkalita
you are welcome
Nov 03 '10 at 04:36 AM Baligh
(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:

x76

asked: Nov 03 '10 at 03:30 AM

Seen: 693 times

Last Updated: Nov 03 '10 at 03:37 AM