0

下面是我的查询,但我的 SQL 似乎不正确。基本上我试图让第 7 列显示来自单独表 (C) 的计数,但我收到错误。有什么帮助吗?查询工作正常,没有 select 语句中的第二个计数,但我只是试图让第 7 列获得另一个表的计数。

select 
    distinct a.auditid,
    a.iav,
    c.VID as 'VMS ID',
    c.severity as 'VMS Severity',
    a.name,
    count (distinct A.dnsname) as 'cat 1s',
    count (distinct c.dnsname) as 'ccri'
from 
    [dbo].[Enterprise 20130703] a
    left join [dbo].[VMS_Retina] c on a.AuditID=c.auditid
    inner join [CCRI 2013] c on a.dnsname=c.DNSName
where
    A.sevcode = 'category i'
    AND A.DNSNAME NOT IN ('autodiscovery,'UNKNOWN')
group by A.auditid, a.iav, a.name, c.VID, c.severity
order by 6 desc
4

2 回答 2

1

您已经c为两个不同的表使用了别名,将一个更改为其他字符并且查询应该可以正常工作:

select 
    distinct a.auditid,
    a.iav,
    c1.VID as 'VMS ID',
    c1.severity as 'VMS Severity',
    a.name,
    count (distinct A.dnsname) as 'cat 1s',
    count (distinct c2.dnsname) as 'ccri'
from
    [dbo].[Enterprise 20130703] a
    left join [dbo].[VMS_Retina] c1 on a.AuditID = c1.auditid
    inner join [CCRI 2013] c2 on a.dnsname = c2.DNSName
where
    A.sevcode = 'category i'
    AND A.DNSNAME NOT IN ('autodiscovery','UNKNOWN')
group by A.auditid, a.iav, a.name, c1.VID, c1.severity
order by 6 desc
于 2013-07-08T12:44:04.327 回答
0

我不知道您使用的是什么 DBMS,但 Oracle Distinct 必须在第一行,所以可能是这样的:

SELECT 
A.auditid,
A.iav,
A.VMS ID,
A.VMS Severity,
A.name,
A.cat_1s,
A.ccri
FROM (
    SELECT 
        distinct a.auditid,
        count (distinct A.dnsname) as 'cat_1s',
        count (distinct c.dnsname) as 'ccri'
        a.iav,
        c.VID as 'VMS ID',
        c.severity as 'VMS Severity',
        a.name,
    FROM 
        [dbo].[Enterprise 20130703] a
        left join [dbo].[VMS_Retina] c on a.AuditID=c.auditid
        inner join [CCRI 2013] c on a.dnsname=c.DNSName
    where
        A.sevcode = 'category i'
        AND A.DNSNAME NOT IN ('autodiscovery','UNKNOWN')
    group by A.auditid, a.iav, a.name, c.VID, c.severity
    order by 6 desc
) A
于 2013-07-08T14:35:38.123 回答