如果在mysql中大于1,则使列中的值等于1

时间:2016-12-06 06:48:17

标签: mysql

我的sql中有一个表。

我已将count列中的值除以列的第99个四分位数。我得到了这个结果。

id  count
1    0.3
2    0.5
3    0.7
4    0.9
5    1.3
6    0.1
7    3.2

计算我使用的第99个四分位数的代码是:

 SELECT
 CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(
    GROUP_CONCAT(count ORDER BY count SEPARATOR ','),
   ',', 100/100 * COUNT(*) + 1), ',', -1) AS DECIMAL) AS `95th Per`
 FROM table_name;

然后我做了:

select id, (count/(SELECT       
       CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(
       GROUP_CONCAT(count ORDER BY count SEPARATOR ',')
        ,',', 100/100 * COUNT(*) + 1), ',', -1)
        AS DECIMAL) AS `95th Per` FROM table_name) as count1 
from table_name.

我现在面临的挑战是

我想要make count of the column's = 1 whenever the count is greater than 1, 就我而言id=5 and 7

有没有办法调整我的查询并获得所需的输出。 提前谢谢。

The code for calcluation of the quartile was found on this site

1 个答案:

答案 0 :(得分:1)

尝试将查询包装在另一个类似于

的选择中
Select 
   Id, 
   Case when count > 1 then 1 else count end as count
From (
    // place original query here
) t

然后,您可以更轻松地操作外部查询以查看计数。我的案例语法可能有点偏,但你明白了。