2017-08-16 87 views
0
Select category, 
     concat(count(category) * 100/ (select count(*) from table3),'%') as category_percentage_males 
from table3 
where gender in ('m') 
group by category 
order by category_percentage_males desc 

由於某種原因,一旦我連續執行命令,就不會返回desc。如果沒有concat命令,則工作正常。任何理由爲什麼發生這種情況?爲什麼我不能通過使用%符號執行連續命令後執行命令

+0

能否請你告訴樣本數據來說明你看到什麼?我認爲這是因爲你期待數字排序,但他們是字符串,所以他們遵循字符串排序。 – Siyual

+0

明白!然而,當我根本不包括concat並且如下所示:count(category)* 100/select table(*)from table3)作爲category_percentage我可以通過desc命令來獲得前三個類別的百分比@Siyual – cpat21

+0

對 - 因爲這是一個數值。 'CONCAT()'返回一個字符串值。字符串和數字排序不同。 – Siyual

回答

1

這是您的查詢:

Select category, concat(count(category) * 100/ (select count(*) from table3),'%') as category_percentage_males 
from table3 where gender in ('m') 
group by category 
order by category_percentage_males desc; 

我強烈建議你要求它爲:

Select category, 
     concat(avg(case when gender = 'm' then 100.0 else 0 end), '%') as category_percentage_males 
group by category 
order by avg(case when gender = 'm' then 100.0 else 0 end) desc;