在 IFNULL 内获取最接近的结果

发布于 2024-08-02 23:50:50 字数 1214 浏览 9 评论 0 原文

我有一个维护脚本,它将一堆数据从一个数据库转储到另一个数据库。

我正在尝试获取数据,因为

  SELECT id, IFNULL(rank1,(SELECT rank2 
  FROM table  
  WHERE rank1 IS NOT NULL and 
        rank2<rank2 of current row 
  ORDER BY rank2 LIMIT 1)) FROM table

我尝试使用当前行的rank2 获取的是最接近的rank2,其中rank1 不为空。我假设rank1 是rank2 的有效替代品

所以,我相信我有两个问题。

  1. 我认为我没有在嵌套选择语句中使用rank2
  2. 我不知道怎么说“最接近当前的rank2”。

我的 Rank1 值范围为 0-20,000,Rank2 值范围为 0-150,000(不知道为什么这很重要)。排名之间不存在有效的相关性。

Rank1 始终是一个更可靠的数字,但通常为空,因此我试图用这种类型的替代品来捏造我的订单。

下面是一些示例数据,

id            rank1            rank2
1             120,000          14,000
2             120,000          18,420
3             126,000          15,500
4             85,000            NULL
5             75,000           16,000
6             70,000           15,700
7             68,000            NULL
8             42,000            NULL
9             26,000            NULL
10            21,500           8,000

我希望得到 2,5,4,6,7,3,1,8,9,10 的订单。或类似的东西。基本上,当我的rank2为空时,得到最接近的rank1的最接近的rank2。

我不认为这是“完美的”,但比仅仅按排名排序要好。

I've got a maintenance script which dumps a bunch of data from one db to another.

I'm trying to get the data as

  SELECT id, IFNULL(rank1,(SELECT rank2 
  FROM table  
  WHERE rank1 IS NOT NULL and 
        rank2<rank2 of current row 
  ORDER BY rank2 LIMIT 1)) FROM table

What I'm attempting to get with the rank2 of current row, is the nearest rank2 where rank1 is not null. I'm making the assumption that rank1 is an effective replacement for rank2

So, i believe I have two problems.

  1. I don't think I have rank2 to be used in the nested select statement
  2. I don't know how to say 'get closest rank2<rank2 to current.

The values I have for Rank1 range from 0-20,000, and rank2 ranges from 0-150,000 (not sure why that would matter). There isn't an effective correlation between the ranks.

Rank1 is always a more reliable figure, but is often null, so I'm trying to fudge the my order with this type of a replacement.

Here's a bit of sample data to use as an example

id            rank1            rank2
1             120,000          14,000
2             120,000          18,420
3             126,000          15,500
4             85,000            NULL
5             75,000           16,000
6             70,000           15,700
7             68,000            NULL
8             42,000            NULL
9             26,000            NULL
10            21,500           8,000

I would hope to get back an order of 2,5,4,6,7,3,1,8,9,10. or something close to that. Basically, when I have a null for rank2 get the nearest rank2 for the nearest rank1.

I don't expect this to be 'perfect', but better than just sorting on rank1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

殊姿 2024-08-09 23:50:50

我不是 100% 确定你在问什么。 COALESCE(rank1,rank2,rankX) 返回第一个非空值对您有用吗?

I'm not 100% sure what you're asking. Would COALESCE(rank1, rank2, rankX) where it would return the first non-null value work for you?

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