0

使用 Sql server 2008 所以我有一个查询表,最后看起来像这样

例子:

month    Year    data
1        2012     123
...      2012     123
12       2012     123
1        2013     123
...      2013     123
12       2013     123

有没有办法对它进行选择,这样它就会像这样出现

month    Year    data    month    Year    data
1        2012     123      1      2013     123
...      2012     123      ...    2013     123
12       2012     123      12     2013     123

基本上为它返回的每个新年添加一行新的列

4

1 回答 1

1

在客户端、C#PHP 或 Python 中执行此操作要容易得多。但它可以在 SQL 中完成:

select  month as Month2012
,       2012 as Year2012
,       max(case when year = 2012 then data end) as Data2012
,       month as Month2013
,       2013 as Year2013
,       max(case when year = 2013 then data end) as Data2013
,       month as Month2014
,       2014 as Year2014
,       max(case when year = 2014 then data end) as Data2014
,       ...
from    YourTable
group by
        month
于 2013-07-29T21:33:17.180 回答