Where am I going wrong? Why am I getting that message when I add the MAX function?
The query involved:
WITH
references_cve AS (
SELECT vulnerability_id, reference
FROM dim_vulnerability_reference
WHERE dim_vulnerability_reference.source = 'NVD'
)
SELECT DISTINCT ON (
da.ip_address,
favi.port,
dv.severity,dp.name, dv.title,
dv.description,
fix,
dv.nexpose_id,
cve.reference,
da.host_name,
dos.description,
da.last_assessed_for_vulnerabilities,
davbs.solution_id
)
da.ip_address AS IPAddress,
CASE WHEN favi.port = -1 THEN NULL ELSE favi.port END AS Port,
dv.severity AS Severity,
dp.name AS Protocol,
dv.title AS VulnerabilityName,
htmlToText(dv.description) AS Summary,
htmlToText(dv.description) AS Description,
htmlToText(fix) as Solution,
'https://www.rapid7.com/db/vulnerabilities/' || dv.nexpose_id as SeeAlso,
dv.nexpose_id AS NexposeID,
''||null as PluginOutput,
cve.reference as CVE,
''||null as APPorOS,
''||null as BU,
da.host_name as DEVICENAME,
date(da.last_assessed_for_vulnerabilities) as LastScanDate,
dos.description as DeviceOperatingSystem,
MAX(sr.solution_id),
davbs.vulnerability_id,
dv.vulnerability_id
FROM fact_asset_vulnerability_instance favi
LEFT JOIN dim_asset da USING (asset_id)
LEFT JOIN dim_vulnerability dv USING (vulnerability_id)
LEFT JOIN dim_site_asset dsa USING (asset_id)
LEFT JOIN dim_site ds USING (site_id)
LEFT JOIN dim_vulnerability_status dvs USING (status_id)
LEFT JOIN dim_protocol dp USING (protocol_id)
LEFT JOIN dim_service dsvc USING (service_id)
LEFT JOIN dim_operating_system dos USING (operating_system_id)
LEFT JOIN dim_asset_vulnerability_best_solution davbs ON da.asset_id = davbs.asset_id AND dv.vulnerability_id = davbs.vulnerability_id
LEFT JOIN dim_solution sr on davbs.solution_id = sr.solution_id
LEFT JOIN references_cve cve on favi.vulnerability_id = cve.vulnerability_id
ORDER BY da.ip_address
The resulting message:
Error: column "da.ip_address" must appear in the GROUP BY clause or be used in an aggregate function
Character:456