【发布时间】:2015-05-06 20:52:36
【问题描述】:
对不起,如果我没有很好地解释,而且标题也不是很清楚。 我正在使用 SQL Server 2005。 我有一个使用 Pivot 的查询,它工作正常,但我知道我必须添加一个新查询,以便从季度获取结果。
这是我从 Month 获取结果的查询
WITH PivotData AS
(
SELECT idWatimetro, mes,ano, valor
FROM E_Registros_Watimetros where ano = 2012
)
SELECT *
FROM PivotData
PIVOT(SUM(valor) FOR idWatimetro IN ([1],[2],[3],[4],[5] AS P order by mes;
所以,只知道我想获得四个注册
1 With Month 1+2+3
2 Wint Month 4+5+6
3 With Month 7+8+9
4 Wint Month 10+11+12
我一直在尝试使用 UNION ALL,但没有按预期工作,任何帮助将不胜感激,对于我糟糕的英语和解释感到抱歉。
这是我没有结果的查询
SELECT *
FROM (
SELECT idWatimetro, ano,mes, valor
FROM E_Registros_Watimetros
WHERE (ano = 2012 and mes = 1 ) or (ano = 2012 and mes = 2)or (ano = 2012 and mes = 3)
UNION ALL
SELECT idWatimetro, ano,mes, valor
FROM E_Registros_Watimetros
WHERE (ano = 2012 and mes = 4 ) or (ano = 2012 and mes = 5)or (ano = 2012 and mes = 6)
) AS SourceTable
PIVOT(SUM(valor) FOR idWatimetro IN ([1],[2],[3],[4],[5]))AS P
多谢了。
【问题讨论】:
标签: sql-server sql-server-2005 pivot-table