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 |
Bart111ac
Starting Member
7 Posts |
Posted - 2014-02-05 : 07:50:48
|
Hi,Thanks reading.I need to export data via SQL to XML. But I need the XML format in Row/column and the output must be a Pivot table. I know that it can be done with AS pivot. But I don't know how to use it in the below scriptI've already got a fantastic script from visakh16.:use Cluster_Data_Mart_NEW;With CTEAS(SELECT [Cluster_Name],[Cell_Name],[Client_Name],[Assigned]FROM CDM_Fact_Personnel_AssignedINNER JOIN CDM_Dim_Organization on CDM_Fact_Personnel_Assigned.Organization_Key = CDM_Dim_Organization.Organization_KeyINNER JOIN CDM_Dim_Client on CDM_Fact_Personnel_Assigned.Client_Key = CDM_Dim_Client.Client_Key)select(select [Cluster_Name] AS [@name],(select [Cell_Name] as [column],null as tmp,Client_Name as [column],null as tmp,Assigned as [column]from CTEwhere [Cluster_Name] = t.[Cluster_Name]for xml path('row'),type) as [*]from (select distinct [Cluster_Name] from CTE)tfor xml path('variable'),root('data')) as col_xmlthe above query needs to be in the same XML format(Row/Clolumn) but then in a Pivot table structure. The desired outcome:................CellA...CellBcompanyA..4.......3companyB..0.......4companyC..1.......2(whithout the ..............)The desired outcome in XML:<data><variable name="Cluster_Name"><row><column></column><column>CellA</column><column>CellB</column></row><row><column>companyA</column><column>4</column><column>3</column></row><row><column>companyB</column><column>0</column><column>4</column></row><row><column>companyC</column><column>1</column><column>2</column></row></variable></data>Is this possible?Thanks in advance.Regards,Bart |
|
Bart111ac
Starting Member
7 Posts |
Posted - 2014-02-07 : 08:21:42
|
;With CTEAS(SELECT [Cluster_Name],[Cell_Name],[Client_Name],[Assigned]FROM CDM_Fact_Personnel_AssignedINNER JOIN CDM_Dim_Organization onCDM_Fact_Personnel_Assigned.Organization_Key =CDM_Dim_Organization.Organization_KeyINNER JOIN CDM_Dim_Client on CDM_Fact_Personnel_Assigned.Client_Key =CDM_Dim_Client.Client_Key) , pvtClust as(select [Cluster_Name], [Client_Name],[CellA], [CellB]from CTEpivot (Sum(Assigned) for[Cell_Name] in ([CellA], [CellB])) pvt)select(select [Cluster_Name] AS [@name],(SELECT '' as [column], null as [tmp],'CellA' as [column], null as [tmp], 'CellB' as [column]for xml path ('row'), type) as [*],(select [Client_Name] as [column],null as tmp,ISNULL([CellA], 0) as [column],null as tmp,ISNULL([CellB], 0) as [column]from pvtClustwhere [Cluster_Name] = t.[Cluster_Name]for xml path('row'),type) as [*]from (select distinct [Cluster_Name] from CTE) tfor xml path('variable'),root('data')) as col_xmlCredits to:visakh16andRussel Loski, MCT, MCSE Data Platform/Business Intelligence. Twitter: @sqlmovers; blog: www.sqlmovers.com |
|
|
|
|
|
|
|