/*============================================================================
  File:     2d_Baseline_NewWaitStats_QueryData.sql

  Summary:  This script creates the table to hold
			Wait Stats data, and the statement to use
			in a scheduled job to snapshot Wait Stats 
			regularly.


  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

/*
	View all data
*/
SELECT  *
FROM  [BaselineData].[dbo].[SQLskills_NewWaitStats]
ORDER BY [RowNum];


/*
	View the top wait for each collection
*/
SELECT  [w].[CaptureDate] ,
        [w].[WaitType] ,
        [w].[Percentage] ,
        [w].[Wait_S] ,
        [w].[WaitCount] ,
        [w].[AvgWait_S]
FROM    [dbo].[SQLskills_NewWaitStats] w
        JOIN ( SELECT   MIN([RowNum]) + 1 AS [RowNumber] ,
                        [CaptureDate]
               FROM     [dbo].[SQLskills_NewWaitStats]
               WHERE    [CaptureDate] IS NOT NULL
                        AND [CaptureDate] > GETDATE() - 60
               GROUP BY [CaptureDate]
             ) m ON [w].[RowNum] = [m].[RowNumber]
ORDER BY [w].[CaptureDate];

