复杂? MySQL查询

时间:2011-03-26 17:08:10

标签: mysql

我有一个包含列,id,date,estValue& amp; gradeid。每个成绩id有大约12个记录,大约有10个不同的成绩,总共大约120个记录[给予或采取]我需要从数据库创建一个选择,给我一个看起来像这样的结果集:


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行]但是所有的列值都是相同的[它们应该都是不同的]即ie每行中的estValue和列应该是唯一值...

我不确定最好的办法是什么。

-Thanks -Sean

3 个答案:

答案 0 :(得分: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也可能是合适的汇总函数。

答案 1 :(得分: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;

答案 2 :(得分:0)

像我说的那样,忘掉小组的一部分。它没有效果。 我想我看到了你的问题。

将gradeid ='1'替换为gradeid = eh.gradeid