# Overall Risk Score Chart - Peaks and Valleys Context

**URL:** <https://discuss.rapid7.com/t/overall-risk-score-chart-peaks-and-valleys-context/40037>\
**Category:** SQL Queries\
**Created:** [July 19, 2024, 6:19pm UTC](https://discuss.rapid7.com/t/overall-risk-score-chart-peaks-and-valleys-context/40037 "2024-07-19T18:19:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwebb](https://discuss.rapid7.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jwebb](https://discuss.rapid7.com/u/jwebb)\
**Post date:** [July 19, 2024, 6:19pm UTC](https://discuss.rapid7.com/t/overall-risk-score-chart-peaks-and-valleys-context/40037/1 "2024-07-19T18:19:00Z")

</div>

Hi All,

Once a month, I provide a slide with the Overall Risk Trend chart, highlighting peaks and valleys corresponding to newly published/discovered vulnerabilities and those that have been remediated. Currently, I generate an SQL query report listing all new and remediated vulnerabilities from the past month to analyze the causes of these fluctuations. I then manually add comments to the chart to explain the peaks and valleys. This process is time-consuming.

To streamline this, I propose using the New and Remediated Vulnerabilities SQL report to show the percentages of the types of vulnerabilities contributing to the peaks and valleys. For example, 20% of new vulnerabilities might be related to Microsoft, 10% to Google Chrome, and for remediated vulnerabilities, 45% might be due to Microsoft patching, etc. This approach will save time and provide clearer insights into the data trends.

Here is the query I’m working with now that provides way more information than is needed -  
WITH assets\_vulns AS (  
SELECT  
fasv.asset\_id,  
fasv.vulnerability\_id,  
baselineComparison (fasv.scan\_id, current\_scan) AS baseline,  
s.baseline\_scan,  
s.current\_scan  
FROM  
fact\_asset\_scan\_vulnerability\_instance fasv  
JOIN (  
SELECT  
asset\_id,  
previousScan (asset\_id) AS baseline\_scan,  
lastScan (asset\_id) AS current\_scan  
FROM  
dim\_asset  
) s ON s.asset\_id = fasv.asset\_id  
AND (  
fasv.scan\_id = s.baseline\_scan  
OR fasv.scan\_id = s.current\_scan  
)  
GROUP BY  
fasv.asset\_id,  
fasv.vulnerability\_id,  
s.baseline\_scan,  
s.current\_scan  
HAVING  
(  
baselineComparison (fasv.scan\_id, current\_scan) = ‘Same’  
)  
OR (  
baselineComparison (fasv.scan\_id, current\_scan) = ‘New’  
)  
OR (  
baselineComparison (fasv.scan\_id, current\_scan) = ‘Old’  
)  
),  
baseline\_scan\_date as (  
SELECT  
av.asset\_id,  
finished  
FROM  
assets\_vulns av  
LEFT JOIN dim\_scan ds ON ds.scan\_id = av.baseline\_scan  
GROUP BY  
av.asset\_id,  
finished  
),  
current\_scan\_date AS (  
SELECT  
av.asset\_id,  
finished  
FROM  
assets\_vulns av  
LEFT JOIN dim\_scan ds ON ds.scan\_id = av.current\_scan  
GROUP BY  
av.asset\_id,  
finished  
),  
new\_vulns AS (  
SELECT  
av.asset\_id,  
av.vulnerability\_id,  
COUNT (av.vulnerability\_id) AS new\_vulns  
FROM  
assets\_vulns AS av  
WHERE  
av.baseline = ‘New’  
GROUP BY  
av.asset\_id,  
av.vulnerability\_id  
),  
remediated\_vulns AS (  
SELECT  
av.asset\_id,  
av.vulnerability\_id,  
COUNT (av.vulnerability\_id) AS remediated\_vulns  
FROM  
assets\_vulns AS av  
WHERE  
av.baseline = ‘Old’  
GROUP BY  
av.asset\_id,  
av.vulnerability\_id  
),  
vuln\_exploit\_count AS (  
SELECT  
CASE  
WHEN ec1.vulnerability\_id IS NOT NULL THEN ec1.vulnerability\_id  
ELSE ec2.vulnerability\_id  
END AS vulnerability\_id,  
metasploit,  
exploitdb  
FROM  
(  
SELECT  
av.vulnerability\_id,  
COUNT(dve.source) AS metasploit  
FROM  
assets\_vulns av  
JOIN dim\_vulnerability\_exploit dve ON av.vulnerability\_id = dve.vulnerability\_id  
WHERE  
dve.source = ‘Metasploit’  
GROUP BY  
av.vulnerability\_id  
) ec1 FULL  
JOIN (  
SELECT  
av.vulnerability\_id,  
COUNT(dve.source) AS exploitdb  
FROM  
assets\_vulns av  
JOIN dim\_vulnerability\_exploit dve ON av.vulnerability\_id = dve.vulnerability\_id  
WHERE  
dve.source = ‘Exploit DB’  
GROUP BY  
av.vulnerability\_id  
) ec2 ON ec2.vulnerability\_id = ec1.vulnerability\_id  
)  
SELECT  
‘Remediated’ AS status,  
da1.ip\_address AS ip\_address,  
da1.host\_name AS hostname,  
bsd.finished AS baseline\_scan\_datetime,  
csd.finished AS current\_scan\_datetime,  
dv1.vulnerability\_id,  
dv1.title,  
CAST(dv1.cvss\_score AS decimal(10, 2)) AS cvss\_score,  
CAST(dv1.riskscore AS decimal(10, 0)) AS riskscore,  
dv1.malware\_kits,  
CASE  
WHEN vec.metasploit IS NULL THEN 0  
ELSE vec.metasploit  
END AS metasploit,  
CASE  
WHEN vec.exploitdb IS NULL THEN 0  
ELSE vec.exploitdb  
END AS exploitdb  
FROM  
remediated\_vulns rv  
JOIN dim\_asset da1 ON da1.asset\_id = rv.asset\_id  
LEFT JOIN baseline\_scan\_date bsd ON bsd.asset\_id = da1.asset\_id  
LEFT JOIN current\_scan\_date csd ON csd.asset\_id = da1.asset\_id  
JOIN dim\_vulnerability dv1 ON dv1.vulnerability\_id = rv.vulnerability\_id  
LEFT JOIN vuln\_exploit\_count vec ON vec.vulnerability\_id = rv.vulnerability\_id  
UNION  
ALL  
SELECT  
‘New’ AS status,  
da2.ip\_address AS ip\_address,  
da2.host\_name AS hostname,  
bsd.finished AS baseline\_scan\_datetime,  
csd.finished AS current\_scan\_datetime,  
dv2.vulnerability\_id,  
dv2.title,  
CAST(dv2.cvss\_score AS decimal(10, 2)) AS cvss\_score,  
CAST(dv2.riskscore AS decimal(10, 0)) AS riskscore,  
dv2.malware\_kits,  
CASE  
WHEN vec.metasploit IS NULL THEN 0  
ELSE vec.metasploit  
END AS metasploit,  
CASE  
WHEN vec.exploitdb IS NULL THEN 0  
ELSE vec.exploitdb  
END AS exploitdb  
FROM  
new\_vulns nv  
JOIN dim\_asset AS da2 ON da2.asset\_id = nv.asset\_id  
LEFT JOIN baseline\_scan\_date bsd ON bsd.asset\_id = da2.asset\_id  
LEFT JOIN current\_scan\_date csd ON csd.asset\_id = da2.asset\_id  
JOIN dim\_vulnerability dv2 ON dv2.vulnerability\_id = nv.vulnerability\_id  
LEFT JOIN vuln\_exploit\_count vec ON vec.vulnerability\_id = nv.vulnerability\_id  
ORDER BY  
status DESC,  
ip\_address,  
hostname,  
title
