dbTalk Databases Forums  

Transpose rows into columns

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Transpose rows into columns in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SeNa
 
Posts: n/a

Default Transpose rows into columns - 11-10-2010 , 09:02 AM






Hi All,

I have two tables as category and product

Category table as

categoryid categoryname

1 Sample

2 new


and product table as

prodid product category id
1 myprod 1
2 test prd 1
3 newprod 2
4 crprod 2


I just wanted to display the records like

Sample New
--------------------------
myprod newprod
test prod crprod


Please suggest me a solution

Thanks in Advance.

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Transpose rows into columns - 11-10-2010 , 09:41 AM






SeNa wrote:
Quote:
Hi All,

I have two tables as category and product

Category table as

categoryid categoryname

1 Sample

2 new


and product table as

prodid product category id
1 myprod 1
2 test prd 1
3 newprod 2
4 crprod 2


I just wanted to display the records like

Sample New
--------------------------
myprod newprod
test prod crprod


What version of SQL Server are you using?
I was going to suggest a pivot but you don't have a grouping column to
use as the row "header" (i.e., a column that relates myprod to newprod
and test prd to crprod). Assuming SQL 2005 or later, this will work:

WITH
new AS (
select ROW_NUMBER() OVER (ORDER BY prodid) AS RowNum
,product FROM product WHERE categoryid=2
), sample AS (
select ROW_NUMBER() OVER (ORDER BY prodid) AS RowNum
,product FROM product WHERE categoryid=1)
SELECT sample.product Sample,new.product New
FROM new FULL OUTER JOIN sample
ON new.rownum=sample.rownum

--
HTH,
Bob Barrows

Reply With Quote
  #3  
Old   
SeNa
 
Posts: n/a

Default Re: Transpose rows into columns - 11-10-2010 , 10:57 AM



On Nov 10, 8:41*pm, "Bob Barrows" <reb01... (AT) NOyahoo (DOT) SPAMcom> wrote:
Quote:
SeNa wrote:
Hi All,

I have two tables as category and product

Category table as

categoryid * *categoryname

1 * * * * * * * * Sample

2 * * * * * * * * *new

and product table as

prodid * product * * category id
1 * * * * *myprod * * * * 1
2 * * * * *test prd * * * * 1
3 * * * * *newprod * * * *2
4 * * * * *crprod * * * * * 2

I just wanted to display the records like

Sample * * *New
--------------------------
myprod * * *newprod
test prod * *crprod

What version of SQL Server are you using?
I was going to suggest a pivot but you don't have a grouping column to
use as the row "header" (i.e., a column that relates myprod to newprod
and test prd to crprod). Assuming SQL 2005 or later, this will work:

WITH
* * new AS (
* * * * select ROW_NUMBER() OVER (ORDER BY prodid) AS RowNum
* * * * ,product FROM product WHERE categoryid=2
* * ), sample AS (
* * * * select ROW_NUMBER() OVER (ORDER BY prodid) AS RowNum
* * * * ,product FROM product WHERE categoryid=1)
SELECT sample.product Sample,new.product New
FROM new FULL OUTER JOIN sample
ON new.rownum=sample.rownum

--
HTH,
Bob Barrows
Hi thanks for your reply.

I am using sql 2005

and the query should be dyanamic where i wanted to avoid modifications
in future.

whenever i add a category and product should reflect in data pull

Thanks

Reply With Quote
  #4  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Transpose rows into columns - 11-10-2010 , 11:20 AM



SeNa wrote:
Quote:
On Nov 10, 8:41 pm, "Bob Barrows" <reb01... (AT) NOyahoo (DOT) SPAMcom> wrote:
SeNa wrote:
Hi All,

I have two tables as category and product

Category table as

categoryid categoryname

1 Sample

2 new

and product table as

prodid product category id
1 myprod 1
2 test prd 1
3 newprod 2
4 crprod 2

I just wanted to display the records like

Sample New
--------------------------
myprod newprod
test prod crprod

What version of SQL Server are you using?
I was going to suggest a pivot but you don't have a grouping column
to use as the row "header" (i.e., a column that relates myprod to
newprod and test prd to crprod). Assuming SQL 2005 or later, this
will work:

WITH
new AS (
select ROW_NUMBER() OVER (ORDER BY prodid) AS RowNum
,product FROM product WHERE categoryid=2
), sample AS (
select ROW_NUMBER() OVER (ORDER BY prodid) AS RowNum
,product FROM product WHERE categoryid=1)
SELECT sample.product Sample,new.product New
FROM new FULL OUTER JOIN sample
ON new.rownum=sample.rownum

--
HTH,
Bob Barrows

Hi thanks for your reply.

I am using sql 2005

and the query should be dyanamic where i wanted to avoid modifications
in future.

whenever i add a category and product should reflect in data pull

The only way it can be dynamic is by using dynamic sql, i.e., creating a
string variable that contains the sql statement needed and using EXECUTE
or sp_executesql to execute it. I don't have the time right now to throw
a script together for you but here's a link that might prove helpful:
http://www.sommarskog.se/dynamic_sql.html

The idea is to use a cursor to loop through the Category table, using
each category id and name to generate each WITH statement as well as the
final select statement

--
HTH,
Bob Barrows

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.