| Author |
Topic |
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-10-27 : 10:09:19
|
Hello All I have an intermediate table result which looks like this ID Header CONTENT GUID 1 Header1 123456 GUID 1 Header2 Phone GUID 1 Header3 Main Street GUID 2 Header1 654321 GUID 2 Header2 Phone GUID 3 Header3 Some Street I need to transform this result set into something like this ID Header1 Header2 Header3 GUID 1 123456 Phone Main Street GUID 2 654321 Phone Some Street I guess i have to use PIVOTing for the same. But im stuck in the point where i cannot use the COUNT/SUM operator inorder to get the CONTENT column.Can anyone help me with the query for achieving the result ???Thanks in advance |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-10-27 : 10:20:34
|
| [code]declare @t table(ID varchar(100),Header varchar(100),CONTENT varchar(100))insert into @tselect 'GUID 1', 'Header1','123456' union allselect 'GUID 1', 'Header2','Phone' union allselect 'GUID 1', 'Header3','Main Street' union allselect 'GUID 2', 'Header1','654321' union allselect 'GUID 2', 'Header2','Phone' union allselect 'GUID 3', 'Header3','Some Street'select * from( select id,header,content from @t) as tpivot( max(Content) for Header in ([Header1],[Header2],[Header3])) as p[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-10-27 : 10:34:25
|
| Hello Madhavani got the hint from your queryi just had to use MAX keyword ...Thanks a lot .. Srivatsa |
 |
|
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-10-27 : 10:45:31
|
Also, can we use Subqueries inside the PIVOT ???i.e i want to use something like this pivot( max(Content) for Header in (Select Header from HeaderTable)) as p The above query returns error, i just need to know whether such a case is possible to select entries dynamically from a select statementRegardsSrivatsa |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
|
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-10-27 : 11:41:32
|
| Thanks I was able to do it.But i have one more issue now, Im not sure whether i should create a new topic for the same or include in this topic.I need to export the so formed data into an excel file. The problem is the SQL Import/Export Wizard is not able to parse the dynamically generated QUERY, since it says i cannot allow DECLARE statements.Also i tried to create a view ,but in vain..Any help in exporting the resulting data into the excel format is deeply appreciated.RegardsSrivatsa |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-10-27 : 11:54:48
|
| Excel has its own pivot functionality. Can't you just link to the raw table and do the pivot in excel? Otherwise perhaps use the sql pivot statement as a SELECT INTO statement to create a pivoted table.Be One with the OptimizerTG |
 |
|
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-10-28 : 04:30:52
|
TGThanks for replying.Actually i know that excel has inbuilt PIVOT functionality but i cannot use the same. Since, my requirement is to to automate the process of pulling the data from SQL and then exporting it to excel/csv.I tried the following options to export in vain.- Export using Views :Creating a view with the query which does the pivoting dynamically, SQL views do not allow DECLARE
- SQL Export using Query : Query parse fails to parse the DECLARE statements
- Importing the data from SQL into Excel: Fail
- Using : bcp, this returns Query successful, but does not export the data for some reason
Any more options you would suggestRegardsSrivatsa |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-10-28 : 10:24:22
|
| I'm not really an excel guy but I would think this should work:- (in excel) link to external data which is your raw, unpivoted data in sql.- (in different worksheet in excel) set up your pivot table using the (linked) data in step one.Then whenever you want to see the current data just "refresh". I think you can set the properties of the excel file to either automatically refresh or not.Be One with the OptimizerTG |
 |
|
|
|