我有一张价格表。这背后的想法是让用户建议他们认为合理的价格。例如,假设我有一个表名tbl_Prices
:
| id |price |
|1 |1200 |
|2 |50000 |
|3 |50000 |
|4 |45000 |
|5 |506980|
|6 |2000 |
从这张表中我们可以看到大多数行的范围是50000 to 506980
。
所以我希望能找到一个能够找到这个范围的查询。
我看了this answer。这是相当不错的,但由于某些原因,我无法增加抵消,我几乎无法理解正在发生的事情,它产生:
|from_price|to_price|count|
|499999 | 500003 |2 |
|499998 | 500002 |2 |
|499997 | 500001 |2 |
|499996 | 500000 |2 |
|500000 | 500004 |2 |
|499996 | 450000 |1 |
答案 0 :(得分:0)
我已找出解决方案,找出按以下定义的总范围:
SELECT
CASE WHEN accountid BETWEEN 0 AND 216 THEN '0 to 216'
WHEN accountid BETWEEN 217 and 319 THEN '217 to 319'
WHEN accountid BETWEEN 320 and 429 THEN '320 to 429'
WHEN accountid BETWEEN 430 and 539 THEN '430 to 539'
WHEN accountid BETWEEN 540 and 649 THEN '540 to 649'
WHEN accountid BETWEEN 650 and 759 THEN '650 to 759'
WHEN accountid >= 860 THEN '860 +' END AS accountgroup, count(accountid) AS total
FROM account
GROUP BY accountgroup;
输出如下:
希望它会有所帮助。感谢。