2016-12-06 209 views
0

我在我的sql中有一個表。如果在mysql中的值大於1,那麼使列中的值等於1

我已將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. 

我面臨什麼challange現在

我想要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

嘗試包裹查詢在另一個選擇類似

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

然後你就可以操縱外部查詢更容易檢查計數。我的案例語法可能有點偏離,但你明白了。

+0

請您詳細說明一下嗎? – Shubham