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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Generic function to transpose column to row

Author  Topic 

majkinetor
Yak Posting Veteran

55 Posts

Posted - 2008-10-07 : 11:10:12
Can anybody show me how can I use PIVOT to transpose column to row.

The column is:
SELECT Name FROM tProducts WHERE ProductID IN (@pid1, @pid2)

I want to get 2 names in a row.

If result is:

| Bike |
| Egg |

I want to get

| Bike | Egg |

I am aware of other solutions for this but I want to do it with PIVOT.

-----------------------------------
In the reminder of the thread it became clear that PIVOT is not good for this so I changed the subject to more meaningfull one.
-----------------------------------

Thx in advance.

www.r-moth.com          http://r-moth.deviantart.com

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-07 : 11:55:29
do you have any other columns in the table?
Go to Top of Page

majkinetor
Yak Posting Veteran

55 Posts

Posted - 2008-10-07 : 12:16:52
Imagine I have only ProductID and Name.

www.r-moth.com          http://r-moth.deviantart.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-07 : 12:53:12
then what value you want to pivot upon? Also what the value you need to aggregate?
Go to Top of Page

majkinetor
Yak Posting Veteran

55 Posts

Posted - 2008-10-08 : 04:08:47
I don't want to aggregate anything I want to transpose :)
I was reading that PIVOT can be used for that. It seems that I was wrong based to ur replies.



www.r-moth.com          http://r-moth.deviantart.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-08 : 05:36:38
quote:
Originally posted by majkinetor

I don't want to aggregate anything I want to transpose :)
I was reading that PIVOT can be used for that. It seems that I was wrong based to ur replies.



www.r-moth.com          http://r-moth.deviantart.com


it will transpose column values based on aggregation of some value. look into examples of books online to get details.
Go to Top of Page

majkinetor
Yak Posting Veteran

55 Posts

Posted - 2008-10-08 : 05:51:31
Ok, so I don't neeed that then.

Is there any function here that can transpose arbitrary row to column (and vice versa) ?

I was googling for it but all examples are case problems and have lots of hard-coded details.

I am talking about something like

Transpose( @table , "to row" | "to column")

where Transpose is TVP function transposing to row or column.



Thx.

www.r-moth.com          http://r-moth.deviantart.com
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-10-08 : 06:12:20
should the rows become the column names?, in which case what are you actually selecting? in a 'normal' pivot query that would be your aggregate.

some sample data / desired output would help illustrate it for us (including column names)

below is a quick / rough example with dynamic sql. it is purely a starting point but should be enough to give you an idea of how to proceed...


create table t (id int, prod varchar(20))
insert into t
values (1,'bike')
,(2,'egg')



declare @sql varchar(1000)

set @sql = 'select '
select @sql = @sql+ 'max(case when prod = ''' + prod + ''' then prod else null end),' from t

set @sql = LEFT(@sql,LEN(@sql)-1)
set @sql = @sql + ' from t'

print @sql

execute (@sql)


Em
Go to Top of Page
   

- Advertisement -