【发布时间】:2019-01-16 22:14:05
【问题描述】:
以下是我获取 documentid 计数的查询
select count(did),did
from (
select count(FileDataId) as fid,
documentid as did,
FileDataId
from DocumentHistory
where documentid in (
select documentid
from Document
where DocumentTypeId=11 and
IsVerified=0 and
lastmodified < '5/jan/2019'
)
group by DocumentId,
filedataid
having count(FileDataId)<2
)
我收到错误
')' 附近的语法不正确。
如果我运行内部查询,它会给我带来结果
我想知道结果中 do 重复了多少次
【问题讨论】:
-
空白让您的查询更容易阅读。而且,一旦你使用它,你会注意到你没有在你的
FROM中给你的子查询一个别名;这是强制性的。试试SELECT * FROM (SELECT 1 as i);,你会得到同样的错误。 -
子查询加个别名就行了,group by也需要
-
现在已将其格式化为清晰易读的格式,问题所在就很清楚了。您将子查询作为查询的来源,但它没有别名。但老实说,这里似乎有些不对劲。因为您已在子查询中将它们分组,所以每行的确实计数为 1。
-
另外,对于 ANSI 标准
lastmodified < '5/jan/2019',这是不好的做法
标签: sql-server