T-SQL语法错误:列“dbo.Policy.CompanyId”在选择列表中无效

时间:2011-09-30 08:46:42

标签: sql sql-server tsql

有人可以帮我弄清楚以下T-SQL语法有什么问题吗?

SELECT 
    p.CompanyId, COUNT(*) as PolicyCount, c.CompanyId, c.CompanyName
FROM 
    dbo.Policy p
    INNER JOIN dbo.Company c ON p.CompanyId = c.CompanyId
WHERE 
    p.PolicyIssuingDate BETWEEN @StartDate AND @EndDate
GROUP BY
    p.PolicyId

我一直收到以下语法错误:

Column 'dbo.Policy.CompanyId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

有什么建议吗?

更新

好的,感谢您的输入人员,我想出来......这是解决方案:

SELECT 
    p.CompanyId, COUNT(p.PolicyId) as PolicyCount, c.CompanyName
FROM 
    dbo.Policy p
    INNER JOIN dbo.Company c ON p.CompanyId = c.CompanyId
WHERE 
    p.PolicyIssuingDate BETWEEN @StartDate AND @EndDate
GROUP BY
    p.CompanyId, c.CompanyName

1 个答案:

答案 0 :(得分:1)

根据您的查询,您正在尝试计算具有相同policyID且发布日期介于@startdate和@enddate之间的记录 但问题是你选择不在group by子句中的companyid