T-SQL语法错误:列'dbo.Policy.CompanyId'在选择列表中无效
问题描述:
有人可以帮我弄清楚下面的T-SQL语法有什么问题吗?T-SQL语法错误:列'dbo.Policy.CompanyId'在选择列表中无效
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.
有什么建议?
UPDATE:
好,谢谢您输入的家伙,我想通了......这里的解决方案:
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
错误消息的哪部分您不明白? –
@MartinSmith:我理解错误信息,但我不明白其背后的原因。我想这是一些基本的SQL概念,我不知道(我是T-SQL的新手,过去我一直使用LINQ to Entities)。 – Kassem
它确实告诉你你需要知道的一切!我考虑回答,但意识到基本上我所做的一切都会改写错误信息。 –