Link to home
Start Free TrialLog in
Avatar of Bizzuka IT
Bizzuka ITFlag for United States of America

asked on

One Query Across Multiple Database, Using Multiple Tables to Generate SubTotals and Totals

I have "N" databases on a given server. Each database contains two tables from which I need to gather data from to generate a report with totals. The key here is that I need ONE SQL query (not stored procedure) to return the proper data for display in a tabular type report.

For the sake of discussion, let's say that each database represents a customer. So we have the following:
Database X
Database Y
Database Z

Each of the databases above have the following two tables:
TableA
TableB

TableA contains site information for that customer and in particular the following columns of interest for this discussion are:
URLPath = URL to customer site
SiteID = unique ID representing customer site

TableB contains the configuration of all the customer sites, like which components are viewable on that site. In particular the columns of interest are:
SiteID = The ID of the site this component belongs to
ComponentName = The name of the component
Enabled = Whether or not the component is enabled on that site

So, within a given database to retrieve the list of components that are enabled on each site, the query is simple

SELECT TableA.URLPath, TableB.ComponentName
FROM TableA
INNER JOIN TableB
ON TableB.SiteID = TableA.SiteID AND TableB.Enabled = 'Y'
ORDER BY TableB.ComponentName

However, if that was all I was looking for I would not have submitted this inquiry. What I am interested in is a result set using ONE SQL query that would give me the data such that the following tablular report can be generated. Notice that the report goes across databases on the server and generates subtotals for the components within each database and totals across the databases. That is the challenge!! Good luck!!

Customer Name: <Database X Name>

URL            ComponentX                          ComponentY                          ComponentZ
www.go123.com      1            0            1
www.go234.com      0            1            1
www.go456.com      0            0            1
SubTotal                            1            1            3

Customer Name: <Database Y Name>

URL            ComponentX                           ComponentY                          ComponentZ
www.xy123.com      1            0            0
www.xy234.com      1            1            1
www.xy456.com      1            1            1
SubTotal                           3            2            2

Customer Name: <Database Z Name>

URL            ComponentX                           ComponentY                          ComponentZ
www.ab123.com      0            0            1
www.ab234.com      1            0            0
www.ab456.com      1            0            1
SubTotal                           2            0            2

TOTAL            6            3            7
ASKER CERTIFIED SOLUTION
Avatar of GRayL
GRayL
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Create a view with UNION that gets the result-base you want. The report with grouping ...  you do on this view.

CREATE VIEW vwMyCrossDBView
AS
SELECT TableA.URLPath, TableB.ComponentName
FROM DATABASEX.DBO.TableA as tableA
INNER JOIN DATABASEX.DBO.TableB as tableB.....
UNION ALL
SELECT TableA.URLPath, TableB.ComponentName
FROM DATABASEY.DBO.TableA as tableA
INNER JOIN DATABASEZ.DBO.TableB as tableB.....
-- an select on the view
select URLPath,count(*)
from vwMyCrossDBView
group by URLPath
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin