我有一个产生结果的表格,如:
Code Month Value
733 May 520.00
733 May 2250.00
733 May 125.00
我有一个要求,我需要在上面显示如下记录:
Code May
733 520
733 2250
733 125
我可以通过 pivot 来考虑一种方式,所以我想:
Select
Code,
IsNull(January,0)[January(Actual)],
IsNull(February,0)[February(Actual)],
IsNull(March,0)[March(Actual)],
IsNull(April,0)[April(Actual)],
IsNull(May,0)[May(Actual)],
IsNull(June,0)[June(Actual)],
IsNull(July,0)[July(Actual)],
IsNull(August,0)[August(Actual)],
IsNull(September,0)[September(Actual)],
IsNull(October,0)[October(Actual)],
IsNull(November,0)[November(Actual)],
IsNull(December,0)[December(Actual)]
From
(
Select
Code,
[Month],
Value
From
#tempMonthWiseActualValue
)s
Pivot
(
min([Value] )
For [Month]
In (January,February,March,April,May,June,July,August,September,October,November,December)
)as pvt
Order by Code
但是有一个限制因为我需要保留所有的值,所以这里没有聚合函数可以工作,即没有MIN,MAX,SUM等。使用Min / Max会给我125或2250的值列。
所以我的问题是:
有没有办法通过使用数据透视表找到所需的结果,还是我应该去#temp表?
答案 0 :(得分:0)
我知道这是一个非常狭窄的解决方法。它的目的是强调将数据放在不同月份的同一行上的问题。 如果你描述了ovarall任务,那么我可以建议更好的解决方案。
Select
Code as Code
Value as May
from Table
where Month = 'May'
答案 1 :(得分:0)
在我的场景中,使用Row_number()将允许获得所需的输出,即使有多个月和一个月的值比其他值多,它也会处理。希望它可以帮助面临类似情况的人:
Select
Code,
rnum,
IsNull(January,0)[January(Value)],
IsNull(February,0)[February(Value)],
IsNull(March,0)[March(Value)],
IsNull(April,0)[April(Value)],
IsNull(May,0)[May(Value)],
IsNull(June,0)[June(Value)],
IsNull(July,0)[July(Value)],
IsNull(August,0)[August(Value)],
IsNull(September,0)[September(Value)],
IsNull(October,0)[October(Value)],
IsNull(November,0)[November(Value)],
IsNull(December,0)[December(Value)]
From
(
Select
Code,
ROW_NUMBER() Over (Order by nSRNo) [rnum],
[Month],
Value
From
#tempMonthWiseActualValue
)s
Pivot
(
Max([Value] )
For [Month]
In (January,February,March,April,May,June,July,August,September,October,November,December)
)as pvt
答案 2 :(得分:0)
有两种方法可以实现这个目标
首先,使用简单的案例:)
SELECT
Code AS Code,
CASE WHEN Month = 'January' THEN Value ELSE 0 END AS January ,
CASE WHEN Month = 'February' THEN Value ELSE 0 END AS February ,
CASE WHEN Month = 'March' THEN Value ELSE 0 END AS March ,
CASE WHEN Month = 'April' THEN Value ELSE 0 END AS April ,
CASE WHEN Month = 'May' THEN Value ELSE 0 END AS May ,
CASE WHEN Month = 'June' THEN Value ELSE 0 END AS June ,
CASE WHEN Month = 'July' THEN Value ELSE 0 END AS July ,
CASE WHEN Month = 'August' THEN Value ELSE 0 END AS August ,
CASE WHEN Month = 'September' THEN Value ELSE 0 END AS September ,
CASE WHEN Month = 'October' THEN Value ELSE 0 END AS October ,
CASE WHEN Month = 'November' THEN Value ELSE 0 END AS November,
CASE WHEN Month = 'December' THEN Value ELSE 0 END AS December
FROM CodePivot1
其次,使用Pivot和Rownum
SELECT Code,
ISNULL(January,0) AS January,
ISNULL(February,0) AS February,
ISNULL(March,0) AS March,
ISNULL(April,0) AS April,
ISNULL(May,0) AS May,
ISNULL(June,0) AS June,
ISNULL(July,0) AS July,
ISNULL(August,0) AS August,
ISNULL(September,0) AS September,
ISNULL(October,0) AS October,
ISNULL(November,0) AS November,
ISNULL(December,0) AS December
FROM
(
SELECT
Code, [Month], Value, ROW_NUMBER() OVER (ORDER BY Code) [rnum]
FROM
CodePivot1
)s
PIVOT
(
MAX([Value])
FOR [Month]
IN (January,February,March,April,May,June,July,August,September,October,November,December)
)as p