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.
Author |
Topic |
san2008
Starting Member
4 Posts |
Posted - 2008-08-27 : 11:35:58
|
I have a database which contains tables with dynamic no. of columns. I had seen a lot of articles on the same but couldn't find any of them graspable or suitable (even the ones where some pivot is used,since I am new to this). I went through the pivot example at SQL Books Online but it was for aggregate functions and i can't understand how do i bring about the foll. output(for RS table data):-LandArea City Price650 Miami 30000250 Kansas 15000Some guidance on this issue will be greatly appreciated. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 13:23:25
|
without some source data its hard to tell what you're trying to do. Can you provide some sample data from your table please? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 13:24:31
|
May be you want this:-SELECT LandArea, City, SUM(Price)FROM YourTableGROUP BY LandArea, City |
 |
|
san2008
Starting Member
4 Posts |
Posted - 2008-08-30 : 11:23:03
|
quote: Originally posted by visakh16 without some source data its hard to tell what you're trying to do. Can you provide some sample data from your table please?
Sorry but I myself am confused how all the data i had entered has gone.Here is the data that i have to useTable- CMCM_ID FieldName10001 LandArea10002 City10003 PriceTable- TableMasterTM_ID Table_ID Column_ID 10001 RES 1000110002 RNT 1000210003 RES 1000310004 RES 1000210005 RNT 1000110006 RNT 10003Table- TableColumnValueMasterTCVM_ID TM_ID Value100001 10001 650100002 10003 30000100003 10004 Miami100004 10001 250100005 10003 15000100006 10004 Kansas100007 10005 450100008 10002 Kansas100009 10006 20000The columns CM_ID and Column_ID are PK and FK columns.And this is the output that is needed when 'RES' is the table name whose data we want.LandArea City Price650 Miami 30000250 Kansas 15000 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-30 : 14:07:53
|
[code]SELECT MAX(CASE WHEN t.FieldName='LandArea' THEN t.Value ELSE NULL END) AS LandArea,SELECT MAX(CASE WHEN t.FieldName='City' THEN t.Value ELSE NULL END) AS City,SELECT MAX(CASE WHEN t.FieldName='Price' THEN t.Value ELSE NULL END) AS PriceFROM(SELECT ROW_NUMBER() OVER (PARTITION BY tcvm.TM_ID ORDER BY tcvm.TCVM_ID ) AS RowNo,cm.FieldName,tcvm.Value,tm.Table_IDFROM TableMaster tmINNER JOIN CM cmON cm.CM_ID=tm.Column_IDINNER JOIN TableColumnValueMaster tcvmON tcvm.TM_ID =tm.TM_ID)tGROUP BY t.Table_ID,t.RowNo[/code] |
 |
|
san2008
Starting Member
4 Posts |
Posted - 2008-08-31 : 13:38:40
|
quote: Originally posted by visakh16
SELECT MAX(CASE WHEN t.FieldName='LandArea' THEN t.Value ELSE NULL END) AS LandArea,SELECT MAX(CASE WHEN t.FieldName='City' THEN t.Value ELSE NULL END) AS City,SELECT MAX(CASE WHEN t.FieldName='Price' THEN t.Value ELSE NULL END) AS PriceFROM(SELECT ROW_NUMBER() OVER (PARTITION BY tcvm.TM_ID ORDER BY tcvm.TCVM_ID ) AS RowNo,cm.FieldName,tcvm.Value,tm.Table_IDFROM TableMaster tmINNER JOIN CM cmON cm.CM_ID=tm.Column_IDINNER JOIN TableColumnValueMaster tcvmON tcvm.TM_ID =tm.TM_ID)tGROUP BY t.Table_ID,t.RowNo
Hi,I ran the queries that you had provided but there were some errors and it did not execute.Here is the exact error that SQL Server gave:Msg 156, Level 15, State 1, Line 2Incorrect syntax near the keyword 'SELECT'.Msg 156, Level 15, State 1, Line 3Incorrect syntax near the keyword 'SELECT'. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-31 : 23:57:26
|
aha...copy paste problemSELECT MAX(CASE WHEN t.FieldName='LandArea' THEN t.Value ELSE NULL END) AS LandArea, MAX(CASE WHEN t.FieldName='City' THEN t.Value ELSE NULL END) AS City, MAX(CASE WHEN t.FieldName='Price' THEN t.Value ELSE NULL END) AS PriceFROM(SELECT ROW_NUMBER() OVER (PARTITION BY tcvm.TM_ID ORDER BY tcvm.TCVM_ID ) AS RowNo,cm.FieldName,tcvm.Value,tm.Table_IDFROM TableMaster tmINNER JOIN CM cmON cm.CM_ID=tm.Column_IDINNER JOIN TableColumnValueMaster tcvmON tcvm.TM_ID =tm.TM_ID)tGROUP BY t.Table_ID,t.RowNo |
 |
|
Piyush
Starting Member
8 Posts |
Posted - 2011-07-25 : 01:54:27
|
Its a very easy way to transform the rows to column attributes, just go through the link:unspammedPiyush Bajaj |
 |
|
|
|
|
|
|