为什么 Select 与返回重复项的函数不同?
我对同一件事尝试了两种不同的变体。第一个版本从 freetexttable
中进行选择,另一个版本插入到临时表中并从中进行选择。我在第一个版本上尝试了多种变体(在范围的两个级别上选择几种组合,包括分组依据、不同的以及将 [rank] 转换为整数。无论如何,第一个查询始终返回 3 行,每行都有值 137
而第二个查询始终返回值为 137
的 1 行,
这是怎么回事? 还是使用group by
?
注意:我想知道原因,而不是如何修复它。我已经有了可接受的解决方法。
select * from
(
select distinct [rank] from freetexttable(dbo.vw_PPN, allKeywords, N'foo', 100000 )
where [key] = 3781054
) as CT
create table #temp ([rank] int)
insert into #temp
select distinct [rank] from freetexttable(dbo.vw_PPN, allKeywords, N'foo', 100000 )
where [key] = 3781054
select * from #temp
drop table #temp
I tried two different variations on the same thing. The first version selects from freetexttable
, the other insets into a temp table and selects from that. I've tried numerous variations on the first version (select several combinations, at both levels of scope, of group by, distinct, and casting [rank] to an integer. Regardless, the first query consistently returns 3 rows each having value 137
whereas the second query consistently returns 1 row having value of 137
.
What is going on here? Why does freetext return duplicates and why aren't they eliminated with select distinct
or with group by
?
Note: I want to know why, not how to fix it. I already have acceptable workarounds.
select * from
(
select distinct [rank] from freetexttable(dbo.vw_PPN, allKeywords, N'foo', 100000 )
where [key] = 3781054
) as CT
create table #temp ([rank] int)
insert into #temp
select distinct [rank] from freetexttable(dbo.vw_PPN, allKeywords, N'foo', 100000 )
where [key] = 3781054
select * from #temp
drop table #temp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜想,您将排名转换为整数,它实际上是浮点数?如果是这样,那么我的下一个猜测是,这归结为典型的浮点比较问题。
关于临时表,您要做的就是选择所有重复数据,将其逐字放入临时表中,然后将其转储出来,重复项等等。这可能会取得更大的成功
I'd guess by the fact that you're casting rank to an integer that it is actually a float? If so, then my next guess would be that it comes down to typical floating point comparison issues.
Regarding your temp table, what you're doing is selecting all the duplicate data, putting it into the temp table verbatim, then just dumping it out, duplicates and all. This might have more success
如果独立运行,第一个查询中的子查询会返回什么?运行 SELECT * FROM (SELECT DISTINCT ...) 有点奇怪(尽管它当然应该返回与内部查询完全相同的东西)。
What does the subquery in the first query return, if you run it standalone? Running
SELECT * FROM (SELECT DISTINCT ...)
is a bit strange (although it should of course return exactly the same thing as the inner query).我永远不能忽视一个好的谜团,但在这种情况下我无法重现这种行为。要么与该自由文本表有关,要么您发布了不存在该问题的不同版本的查询。如果我们可以查看以下结果,那就太好了:
用子查询替换您的自由文本表以允许测试,在两种情况下我都得到一行,即使子查询有您所说的三行:
排名
137
(1行)受影响)
(1 行受影响)
排名
137
(1 行受影响)
I can never ignore a good mystery, but in this case I just couldn't reproduce this behavior. Either its something to do with that freetexttable or maybe you posted a different version of the query which doesn't exhibit the issue. It would have been nice if we could look at the result of :
Replacing your freetexttable with a subquery to allow testing, I get one row in both cases, even though the subquery has three like you said :
rank
137
(1 row(s) affected)
(1 row(s) affected)
rank
137
(1 row(s) affected)