2011-03-26 36 views
0

我有一個表,列id,日期,estValue & gradeid。每個成績ID有大約12條記錄,總共約120條記錄有大約10個不同的成績[給與接受]。我需要從數據庫中創建一個選擇,它給我一個看起來像這樣的結果集:複雜嗎? MySQL Query


date |gradeid1 |gradeid2 |gradeid3 3|etc... 
01/01/01|estValue1|estValue2||estValue3|etc.... 
01/01/02|estValue1|estValue2||estValue3|etc.... 

我有一個查詢,可以選擇一個記錄,但我需要他們全部按日期排序:


select eh.id, eh.date as wdate, 
    (select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '1') as '1', 
    (select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '2') as '2', 
    (select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '3') as '3', 
    (select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '4') as '4', 
    (select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '5') as '5', 
    (select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '6') as '6', 
    (select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '7') as '7', 
    (select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '8') as '8', 
    (select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '9') as '9', 
    (select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '10') as '10' 
from nas_estimatehistory eh 
group by wdate 
order by `wdate` asc 
limit 1; 

返回差不多就是我所需要的,但只有1行,如果我刪除了極限,然後我得到每行[12行]的一行,但所有的列值是相同的[他們應該都是不同的],即estValue在每行和列應該是一個唯一的值...

我不確定最好的方法是什麼。

-Thanks -Sean

+0

表格模式(創建語句)和樣例數據(表示爲SQL INSERT語句)在這類問題中非常有用。 – outis 2011-03-26 18:12:41

回答

1

Cross-tabulation的關鍵在於:使用一個aggregate functionIF功能。

SELECT eh.date AS wdate, 
    GROUP_CONCAT(IF(gradeid=1,estValue,NULL)) as `1`, 
    GROUP_CONCAT(IF(gradeid=2,estValue,NULL)) as `2`, 
    GROUP_CONCAT(IF(gradeid=3,estValue,NULL)) as `3`, 
    GROUP_CONCAT(IF(gradeid=4,estValue,NULL)) as `4`, 
    GROUP_CONCAT(IF(gradeid=5,estValue,NULL)) as `5`, 
    GROUP_CONCAT(IF(gradeid=6,estValue,NULL)) as `6`, 
    GROUP_CONCAT(IF(gradeid=7,estValue,NULL)) as `7`, 
    GROUP_CONCAT(IF(gradeid=8,estValue,NULL)) as `8`, 
    GROUP_CONCAT(IF(gradeid=9,estValue,NULL)) as `9`, 
    GROUP_CONCAT(IF(gradeid=10,estValue,NULL)) as `10` 
FROM nas_estimatehistory eh 
GROUP BY wdate 
ORDER BY `wdate` ASC; 

MAXMIN也可以是合適的聚集函數。

+0

這正是我所期待的,是在MySQL文檔中記載的交叉列表,或者它是更理論類型的方法[也可以在我學習時學習一些東西!] – 2011-03-27 15:45:52

+0

有趣的是,它以blob對象的形式返回值 - 我必須將它們轉換爲字符串才能讓Coldfusion正確地看到它們,在查詢本身中是否有這樣做? – 2011-03-27 17:06:55

+0

@Sean:僅當'estValue'是一個二進制字符串(使IF()'表達式的類型爲二進制字符串)時,'GROUP_CONCAT'應該返回'BLOB';將'estValue'改爲非二進制文本類型應該注意這一點。有關更多信息,請參見[documentation](http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_group-concat)。如上所述,另一種選擇是切換到「MAX」或「MIN」。 – outis 2011-03-28 00:38:07

0

就像我說的,忘記該集團的一部分。它沒有效果。 我想我看到你的問題。

替換gradeid = '1' 與gradeid = eh.gradeid

+0

hmm。我需要每行都是1個月,每個gradeid每月有1個值......我不確定你想告訴我什麼。 – 2011-03-26 17:27:49

1
select eh.id, wdate, 
    (select estValue from nas_estimatehistory where `date` like `pattern` and gradeid = '1') as '1', 
    (select estValue from nas_estimatehistory where `date` like `pattern` and gradeid = '2') as '2', 
    (select estValue from nas_estimatehistory where `date` like `pattern` and gradeid = '3') as '3', 
    (select estValue from nas_estimatehistory where `date` like `pattern` and gradeid = '4') as '4', 
    (select estValue from nas_estimatehistory where `date` like `pattern` and gradeid = '5') as '5', 
    (select estValue from nas_estimatehistory where `date` like `pattern` and gradeid = '6') as '6', 
    (select estValue from nas_estimatehistory where `date` like `pattern` and gradeid = '7') as '7', 
    (select estValue from nas_estimatehistory where `date` like `pattern` and gradeid = '8') as '8', 
    (select estValue from nas_estimatehistory where `date` like `pattern` and gradeid = '9') as '9', 
    (select estValue from nas_estimatehistory where `date` like `pattern` and gradeid = '10') as '10' 
from (
    select eh.*, date_format(eh.date, '%Y-%m') wdate, 
       concat(date_format(eh.date, '%Y-%m'),'-%') `pattern` 
    from nas_estimatehistory) eh 
group by wdate 
order by wdate asc; 
+0

這也適用於[需要小修正:來自nas_estimatehistory呃),對我來說比outis的答案更容易理解。 – 2011-03-27 15:49:55