为什么 Select 与返回重复项的函数不同?

发布于 2024-08-10 21:27:33 字数 673 浏览 3 评论 0原文

我对同一件事尝试了两种不同的变体。第一个版本从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛起拖把扫天下 2024-08-17 21:27:33

我猜想,您将排名转换为整数,它实际上是浮点数?如果是这样,那么我的下一个猜测是,这归结为典型的浮点比较问题。

关于临时表,您要做的就是选择所有重复数据,将其逐字放入临时表中,然后将其转储出来,重复项等等。这可能会取得更大的成功

create table #temp ([rank] int)
insert into #temp
    select [rank] from freetexttable(dbo.vw_PPN, allKeywords, N'foo', 100000 )
    where [key] = 3781054
select distinct [rank] from #temp
drop table #temp

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

create table #temp ([rank] int)
insert into #temp
    select [rank] from freetexttable(dbo.vw_PPN, allKeywords, N'foo', 100000 )
    where [key] = 3781054
select distinct [rank] from #temp
drop table #temp
抚笙 2024-08-17 21:27:33

如果独立运行,第一个查询中的子查询会返回什么?运行 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).

多像笑话 2024-08-17 21:27:33

我永远不能忽视一个好的谜团,但在这种情况下我无法重现这种行为。要么与该自由文本表有关,要么您发布了不存在该问题的不同版本的查询。如果我们可以查看以下结果,那就太好了:

Select * from freetexttable(dbo.vw_PPN, allKeywords, N'foo', 100000 )

用子查询替换您的自由文本表以允许测试,在两种情况下我都得到一行,即使子查询有您所说的三行:

Select Distinct [rank] from 
    (select 137 as [rank], 3781054 as [key] union all 
     select 137, 3781054 union all 
     select 137, 3781054) x
Where [key] = 3781054

排名


137

(1行)受影响)

create table #temp ([rank] int)
insert into #temp
    select distinct [rank] from 
        (select 137 as [rank], 3781054 as [key] union all 
         select 137, 3781054 union all 
         select 137, 3781054) x
    where [key] = 3781054
select * from #temp
drop table #temp

(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 :

Select * from freetexttable(dbo.vw_PPN, allKeywords, N'foo', 100000 )

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 :

Select Distinct [rank] from 
    (select 137 as [rank], 3781054 as [key] union all 
     select 137, 3781054 union all 
     select 137, 3781054) x
Where [key] = 3781054

rank


137

(1 row(s) affected)

create table #temp ([rank] int)
insert into #temp
    select distinct [rank] from 
        (select 137 as [rank], 3781054 as [key] union all 
         select 137, 3781054 union all 
         select 137, 3781054) x
    where [key] = 3781054
select * from #temp
drop table #temp

(1 row(s) affected)

rank


137

(1 row(s) affected)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文