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
 General SQL Server Forums
 New to SQL Server Programming
 select variable fields

Author  Topic 

dacow
Starting Member

5 Posts

Posted - 2010-12-10 : 16:16:38
Hi.
This is an example to explain my question. didnt put primary or foreign keys to make the example code more clean.


create table myColumn
( colId int,
colName varchar(30)
)

create table myRow
( rowId int,
rowName varchar(30)
)

create table myTable
( tableId int,
tableAuthor varchar(30)
)

create table myValues
( colId int,
rowId int,
quantity int
)

i want to build get registers in form of a table (with a select) like

rowName1 | quantity_for_col_id1 | quantity_for_col_id2 | quantity_for_col_id3 .....
rowName2 | quantity_for_col_id1 | quantity_for_col_id2 | quantity_for_col_id3 .....
rowName3 | quantity_for_col_id1 | quantity_for_col_id2 | quantity_for_col_id3 .....
RowName4 | quantity_for_col_id1 | quantity_for_col_id2 | quantity_for_col_id3 .....


note that table myValues only has registers when quantity has a value.
Lets say

col1 col2 col3
row1 23
row2
row3 73

only row1,col1 = 23 and row3,col2 = 73 will be saved as registers in myValue table.

So in the select i cant put

Select
col1
col2
col2
from ...

because col1,col2,... are variable, they are in the table columns.

any help on this, i just cant do it, been trying two days

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-12-10 : 16:59:00
Have you looked at the PIVOT operator in SQL Server Books Online? What version of SQL Server are you using?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-12-10 : 16:59:59
the table DDL is a good start

Please post some sample data in DML format

INSERT INTO Table(Cols)
SELECT data UNION ALL
SELECT data UNION ALL
ect

Then post an expected result you want to see



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -