gpt4 book ai didi

sql - 在同一查询中使用 count(*) 结果字段

转载 作者:行者123 更新时间:2023-11-29 13:43:36 25 4
gpt4 key购买 nike

您好,我正在使用以下查询来计算每项 Assets 的平均漏洞数量趋势。我需要使用 count(*) 操作的结果进行进一步计算,如您在我的查询中所见。不幸的是,我有错误,列 scanned_asset 不存在。我该如何解决?

SELECT to_char(fad.day, 'DD-MM-YYYY') AS day, count(*) AS scanned_assets,
scanned_assets/(SELECT DISTINCT vulnerabilities FROM fact_all_date fadt WHERE
fadt.day=fad.day) AS avg,
FROM fact_asset_date fad
INNER JOIN dim_asset da USING(asset_id)
WHERE (da.credential_status='All credentials successful' OR
da.credential_status='Credentials partially successful')
GROUP BY fad.day
ORDER BY fad.day ASC

最佳答案

这样的东西应该有用吗?

SELECT 
x.day,
x.scanned_assets,
x.scanned_assets / (SELECT DISTINCT vulnerabilities FROM fact_all_date fadt WHERE fadt.day = x.day) AS avg
FROM
(SELECT
TO_CHAR(fad.day, 'DD-MM-YYYY') AS day,
COUNT(*) AS scanned_assets
FROM
fact_asset_date fad
INNER JOIN dim_asset da USING (asset_id)
WHERE
da.credential_status = 'All credentials successful'
OR da.credential_status = 'Credentials partially successful'
GROUP BY
fad.day) x
ORDER BY
x.day;

编辑:删除方括号,因为 POSTGRESQL 不喜欢它们。

关于sql - 在同一查询中使用 count(*) 结果字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51873176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com