|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 29, 2013 2:07 PM
Points: 21,
Visits: 137
|
|
I need some help in pivoting the table. The table is like this: File Main Sub Text 1 1 A. hello 1 1 B. SQL 5 1 A. central 5 1 B. .com
The modified table look like this
File Main A. B. 1 1 hello SQL 5 1 central .com
All text rows related to A come under column A and same B.
i tried a lot with different Pivoting queries but i'm getting either NULLS or empty fields.
Can anyone suggest me how to get this.
Thanks in Advance!
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Yesterday @ 11:04 PM
Points: 1,456,
Visits: 14,263
|
|
Hi
will you only "ever" have columns A and B to pivot on?
__________________________________________________________________ you can lead a user to data....but you cannot make them think ! __________________________________________________________________
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 1:36 PM
Points: 887,
Visits: 2,062
|
|
If it fixed number of columns classic cross tab should work:
CREATE TABLE #Temp([File] INT, Main INT, Sub CHAR(2), [Text] VARCHAR(20)); INSERT INTO #Temp ([File], Main, Sub, [Text]) VALUES (1, 1, 'A.', 'hello'), (1, 1, 'B.', 'SQL'), (5, 1, 'A.', 'central'), (5, 1, 'B.', '.com');
SELECT [File] ,Main ,[A.] = MAX(CASE WHEN Sub = 'A.' THEN [Text] END) ,[B.] = MAX(CASE WHEN Sub = 'B.' THEN [Text] END) FROM #Temp GROUP BY [File], Main;
DROP TABLE #Temp;
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 29, 2013 2:07 PM
Points: 21,
Visits: 137
|
|
| Yes, I have only two group of values "A" and "B" need to be pivoted
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Yesterday @ 11:04 PM
Points: 1,456,
Visits: 14,263
|
|
ashwinrao.k23 (8/10/2012) Yes, I have only two group of values "A" and "B" need to be pivoted
ok...in which case the solution from rVadim will work for you.
for more info, suggest you search under the "Authors" (left sidebar) for Jeff Moden and his articles on crosstabs and pivots
__________________________________________________________________ you can lead a user to data....but you cannot make them think ! __________________________________________________________________
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 29, 2013 2:07 PM
Points: 21,
Visits: 137
|
|
thanks for the Reply! If i use Aggregate functions then its eating some of my data fileds. For MAX its giving only maximim valuse of available filds..
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Yesterday @ 11:04 PM
Points: 1,456,
Visits: 14,263
|
|
ashwinrao.k23 (8/10/2012) thanks for the Reply! If i use Aggregate functions then its eating some of my data fileds. For MAX its giving only maximim valuse of available filds..
not quite sure what data fields are being "eaten"....maybe if you provide a script of the table....in the format that rVadim set out above, with a representative sample of data that demonstrates your problem (and expected results please)...then I am sure someone will be able to help
__________________________________________________________________ you can lead a user to data....but you cannot make them think ! __________________________________________________________________
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 1:36 PM
Points: 887,
Visits: 2,062
|
|
Exactly!
If sample you provided doesn't accurately reflect you actual situation then you need to provide a better sample.
People will be more willing to help if you include CREATE TABLE and INSERT statement to generate sample data.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Yesterday @ 11:04 PM
Points: 1,456,
Visits: 14,263
|
|
ashwinrao.k23 (8/10/2012) thanks for the Reply! If i use Aggregate functions then its eating some of my data fileds. For MAX its giving only maximim valuse of available filds..
hmm...just a thought, but by any chance do you have data like this (based on your original description)
The table is like this: File Main Sub Text 1 1 A. hello 1 1 B. SQL 5 1 A. central 5 1 B. .com 1 1 A. hello_again 1 1 B. SQL_again
__________________________________________________________________ you can lead a user to data....but you cannot make them think ! __________________________________________________________________
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 29, 2013 2:07 PM
Points: 21,
Visits: 137
|
|
| No. all fields will not repeat as same. Text may be same but File and Main will be different..
|
|
|
|