我只是遇到了麻煩,無法對我需要的查詢進行分類。我有這樣在mysql查詢中對數據進行分組或數據透視,使行成爲列
colDist colGender attnedancePrcnt
Place-1 Male 2.5 %
Place-1 Female 1.5%
Place-2 Male 3.5%
Place-2 Female 2.1%
的情景現在我需要在mysql中查詢到有這樣
colDist colMale colFemale
Place-1 2.5% 1.5 %
Place-2 3.5% 2.1 %
數據這有點組由coldDist但我只是面臨的麻煩,使colMale並從第一個數據集colfemale到第二個。
高級謝謝。
這種方法.....
select colDist, if(colGender='Male',colGender,null) as colMale ,
if(colGender='Female',colGender,null) as colFemale
from tablename
給了我這樣的
colDist colMale colFemale attnedancePrcnt
Place-1 Male NULL 2.5
Place-1 NULL Female 1.5
Place-2 Male NULL 3.5
Place-2 NULL Female 2.1
的結果,但我又需要他們這樣的:
colDist colMale colFemale
Place-1 2.5% 1.5 %
Place-2 3.5% 2.1 %
那麼怎麼可以根據需要對數據進行分組或轉儲?
謝謝。
這被稱爲數據透視表,並有許多像這樣的答案在SO上:http://stackoverflow.com/questions/5846007/sql-query-to-pivot-a-column-using-case-when – liquorvicar 2012-03-31 07:51:32