我有一个维护脚本,它将一堆数据从一个数据库转储到另一个数据库。
我正在尝试获取数据,因为
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 的有效替代品
所以,我相信我有两个问题。
- 我认为我没有在嵌套选择语句中使用rank2
- 我不知道怎么说“最接近当前的
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.
- I don't think I have rank2 to be used in the nested select statement
- 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.
发布评论
评论(1)
我不是 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?