![]() |
|

SQL Monitor offers straightforward server monitoring through a web-based UI, to help you prioritize your workload:
Start monitoring your servers today with a free trial.
|
|
By: Dattatrey Sindol | Read Comments | Related Tips: More > Microsoft Excel Integration |
I have read a tip on Importing SQL Server Data from Multiple Data Sources into PowerPivot for Excel, which talks about bringing in data from two or more sources into PowerPivot and storing data from each source table/query as a separate PowerPivot table. I would like to know if there is a way to combine data from two or more databases/sources into one table in PowerPivot.
In this tip we will see how the above stated problem can be addressed. For the purpose of this tip, let's consider the following scenario.
We have a Sales Database which has a large amount of data. The data in this database is archived every year and stored in a separate database, which is on a different server. Let's assume that we have data starting from January 2010 and data for 2010 has been archived and stored in SalesDB_2010, data for 2011 has been archived and stored in SalesDB_2011, and the current data is present in SalesDB. All these three databases are present on different servers.
Let's first prepare the sample data for this demonstration using the following steps:
CREATE TABLE dbo.FactSales( Id INT IDENTITY(1,1) PRIMARY KEY , DateKey INT , ProductKey INT , SalesAmount MONEY ) GO
INSERT INTO dbo.FactSales (DateKey,ProductKey,SalesAmount) VALUES (20100101,123,1000.26) , (20100201,456,1003.20) , (20100301,789,1007.856) , (20100401,123,1011.66) , (20100501,456,1016.982) , (20100601,789,1020.594) , (20100701,123,1028.70) , (20100801,456,1039.3515) , (20100901,789,1043.217) , (20101001,123,1046.28) , (20101101,456,1049.118) , (20101201,789,1065.90) GO
INSERT INTO dbo.FactSales (DateKey,ProductKey,SalesAmount) VALUES (20110101,123,2016.882) , (20110201,456,2023.32) , (20110301,789,2024.994) , (20110401,123,2033.964) , (20110501,456,2039.994) , (20110601,789,2041.188) , (20110701,123,2045.8791) , (20110801,456,2084.6752) , (20110901,789,2092.56) , (20111001,123,2097.2945) , (20111101,456,2101.2043) , (20111201,789,2108.4343) GO
INSERT INTO dbo.FactSales (DateKey,ProductKey,SalesAmount) VALUES (20120101,123,3001.3125) , (20120201,456,3011.73) , (20120301,789,3032.3036) , (20120401,123,3050.946) , (20120501,456,3051.6096) , (20120601,789,3061.782) , (20120701,123,3117.87) , (20120801,456,3123.2728) , (20120901,789,3123.4376) , (20121001,123,3124.1301) , (20121101,456,3212.082) , (20121201,789,3222.7484) GO
Follow the below steps to extract the data from the above three data sources and consolidate them into a single PowerPivot table. Here we are basically doing a UNION ALL operation.
SELECT Id, DateKey, ProductKey, SalesAmount FROM OPENQUERY( SalesDB_2010 , 'SELECT Id, DateKey, ProductKey, SalesAmount FROM [SalesDB_2010].[dbo].[FactSales]' ) UNION ALL SELECT Id, DateKey, ProductKey, SalesAmount FROM OPENQUERY( SalesDB_2011 , 'SELECT Id, DateKey, ProductKey, SalesAmount FROM [SalesDB_2011].[dbo].[FactSales]' ) UNION ALL SELECT Id, DateKey, ProductKey, SalesAmount FROM [dbo].[FactSales]
Note that the primary data source is Server3 and the linked servers are created from this data source to the other two data sources Server1 and Server2, which contain the 2010 and 2011 data respectively. As we can see, we are using a simple OPENQUERY and UNION ALL in the above query, which is verified and executed in SSMS. Based on this, we can clearly say that pretty much any query which works in SSMS (when executed on the Primary Data Source which is Server3 for this example), can be used in PowerPivot.
To refresh the data in PowerPivot, go to the Home Tab in PowerPivot ribbon and click on the down-arrow of the Refresh button and select "Refresh" or "Refresh All" depending upon whether you want to refresh the current table or all the tables in PowerPivot respectively.
So, combining data from multiple servers/databases/tables/queries into a single PowerPivot table is as simple as that.
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |