sql - sql server returns duplicates
问题描述
I have a query in which I am fetching results from the sql server based on a list of values. Now there are two ways I can do this with:
- execute query for one value at a time and fetch the top 1 result
- Use a where-in clause and send multiple values.
The problem with the first approach is that it takes too much time since there are over 17000 values in the list. and The problem with the second approach is that it returns duplicates since the column I'm using for comparison is not unique. But it reduces execution time significantly.
The query is something like this:
'''SELECT *
FROM dbo.my_table
where AccountID IN {}
ORDER BY dummy_column DESC'''.format(values)
How can I remove those duplicates using the second approach? Or is there a better way to do it?
解决方案
样本数据和期望的结果会有所帮助。但是你可以用row_number()
. 这是一个不使用子查询的简单方法:
SELECT TOP (1) WITH TIES t.*
FROM dbo.my_table t
WHERE AccountID IN {}
ORDER BY ROW_NUMBER() OVER (PARTITION BY AccountId ORDER BY dummy_column DESC);
注意:这不会以任何特定方式对结果进行排序。目前尚不清楚这实际上是一个要求(鉴于第一种方法没有任何此类排序)。
推荐阅读
- python - 找出银行中的一笔钱通过年存款通过某个数字所需的年数
- excel - 日期之间的Excel时差随着日期的变化而跳跃
- javascript - 在 JavaScript 中按 3 个不同的标准排序
- python - 如何打印变量的名称?
- flutter - 如何在容器中创建向内阴影(斜角效果),
- spring - 无法将spring cloud配置服务器添加到spring app
- gradle - Gradle compile vs Gradle Implementation 在使用 gradle 6.9 的 gradle build 中没有产生相同的结果
- node.js - expo build:web docker 找不到正确的入口点
- javascript - 避免重复自己
- swiftui - SwiftUI NavigationLink 自动弹回