![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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? |
#3
| |||
| |||
|
|
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 |
#4
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |