Hi Guys,I've got a bit of a query problem that I am not quite getting where to start, to be honest. I think I know what bits I need to use just not sure how to bash all the parts together:I have a table that contains snapshots of disk space once a day, for a bunch of servers:[code="sql"]CREATE TABLE [dbo].[mgmt_disk_free_space]( [rowguid] [uniqueidentifier] NOT NULL DEFAULT (newsequentialid()), [rowdate_utc] [date] NOT NULL DEFAULT (getutcdate()), [dayid] [smallint] NULL DEFAULT (datepart(dayofyear,getutcdate())), [server_name] [sysname] NOT NULL, [volume_tag] [sysname] NOT NULL, [capacity] [decimal](18, 0) NOT NULL, [free] [decimal](18, 0) NOT NULL, [used] AS ([capacity]-[free]), [usage_free_percent] AS (([free]/[capacity])*(100)), [usage_used_perecnt] AS ((([capacity]-[free])/[capacity])*(100)))[/code]From this data, I want to get a result set that can tell me: ... the change in disk space per server, per drive in the last 28 days (though if 28 days can be @interval tinyint that would be excellent too)... a guesstimate/projection of days left until a threshold is reached - that threshold would be either something like 10GB free space or a percentage instead (@threshold bigint)The two queries do not need to be in one statement, but if they are then awesome.So I partly do not know how to bolt this together as I said earlier but also I Am not quite sure how to think about the problem even!I have this rather basic query that gives me AVG(free) broken down by day/drive/server:[code="sql"]select server_name, volume_tag, dayid, avg(free)from [mgmt_disk_free_space]group by server_name, volume_tag, dayid[/code]But just not getting where to put in Lead and Lag, if indeed I need to use that. I'd rather use them over a self join. But even with a self join I am still not sure of the mechanics in the query I'm aiming! I managed to get this far:[code="sql"]select server_name, volume_tag, avg(free), change_in_free=(free-lag(isnull(free,0)) over (order by dayid))from [mgmt_disk_free_space]group by server_name, volume_tag, dayid, freeorder by server_name, volume_tag[/code]Before declaring I've no idea what I am doing now!So any help would be much appreciatedThanks!Alex
↧