/*============================================================================
  File:     4b_Baselines_FileData_QueryData.sql

  Summary:  This script contains the statements to
			regularly query the captured file data.

  Date:     April 2015

  SQL Server Version: 2005/2008/2008R2/2012/2014
------------------------------------------------------------------------------
  Written by Erin Stellato, SQLskills.com

  (c) 2015, SQLskills.com. All rights reserved.

  For more scripts and sample code, check out 
    http://www.SQLskills.com

  You may alter this code for your own *non-commercial* purposes. You may
  republish altered code as long as you include this copyright and give due
  credit, but you must obtain prior permission before blogging this code.
  
  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.
============================================================================*/

USE [BaselineData];
GO

SELECT  [DatabaseName] ,
        [FileID] ,
        [DriveLetter] ,
        [LogicalFileName] ,
        [PhysicalFileName] ,
        [SizeMB] ,
        [SpaceUsedMB] ,
        [FreeSpaceMB] ,
        CAST([FreeSpaceMB] / [SizeMB] * 100 AS DECIMAL(38, 2)) AS 'PercentFree' ,
        CASE WHEN [MaxSize] = 0
             THEN 'Max size = ' + CAST([SizeMB] / 128.00 AS VARCHAR(15))
                  + ' MB'
             WHEN [MaxSize] = -1 THEN 'No max size set'
             WHEN [MaxSize] = 268435456 THEN 'Max size = 2 TB'
             ELSE 'Max size = '
                  + CAST(CAST([MaxSize] / 128.00 AS DECIMAL(38, 2)) AS VARCHAR(15))
                  + ' MB'
        END AS "MaximumSize" ,
        CASE WHEN [Growth] = 0 THEN 'No growth'
             WHEN [Growth] > 0
                  AND [IsPercentGrowth] = 1
             THEN CAST([Growth] AS VARCHAR(15)) + '%'
             WHEN [Growth] > 0
                  AND [IsPercentGrowth] = 0
             THEN CAST(CAST([Growth] / 128.00 AS DECIMAL(38, 2))  AS VARCHAR(15))
                  + ' MB'
        END AS 'AutoGrowth' ,
        [CaptureDate]
FROM    [dbo].[SQLskills_FileInfo]
WHERE   CONVERT(VARCHAR(10), [CaptureDate], 112) = CONVERT(VARCHAR(10), GETDATE(), 112)
ORDER BY [PercentFree] ASC


/*
	optional predicate
*/
SELECT  [DatabaseName] ,
        [FileID] ,
        [DriveLetter] ,
        [LogicalFileName] ,
        [PhysicalFileName] ,
        [SizeMB] ,
        [SpaceUsedMB] ,
        [FreeSpaceMB] ,
        CAST([FreeSpaceMB] / [SizeMB] * 100 AS DECIMAL(38, 2)) AS 'PercentFree' ,
        CASE WHEN [MaxSize] = 0
             THEN 'Max size = ' + CAST([SizeMB] / 128.00 AS VARCHAR(15))
                  + ' MB'
             WHEN [MaxSize] = -1 THEN 'No max size set'
             WHEN [MaxSize] = 268435456 THEN 'Max size = 2 TB'
             ELSE 'Max size = '
                  + CAST(CAST([MaxSize] / 128.00 AS DECIMAL(38, 2)) AS VARCHAR(15))
                  + ' MB'
        END AS "MaximumSize" ,
        CASE WHEN [Growth] = 0 THEN 'No growth'
             WHEN [Growth] > 0
                  AND [IsPercentGrowth] = 1
             THEN CAST([Growth] AS VARCHAR(15)) + '%'
             WHEN [Growth] > 0
                  AND [IsPercentGrowth] = 0
             THEN CAST(CAST([Growth] / 128.00 AS DECIMAL(38, 2))  AS VARCHAR(15))
                  + ' MB'
        END AS 'AutoGrowth' ,
        [CaptureDate]
FROM    [dbo].[SQLskills_FileInfo]
WHERE [DatabaseName] = N'[HumanResources]'
	AND FileID = 1
ORDER BY [FileID], [CaptureDate];

/*
	total db size
*/
SELECT  [DatabaseName] ,
        SUM([SizeMB]) AS 'DatabaseSizeMB' ,
        SUM([SpaceUsedMB]) AS 'SpaceUsedMB' ,
        SUM([FreeSpaceMB]) AS 'FreeSpaceMB' ,
        CAST(SUM([FreeSpaceMB]) / SUM([SizeMB]) * 100 AS DECIMAL(38, 2)) AS 'PercentFree' ,
        [CaptureDate]
FROM    [dbo].[SQLskills_FileInfo]
WHERE   [DatabaseName] = N'[HumanResources]'
GROUP BY [DatabaseName] ,
        [CaptureDate]
ORDER BY [CaptureDate] DESC;

/*
	db size, exclude logs
*/
SELECT  [DatabaseName] ,
        SUM([SizeMB]) AS 'DatabaseSizeMB' ,
        SUM([SpaceUsedMB]) AS 'SpaceUsedMB' ,
        SUM([FreeSpaceMB]) AS 'FreeSpaceMB' ,
        CAST(SUM([FreeSpaceMB]) / SUM([SizeMB]) * 100 AS DECIMAL(38, 2)) AS 'PercentFree' ,
        [CaptureDate]
FROM    [dbo].[SQLskills_FileInfo]
WHERE   [DatabaseName] = N'[HumanResources]'
        AND [Type] <> 1
GROUP BY [DatabaseName] ,
        [CaptureDate]
ORDER BY [CaptureDate] DESC;


/*
	files by drive
*/
SELECT DISTINCT
        ( [vs].[logical_volume_name] ) AS 'Drive' ,
        [vs].[available_bytes] / 1048576 AS 'MBFree'
FROM    [sys].[master_files] AS f
        CROSS APPLY [sys].[dm_os_volume_stats]([f].[database_id],
                                               [f].[file_id]) AS vs
ORDER BY [vs].[logical_volume_name];