查询以转置SQL查询数据集

时间:2014-11-13 18:48:38

标签: sql sql-server tsql pivot

我在表中有值,需要以不同的格式进行复制。有人请提供指示

+---------------------------------------+
| Column1    date                 Value |
+---------------------------------------+
| US         07-18-2014           1     |
| US         07-19-2014           2     |
| Canada     07-18-2014           3     |
| Canada     07-19-2014           2     |
| France     07-18-2014           4     |
| France     07-19-2014           3     |
| Japan      07-18-2014           5     |
| China      07-18-2014           7     |
| China      07-19-2014           8     |

这需要转换成类似的东西

+------------------------------------------------------------+
|              US      Canada      France  Japan   China     |
+------------------------------------------------------------+
| 08-18-2014  1         3           4        5      7        |
| 08-19-2014  2         2           3               8        |
+------------------------------------------------------------+

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

SELECT * FROM
(
    SELECT column1,date,value
    FROM  mytable
)t
PIVOT (SUM(value) FOR item
IN ([US],[FRANCE])) AS pvt