--per instance
WITH [cteFilteredSize]   AS (
                            SELECT [d].[Server Name],
                                   SUM([d].[UsedSpaceMB]) AS [filteredInstanceMBUsed]
                            FROM [dbo].[octpdba-931-detail] [d]
                            WHERE [d].[toIgnore] = 0
                            GROUP BY [d].[server Name]
                            ),
     [cteUnfilteredSize] AS (
                            SELECT [d].[Server Name],
                                   SUM([d].[UsedSpaceMB]) AS [unfilteredInstanceMBUsed]
                            FROM [dbo].[octpdba-931-detail] [d]
                            GROUP BY [d].[server Name]
                            ),
     [cteCurrentSize]    AS (
                            SELECT [g].[Server Name],
                                   SUM([g].[SpaceUsedMB]) [currentMbUsed]
                            FROM [dbo].[octpdba-931-global] [g]
                            GROUP BY [g].[Server Name]
                            ),
     [cteCompressedSize] AS (
                            SELECT [d].[Server Name],
                                   SUM([d].[compressEstimateSpaceMB]) AS [filteredInstanceMBCompressionEstimate]
                            FROM [dbo].[octpdba-931-detail] [d]
                            WHERE [d].[toIgnore] = 0
                            GROUP BY [d].[server Name]
                            )

SELECT [f].[Server Name]                                                                                                                                     AS [instance],
       cast([f].[filteredInstanceMBUsed] AS NUMERIC(25, 2))               AS [Filtered tables size in MB],
       cast([uf].[unfilteredInstanceMBUsed] AS NUMERIC(25, 2))            AS [Unfiltered tables size in MB],
        CAST([c].[currentMbUsed] AS NUMERIC(25, 2))                        AS [Current db size in MB],
        CAST([d].[filteredInstanceMBCompressionEstimate] AS NUMERIC(25, 2)) AS [Estimated compressed db size in MB]
FROM [cteFilteredSize] [f]
    INNER JOIN [cteUnfilteredSize] [uf] ON [uf].[Server Name] = [f].[Server Name]
    INNER JOIN [cteCurrentSize] [c] ON [c].[Server Name] = [f].[Server Name]
    INNER JOIN [cteCompressedSize] [d] ON [d].[Server Name] = [f].[Server Name]
ORDER BY [f].[Server Name]
;


--per instance and db
WITH [cteFilteredSize]   AS (
                            SELECT [d].[Server Name],
                                   [d].[dbName],
                                   SUM([d].[UsedSpaceMB]) AS [filteredInstanceMBUsed]
                            FROM [dbo].[octpdba-931-detail] [d]
                            WHERE [d].[toIgnore] = 0
                            GROUP BY [d].[server Name],
                                     [d].[dbName]
                            ),
     [cteUnfilteredSize] AS (
                            SELECT [d].[Server Name],
                                   [d].[dbName],
                                   SUM([d].[UsedSpaceMB]) AS [unfilteredInstanceMBUsed]
                            FROM [dbo].[octpdba-931-detail] [d]
                            GROUP BY [d].[server Name],
                                     [d].[dbName]
                            ),
     [cteCurrentSize]    AS (
                            SELECT [g].[Server Name],
                                   [g].[dbname],
                                   SUM([g].[SpaceUsedMB]) [currentMbUsed]
                            FROM [dbo].[octpdba-931-global] [g]
                            GROUP BY [g].[Server Name],
                                     [g].[dbname]
                            ),
     [cteCompressedSize] AS (
                            SELECT [d].[Server Name],
                                   [d].[dbName],
                                   SUM([d].[compressEstimateSpaceMB]) AS [filteredInstanceMBCompressionEstimate]
                            FROM [dbo].[octpdba-931-detail] [d]
                            WHERE [d].[toIgnore] = 0
                            GROUP BY [d].[server Name],
                                     [d].[dbName]
                            )

SELECT [f].[Server Name],
       [f].[dbName]                                                                                                                                          AS [db],
       REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, CAST([f].[filteredInstanceMBUsed] AS NUMERIC(25, 2))), 1), ',', ''''), '.00', '')                AS [Filtered tables size in MB],
       REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, CAST([uf].[unfilteredInstanceMBUsed] AS NUMERIC(25, 2))), 1), ',', ''''), '.00', '')             AS [Unfiltered tables size in MB],
       REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, CAST([c].[currentMbUsed] AS NUMERIC(25, 2))), 1), ',', ''''), '.00', '')                         AS [Current db size in MB],
       REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, CAST([d].[filteredInstanceMBCompressionEstimate] AS NUMERIC(25, 2))), 1), ',', ''''), '.00', '') AS [Estimated compressed db size in MB]
FROM [cteFilteredSize] [f]
    INNER JOIN [cteUnfilteredSize] [uf] ON [uf].[Server Name] = [f].[Server Name] AND [uf].[dbName] = [f].[dbName]
    INNER JOIN [cteCurrentSize] [c] ON [c].[Server Name] = [f].[Server Name] AND [c].[dbname] = [f].[dbName]
    INNER JOIN [cteCompressedSize] [d] ON [d].[Server Name] = [f].[Server Name] AND [d].[dbName] = [f].[dbName]
ORDER BY [f].[Server Name],
         [f].[dbName]
;
--per customer
WITH [cteFilteredSize]   AS (
                            SELECT [d].[Server Name],
                                   [d].[dbName],
                                   SUM([d].[UsedSpaceMB]) AS [filteredInstanceMBUsed]
                            FROM [dbo].[octpdba-931-detail] [d]
                            WHERE [d].[toIgnore] = 0
                            GROUP BY [d].[server Name],
                                     [d].[dbName]
                            ),
     [cteUnfilteredSize] AS (
                            SELECT [d].[Server Name],
                                   [d].[dbName],
                                   SUM([d].[UsedSpaceMB]) AS [unfilteredInstanceMBUsed]
                            FROM [dbo].[octpdba-931-detail] [d]
                            GROUP BY [d].[server Name],
                                     [d].[dbName]
                            ),
     [cteCurrentSize]    AS (
                            SELECT [g].[Server Name],
                                   [g].[dbname],
                                   SUM([g].[SpaceUsedMB]) [currentMbUsed]
                            FROM [dbo].[octpdba-931-global] [g]
                            GROUP BY [g].[Server Name],
                                     [g].[dbname]
                            ),
     [cteCompressedSize] AS (
                            SELECT [d].[Server Name],
                                   [d].[dbName],
                                   SUM([d].[compressEstimateSpaceMB]) AS [filteredInstanceMBCompressionEstimate]
                            FROM [dbo].[octpdba-931-detail] [d]
                            WHERE [d].[toIgnore] = 0
                            GROUP BY [d].[server Name],
                                     [d].[dbName]
                            )
SELECT CASE
           WHEN [d].[Server Name] LIKE 'ama%' OR [d].[Server Name] LIKE 'swam%' THEN 'ama'
           WHEN [d].[Server Name] LIKE 'cvi%' OR [d].[Server Name] LIKE 'swcv%' THEN 'cvi'
           WHEN [d].[Server Name] LIKE 'sun%' OR [d].[Server Name] LIKE 'swsu%' THEN 'sun'
           ELSE 'central'
       END                                                                                                                                                        AS [customer],
       CAST(SUM([f].[filteredInstanceMBUsed]) AS NUMERIC(25, 2))            AS [Filtered tables size in MB],
        CAST(SUM([uf].[unfilteredInstanceMBUsed]) AS NUMERIC(25, 2))           AS [Unfiltered tables size in MB],
        CAST(SUM([c].[currentMbUsed]) AS NUMERIC(25, 2))                 AS [Current db size in MB],
        CAST(SUM([d].[filteredInstanceMBCompressionEstimate]) AS NUMERIC(25, 2)) AS [Estimated compressed db size in MB]
FROM [cteFilteredSize] [f]
    INNER JOIN [cteUnfilteredSize] [uf] ON [uf].[Server Name] = [f].[Server Name] AND uf.[dbName] = f.[dbName]
    INNER JOIN [cteCurrentSize] [c] ON [c].[Server Name] = [f].[Server Name] AND c.[dbName] = f.[dbName]
    INNER JOIN [cteCompressedSize] [d] ON [d].[Server Name] = [f].[Server Name] AND d.[dbName] = f.[dbName]
GROUP BY CASE
             WHEN [d].[Server Name] LIKE 'ama%' OR [d].[Server Name] LIKE 'swam%' THEN 'ama'
             WHEN [d].[Server Name] LIKE 'cvi%' OR [d].[Server Name] LIKE 'swcv%' THEN 'cvi'
             WHEN [d].[Server Name] LIKE 'sun%' OR [d].[Server Name] LIKE 'swsu%' THEN 'sun'
             ELSE 'central'
         END
;
