-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSQLStrings.cs
More file actions
580 lines (535 loc) · 26 KB
/
SQLStrings.cs
File metadata and controls
580 lines (535 loc) · 26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
using System;
using System.Collections.Generic;
using System.Text;
namespace Symantec.CWoC.PatchTrending {
public static class SQLStrings {
#region string sql_spTrendInactiveComputers = @"...
public static string sql_drop_spTrendInactiveComputers = @"
if exists(select 1 from sys.objects where name = 'spTrendInactiveComputers' and type = 'P')
begin
drop procedure spTrendInactiveComputers
end
";
public static string sql_spTrendInactiveComputers = @"
create procedure spTrendInactiveComputers
@force as int = 0,
@collectionguid as uniqueidentifier = '01024956-1000-4CDB-B452-7DB0CFF541B6'
as
if not exists (select 1 from sys.objects where type = 'u' and name = 'TREND_InactiveComputerCounts')
begin
create table TREND_InactiveComputerCounts (
[_exec_id] int not null,
[timestamp] datetime not null,
[collectionguid] uniqueidentifier not null,
[Managed machines] int not null,
[Inactive computers (7 days)] int not null,
[New Inactive computers] int not null,
[New Active computers] int not null,
[Inactive computers (17 days)] int not null
)
end
if not exists (select 1 from sys.objects where type = 'u' and name = 'TREND_InactiveComputer_Current')
begin
CREATE TABLE [TREND_InactiveComputer_Current] (guid uniqueidentifier not null, collectionguid uniqueidentifier not null, _exec_time datetime not null)
CREATE UNIQUE CLUSTERED INDEX [IX_TREND_InactiveComputer_Current] ON [dbo].[TREND_InactiveComputer_Current]
(
[CollectionGuid] ASC, [Guid] ASC
)
end
if not exists (select 1 from sys.objects where type = 'u' and name = 'TREND_InactiveComputer_Previous')
begin
CREATE TABLE [TREND_InactiveComputer_Previous] (guid uniqueidentifier not null, collectionguid uniqueidentifier not null, _exec_time datetime not null)
CREATE UNIQUE CLUSTERED INDEX [IX_TREND_InactiveComputer_Previous] ON [dbo].[TREND_InactiveComputer_Previous]
(
[CollectionGuid] ASC, [Guid] ASC
)
end
if ((select MAX(_exec_time) from TREND_InactiveComputer_Current where _exec_time > dateadd(hour, -23, getdate())) is null) or (@force = 1)
begin
-- STAGE 1: If we have current data, save it in the _previous table
if (select count (*) from TREND_InactiveComputer_Current where CollectionGuid = @CollectionGuid) > 0
begin
delete from TREND_InactiveComputer_Previous where CollectionGuid = @CollectionGuid
insert TREND_InactiveComputer_Previous (guid, collectionguid, _exec_time)
select * from TREND_InactiveComputer_Current where CollectionGuid = @CollectionGuid
-- STAGE 2: Insert current data in the current table
delete from TREND_InactiveComputer_Current where CollectionGuid = @CollectionGuid
insert TREND_InactiveComputer_Current (guid, CollectionGuid, _exec_time)
SELECT DISTINCT(c.ResourceGuid) as 'Guid', @collectionguid, getdate()
FROM CollectionMembership c
INNER JOIN (
select [ResourceGuid]
from dbo.ResourceUpdateSummary
where InventoryClassGuid = '9E6F402A-6A45-4CBA-9299-C2323F73A506'
group by [ResourceGuid]
having max([ModifiedDate]) < GETDATE() - 7
) as dt
ON c.ResourceGuid = dt.ResourceGuid
WHERE c.CollectionGuid = @CollectionGuid
--STAGE 3: Extract the add/drop counts and insert data in the trending table
declare @added as int, @removed as int
-- Added in c
set @added = (
select count(*)
from TREND_InactiveComputer_Current c
full join TREND_InactiveComputer_Previous p
on p.guid = c.guid
where p.guid is null
and p.collectionguid = @CollectionGuid
)
-- Removed in c
set @removed = (
select count(*)
from TREND_InactiveComputer_Current c
full join TREND_InactiveComputer_Previous p
on p.guid = c.guid
where c.guid is null
and p.CollectionGuid = @CollectionGuid
)
declare @managed as int, @inactive_1 as int, @inactive_2 as int
set @managed = (select count(distinct(ResourceGuid)) from CollectionMembership where CollectionGuid = @CollectionGuid)
set @inactive_1 = (
select count(distinct(c.ResourceGuid))
from CollectionMembership c
INNER JOIN
(
select [ResourceGuid]
from dbo.ResourceUpdateSummary
where InventoryClassGuid = '9E6F402A-6A45-4CBA-9299-C2323F73A506'
group by [ResourceGuid]
having max([ModifiedDate]) < GETDATE() - 7
) as dt
ON c.ResourceGuid = dt.ResourceGuid
where c.CollectionGuid = @collectionguid
)
set @inactive_2 = (
select count(distinct(c.ResourceGuid))
from CollectionMembership c
INNER JOIN
(
select [ResourceGuid]
from dbo.ResourceUpdateSummary
where InventoryClassGuid = '9E6F402A-6A45-4CBA-9299-C2323F73A506'
group by [ResourceGuid]
having max([ModifiedDate]) < GETDATE() - 17
) as dt
ON c.ResourceGuid = dt.ResourceGuid
where c.CollectionGuid = @collectionguid
)
declare @execid as int
set @execid = (select isnull(max(_exec_id), 0) from TREND_InactiveComputerCounts where collectionguid = @collectionguid) + 1
insert TREND_InactiveComputerCounts (_exec_id, timestamp, collectionguid, [Managed machines], [inactive computers (7 days)], [New Inactive Computers], [New Active Computers], [Inactive Computers (17 days)])
values (@execid, getdate(), @collectionguid, @managed, @inactive_1, @added, @removed, @inactive_2)
end
else
begin
delete from TREND_InactiveComputer_Current where collectionguid = @collectionguid
insert TREND_InactiveComputer_Current (guid, collectionguid, _exec_time)
select distinct(c.ResourceGuid) as 'Guid', @collectionguid, getdate()
from CollectionMembership c
INNER JOIN
(
select [ResourceGuid]
from dbo.ResourceUpdateSummary
where InventoryClassGuid = '9E6F402A-6A45-4CBA-9299-C2323F73A506'
group by [ResourceGuid]
having max([ModifiedDate]) < GETDATE() - 17
) as dt
ON c.ResourceGuid = dt.ResourceGuid
where c.CollectionGuid = @collectionguid
end
end
select * from TREND_InactiveComputerCounts where CollectionGuid = @collectionguid order by _exec_id desc
";
#endregion
#region string sql_spTrendPatchComplianceByComputer = @"...
public static string sql_drop_spTrendPatchComplianceByComputer = @"
if exists(select 1 from sys.objects where name = 'spTrendPatchComplianceByComputer' and type = 'P')
begin
drop procedure spTrendPatchComplianceByComputer
end
";
public static string sql_spTrendPatchComplianceByComputer = @"
create procedure spTrendPatchComplianceByComputer
@collectionguid as uniqueidentifier = '01024956-1000-4cdb-b452-7db0cff541b6',
@force as int = 0
as
if (not exists(select 1 from sys.objects where name = 'PM_TRENDS2_TEMP' and type = 'U'))
begin
CREATE TABLE [dbo].[PM_TRENDS2_TEMP](
[_ResourceGuid] [uniqueidentifier] NOT NULL,
[Computer Name] [varchar](250) NOT NULL,
[Compliance] [numeric](6, 2) NULL,
[Applicable (Count)] [int] NULL,
[Installed (Count)] [int] NULL,
[Not Installed (Count)] [int] NULL,
[Restart Pending] [varchar](3) NOT NULL,
[_DistributionStatus] [nvarchar](16) NULL,
[_OperatingSystem] [nvarchar](128) NULL,
[_StartDate] [datetime] NULL,
[_EndDate] [datetime] NULL,
) ON [PRIMARY]
end
if (not exists(select 1 from sys.objects where type = 'U' and name = 'TREND_WindowsCompliance_ByComputer'))
begin
CREATE TABLE [dbo].[TREND_WindowsCompliance_ByComputer](
[_Exec_id] [int] NOT NULL,
[CollectionGuid] [uniqueidentifier] NOT NULL,
[_Exec_time] [datetime] NOT NULL,
[Percent] int NOT NULL,
[Computer #] int NOT NULL,
[% of Total] money NOT NULL,
) ON [PRIMARY]
CREATE UNIQUE CLUSTERED INDEX [IX_TREND_WindowsCompliance_ByComputer] ON [dbo].[TREND_WindowsCompliance_ByComputer]
(
[CollectionGuid] ASC,
[Percent] ASC,
[_exec_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING =
OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
end
-- PART II: Get data into the trending table if no data was captured in the last 23 hours
if (select MAX(_exec_time) from TREND_WindowsCompliance_ByComputer where collectionguid = @collectionguid) < dateadd(hour, -23, getdate()) or ((select COUNT(*) from TREND_WindowsCompliance_ByComputer where collectionguid = @collectionguid) = 0) or (@force = 1)
begin
-- Get the compliance by update to a 'temp' table
truncate table PM_TRENDS2_TEMP
insert into PM_TRENDS2_TEMP
exec spPMWindows_ComplianceByComputer
@OperatingSystem = '%',
@DistributionStatus = 'active',
@FilterCollection = @collectionguid,
@StartDate = '1990-08-21T00:00:00',
@EndDate = '2020-12-31',
@pCulture = 'en-gb',
@TrusteeScope = '{2e1f478a-4986-4223-9d1e-b5920a63ab41}',
@VendorGuid = '00000000-0000-0000-0000-000000000000',
@CategoryGuid = '00000000-0000-0000-0000-000000000000'
declare @id as int
set @id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByComputer where collectionguid = @collectionguid)
declare @total as float
set @total = (select COUNT(*) from PM_TRENDS2_TEMP)
insert into TREND_WindowsCompliance_ByComputer
select (ISNULL(@id + 1, 1)), @CollectionGuid as CollectionGuid, GETDATE() as '_Exec_time', CAST(compliance as decimal) as 'Percentile', COUNT(*) as 'Computer #', cast((CAST(count(*) as float) / @total) * 100 as money) as '% of Total'
from PM_TRENDS2_TEMP
group by CAST(compliance as decimal)
order by CAST(compliance as decimal)
end
select *
from TREND_WindowsCompliance_ByComputer
where _exec_id = (select max(_exec_id) from TREND_WindowsCompliance_ByComputer where collectionguid = @collectionguid)
and collectionguid = @collectionguid
";
#endregion
#region string sql_spTrendPatchComplianceByUpdate = @"...
public static string sql_drop_spTrendPatchComplianceByUpdate = @"
if exists(select 1 from sys.objects where name = 'spTrendPatchComplianceByUpdate' and type = 'P')
begin
drop procedure spTrendPatchComplianceByUpdate
end
";
public static string sql_spTrendPatchComplianceByUpdate = @"
create procedure spTrendPatchComplianceByUpdate
@collectionguid as uniqueidentifier = '01024956-1000-4cdb-b452-7db0cff541b6',
@force as int = 0
as
-- #########################################################################################################
-- PART I: Make sure underlying infrastructure exists and is ready to use
if (exists(select 1 from sys.objects where name = 'PM_TRENDS_TEMP' and type = 'U'))
begin
truncate table PM_TRENDS_TEMP
end
else
begin
CREATE TABLE [dbo].[PM_TRENDS_TEMP](
[_SWUGuid] [uniqueidentifier] NOT NULL,
[Bulletin] [varchar](250) NOT NULL,
[Update] [varchar](250) NOT NULL,
[Severity] [varchar](250) NOT NULL,
[Custom Severity] [nvarchar](100) NULL,
[Release Date] [datetime] NOT NULL,
[Compliance] [numeric](6, 2) NULL,
[Applicable (Count)] [int] NULL,
[Installed (Count)] [int] NULL,
[Not Installed (Count)] [int] NULL,
[_SWBGuid] [uniqueidentifier] NOT NULL,
[_ScopeCollection] [uniqueidentifier] NULL,
[_Collection] [uniqueidentifier] NULL,
[_StartDate] [datetime] NULL,
[_EndDate] [datetime] NULL,
[_DistributionStatus] [nvarchar](16) NULL,
[_OperatingSystem] [nvarchar](128) NULL,
[_VendorGuid] [uniqueidentifier] NULL,
[_CategoryGuid] [uniqueidentifier] NULL
) ON [PRIMARY]
end
if (not exists(select 1 from sys.objects where type = 'U' and name = 'TREND_WindowsCompliance_ByUpdate'))
begin
CREATE TABLE [dbo].[TREND_WindowsCompliance_ByUpdate](
[_Exec_id] [int] NOT NULL,
[CollectionGuid] [uniqueidentifier] NOT NULL,
[_Exec_time] [datetime] NOT NULL,
[Bulletin] [varchar](250) NOT NULL,
[UPDATE] [varchar](250) NOT NULL,
[Severity] [varchar](250) NOT NULL,
[Installed] [int] NULL,
[Applicable] [int] NULL,
[DistributionStatus] [nvarchar](16) NULL
) ON [PRIMARY]
CREATE UNIQUE CLUSTERED INDEX [IX_TREND_WindowsCompliance_ByUpdate] ON [dbo].[TREND_WindowsCompliance_ByUpdate]
(
[CollectionGuid] asc,
[Bulletin] ASC,
[Update] ASC,
[_exec_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING =
OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IX_TREND_WindowsCompliance_ByUpdate_OrderbyUpdate] ON [dbo].[TREND_WindowsCompliance_ByUpdate]
(
[UPDATE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
end
-- PART II: Get data into the trending table if no data was captured in the last 24 hours
if (select MAX(_exec_time) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = @CollectionGuid) < dateadd(hour, -23, getdate()) or ((select COUNT(*) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = @CollectionGuid) = 0) or (@force = 1)
begin
-- Get the compliance by update to a 'temp' table
insert into PM_TRENDS_TEMP
exec spPMWindows_ComplianceByUpdate
@OperatingSystem = '%',
@DistributionStatus = 'Active',
@FilterCollection = @collectionguid,
@StartDate = '1900-06-29T00:00:00',
@EndDate = '2020-06-29T00:00:00',
@pCulture = 'en-GB',
@ScopeCollectionGuid = '91c68fcb-1822-e793-b59c-2684e99a64cd',
@TrusteeScope = '{2e1f478a-4986-4223-9d1e-b5920a63ab41}',
@VendorGuid = '00000000-0000-0000-0000-000000000000',
@CategoryGuid = '00000000-0000-0000-0000-000000000000',
@DisplayMode = 'all'
declare @id as int
set @id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = @CollectionGuid)
insert into TREND_WindowsCompliance_ByUpdate
select (ISNULL(@id + 1, 1)), @collectionguid, GETDATE() as '_Exec_time', Bulletin, [UPDATE], Severity, [Installed (Count)] as 'Installed', [Applicable (Count)] as 'Applicable', _DistributionStatus as 'DistributionStatus'
from PM_TRENDS_TEMP
end
-- Return the latest results
select *, applicable - installed as 'Vulnerable', cast(cast(installed as float) / cast(applicable as float) * 100 as money) as 'Compliance %'
from TREND_WindowsCompliance_ByUpdate
where _exec_id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = @CollectionGuid)
and CollectionGuid = @CollectionGuid
-- and cast(cast(installed as float) / cast(applicable as float) * 100 as money) < %ComplianceThreshold%
-- and applicable > %ApplicableThreshold%
union
select max(_exec_id), @CollectionGuid, max(_exec_time), Bulletin, '-- ALL --' as [update], '' as severity, sum(installed) as 'Installed', sum(applicable) as 'Applicable', '' as DistributionStatus, sum(applicable) - sum(installed) as 'Vulnerable', cast(cast(sum(installed) as float) / cast(sum(applicable) as float) * 100 as money) as 'Compliance %'
from TREND_WindowsCompliance_ByUpdate
where _exec_id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = @CollectionGuid)
and CollectionGuid = @CollectionGuid
group by Bulletin
--having sum(applicable) >%ApplicableThreshold%
-- and cast(cast(sum(installed) as float) / cast(sum(applicable) as float) * 100 as money) < %ComplianceThreshold%
order by Bulletin,[update]
";
#endregion
#region SQL query strings
public static string sql_get_bulletin_data = @"
select Convert(varchar(255), max(_Exec_time), 127) as 'Date', SUM(installed) as 'Installed', SUM(Applicable) as 'Applicable'
from TREND_WindowsCompliance_ByUpdate
where Bulletin = '{0}' and CollectionGuid = '{1}'
group by _Exec_id order by date
";
public static string sql_get_update_data = @"
select Convert(varchar(255), _Exec_time, 127) as 'Date', installed as 'Installed', Applicable as 'Applicable'
from TREND_WindowsCompliance_ByUpdate
where [update] = '{0}' and bulletin = '{1}' and CollectionGuid = '{2}'
";
public static string sql_get_bulletins_in = @"
-- Get all tracked bulletins
select bulletin
from TREND_WindowsCompliance_ByUpdate
where bulletin in ({0})
and collectionguid = '{1}'
group by bulletin
having MAX(_exec_id) = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where collectionguid = '{1}')
order by MIN(_exec_time) desc, Bulletin desc
";
public static string sql_get_all_bulletins = @"
-- Get all tracked bulletins
select bulletin
from TREND_WindowsCompliance_ByUpdate
where collectionguid = '{0}'
group by bulletin
having MAX(_exec_id) = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where collectionguid = '{0}')
order by MIN(_exec_time) desc, Bulletin desc
";
public static string sql_get_all_bulletins_bysev = @"
-- Get all tracked bulletins
select bulletin, severity
from TREND_WindowsCompliance_ByUpdate
where collectionguid = '{0}' and severity = '{1}'
group by bulletin
having MAX(_exec_id) = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where collectionguid = '{0}')
order by MIN(_exec_time) desc, Bulletin desc
";
public static string sql_get_global_compliance_data = @"
select Convert(varchar, max(_Exec_time), 127) as 'Date', SUM(installed) as 'Installed', SUM(Applicable) as 'Applicable'
from TREND_WindowsCompliance_ByUpdate
where collectionguid = '{0}'
group by _Exec_id order by date
";
public static string sql_get_global_compliance_data_bysev = @"
select Convert(varchar, max(_Exec_time), 127) as 'Date', SUM(installed) as 'Installed', SUM(Applicable) as 'Applicable'
from TREND_WindowsCompliance_ByUpdate
where collectionguid = '{0}' and severity = '{1}'
group by _Exec_id order by date
";
public static string sql_get_topn_vulnerable = @"
select top 10 Bulletin --, SUM(Applicable) - SUM(installed)
from TREND_WindowsCompliance_ByUpdate
where _Exec_id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where collectionguid = '{0}')
and collectionguid = '{0}'
group by Bulletin
order by SUM(Applicable) - SUM(installed) desc
";
public static string sql_get_topn_vulnerable_upd = @"
select top 25 [Update]
from TREND_WindowsCompliance_ByUpdate
where [_Exec_id] = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where collectionguid = '{0}')
and collectionguid = '{0}'
group by [Update], [Bulletin]
order by SUM(Applicable) - SUM(installed) desc
";
public static string sql_get_bottomn_compliance = @"
select top 10 Bulletin --, CAST(SUM(installed) as float) / CAST(SUM(Applicable) as float) * 100
from TREND_WindowsCompliance_ByUpdate
where _Exec_id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where collectionguid = '{0}')
and collectionguid = '{0}'
group by Bulletin
having SUM(Applicable) - SUM(installed) > 100
order by CAST(SUM(installed) as float) / CAST(SUM(Applicable) as float) * 100
";
public static string sql_get_bottomn_compliance_upd = @"
select top 25 [Update] --, CAST(SUM(installed) as float) / CAST(SUM(Applicable) as float) * 100
from TREND_WindowsCompliance_ByUpdate
where _Exec_id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where collectionguid = '{0}')
and collectionguid = '{0}'
group by Bulletin, [Update]
having SUM(Applicable) - SUM(installed) > 100
order by CAST(SUM(installed) as float) / CAST(SUM(Applicable) as float) * 100
";
public static string sql_get_topn_movers_up = @"
-- Return the 10 bulletins for which more computers are secured
select top 10 t1.Bulletin, t1._Exec_id, (sum(t2.Applicable) - SUM(t2.installed)) - (sum(t1.Applicable) - SUM(t1.installed)) as 'Delta'
from TREND_WindowsCompliance_ByUpdate t1
join TREND_WindowsCompliance_ByUpdate t2
on t1._Exec_id -1 = t2._Exec_id and t1.Bulletin = t2.Bulletin and t1.[UPDATE] = t2.[update] and t1.collectionguid = t2.collectionguid
where t1._Exec_id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where collectionguid = '{0}')
and t1.collectionguid = '{0}'
group by t1.Bulletin, t1._Exec_id
having (sum(t2.Applicable) - SUM(t2.installed)) - (sum(t1.Applicable) - SUM(t1.installed)) > 0
order by (sum(t2.Applicable) - SUM(t2.installed)) - (sum(t1.Applicable) - SUM(t1.installed)) desc
";
public static string sql_get_topn_movers_down = @"
-- Return the 10 bulletins for which more computers are vulnerable
select top 10 t1.Bulletin, t1._Exec_id, (sum(t2.Applicable) - SUM(t2.installed)) - (sum(t1.Applicable) - SUM(t1.installed)) as 'Delta'
from TREND_WindowsCompliance_ByUpdate t1
join TREND_WindowsCompliance_ByUpdate t2
on t1._Exec_id -1 = t2._Exec_id
and t1.Bulletin = t2.Bulletin
and t1.[UPDATE] = t2.[update]
and t1.collectionguid = t2.collectionguid
where t1._Exec_id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where collectionguid = '{0}')
and t1.collectionguid = '{0}'
group by t1.Bulletin, t1._Exec_id
having (sum(t2.Applicable) - SUM(t2.installed)) - (sum(t1.Applicable) - SUM(t1.installed)) < 0
order by (sum(t2.Applicable) - SUM(t2.installed)) - (sum(t1.Applicable) - SUM(t1.installed))
";
public static string sql_get_updates_bybulletin = @"
select distinct([UPDATE])
from TREND_WindowsCompliance_ByUpdate
where bulletin = '{0}'
and collectionguid = '{1}'
group by [update]
having MAX(_exec_id) = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = '{1}')
";
public static string sql_get_updates_bybulletin_bysev = @"
select distinct([UPDATE])
from TREND_WindowsCompliance_ByUpdate
where bulletin = '{0}'
and collectionguid = '{1}'
and severity = '{2}'
group by [update]
having MAX(_exec_id) = (select MAX(_exec_id) from TREND_WindowsCompliance_ByUpdate where CollectionGuid = '{1}')
";
public static string sql_get_compliance_bypccount = @"
declare @id as int
set @id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByComputer where CollectionGuid = '{0}')
if (@id > 1)
begin
select t1.[Percent], t3.[min], t2.[Computer #], t1.[Computer #], t3.[max], t1.[% of Total]
-- , t1.[% of Total], t2.[% of Total]
from TREND_WindowsCompliance_ByComputer t1
join TREND_WindowsCompliance_ByComputer t2
on t1.[Percent] = t2.[Percent]
join (
select[Percent], MIN(t3.[Computer #]) as min, MAX(t3.[computer #]) as max
from TREND_WindowsCompliance_ByComputer t3
where CollectionGuid = '{0}'
group by [Percent]
) t3
on t1.[Percent] = t3.[percent]
where t1._Exec_id = @id
and t2._Exec_id = @id - 1
and t1.CollectionGuid = '{0}'
-- and t1.[Percent] > 74
end
";
public static string sql_get_compliance_bypcpercent = @"declare @id as int
set @id = (select MAX(_exec_id) from TREND_WindowsCompliance_ByComputer where CollectionGuid = '{0}')
if (@id > 1)
begin
select t1.[Percent], t3.[min], t2.[% of Total], t1.[% of Total], t3.[max], t1.[% of Total]
-- , t1.[% of Total], t2.[% of Total]
from TREND_WindowsCompliance_ByComputer t1
join TREND_WindowsCompliance_ByComputer t2
on t1.[Percent] = t2.[Percent]
join (
select[Percent], MIN(t3.[% of Total]) as min, MAX(t3.[% of Total]) as max
from TREND_WindowsCompliance_ByComputer t3
where CollectionGuid = '{0}'
group by [Percent]
) t3
on t1.[Percent] = t3.[percent]
where t1._Exec_id = @id
and t2._Exec_id = @id - 1
and t1.CollectionGuid = '{0}'
-- and t1.[Percent] > 74
end
";
public static string sql_get_inactive_computer_trend = @"
select Convert(varchar, timestamp, 127), [Inactive computers (7 days)], [Inactive computers (17 days)], [New inactive computers], [New Active Computers]
from TREND_InactiveComputerCounts
where CollectionGuid = '{0}'
and [Managed machines] > 0 order by _exec_id
order by _exec_id
";
public static string sql_get_inactive_computer_percent = @"
select Convert(varchar, timestamp, 127), cast([Inactive computers (7 days)] as money) / cast([Managed machines] as money) * 100 as '7-days inactive (% of managed)', cast([Inactive computers (17 days)] as money) / cast([Managed machines] as money) * 100 as '17-days inactive (% of managed)', CAST([New inactive computers] as money) / CAST([Managed machines] AS money) * 100 as '++ (% of managed)', CAST([New active computers] as money) / CAST([Managed machines] as money) * 100 as '-- (% of managed)'
from TREND_InactiveComputerCounts
where CollectionGuid = '{0}'
and [Managed machines] > 0 order by _exec_id
order by _exec_id
";
public static string sql_exec_spTrendInactiveComputers = @"
if exists (select 1 from sys.objects where type = 'p' and name = 'spTrendInactiveComputers')
exec spTrendInactiveComputers @CollectionGuid = '{0}'
";
public static string sql_exec_spTrendPatchComplianceByComputer = @"
if exists (select 1 from sys.objects where type = 'p' and name = 'spTrendPatchComplianceByComputer')
exec spTrendPatchComplianceByComputer @CollectionGuid = '{0}'
";
public static string sql_exec_spTrendPatchComplianceByUpdate = @"
if exists (select 1 from sys.objects where type = 'p' and name = 'spTrendPatchComplianceByUpdate')
exec spTrendPatchComplianceByUpdate @CollectionGuid = '{0}'
";
#endregion
}
}