UNPIVOT 上的聚合和别名
Aggregate and alias on UNPIVOT
如何重命名(别名)默认州(例如:QLD 到 Quensland)作为以下查询的输出?
另外,我如何将各州的预测汇总(SUM)到更高级别(例如:国家)?
SELECT PRODUCT_BK, Month, State, Forecast
FROM
(SELECT * FROM Forecast_Table) t
UNPIVOT
(Forecast FOR State IN (SA_NT, QLD, VIC_TAS, WA, NSW_ACT)) AS fcst
谢谢
这是 的跟进。
您可以将 case
用于此类目的
SELECT PRODUCT_BK,
MONTH,
CASE [State] WHEN 'QLD' THEN 'Quensland'
WHEN 'SA_NT' THEN 'something else'
ELSE [State]
END AS [State],
Forecast
FROM (SELECT * FROM Forecast_Table) t
UNPIVOT (Forecast FOR State IN (SA_NT, QLD, VIC_TAS, WA, NSW_ACT)) AS fcst
或者,作为替代方法,您可以在您的数据库中创建 #temp_table
或 @variable_table
或 table,用新旧名称填充此 table 并加入 final用这个 table.
输出
示例:
-- create variable table to store reference between old and new alias
DECLARE @Sometable AS TABLE
(
OldName NVARCHAR(10) ,
ShouldBeName NVARCHAR(20)
)
INSERT @Sometable
( OldName, ShouldBeName )
VALUES ( 'SA_NT', 'Some Name 1' ),
( 'QLD', 'Some Name 2' ),
( 'VIC_TAS', 'Some Name 3' );
--final query
SELECT fcst.PRODUCT_BK ,
fcst.Month ,
COALESCE(S.ShouldBeName, fcst.[State]) AS [State] ,
fcst.Forecast
FROM ( SELECT * FROM Forecast_Table ) t
UNPIVOT ( Forecast FOR State IN ( SA_NT, QLD, VIC_TAS, WA, NSW_ACT ) ) AS fcst
LEFT JOIN @Sometable AS S ON fcst.[State] = S.OldName
或者,甚至像这样:
SELECT fcst.PRODUCT_BK ,
fcst.Month ,
COALESCE(S.ShouldBeName, fcst.[State]) AS [State] ,
fcst.Forecast
FROM ( SELECT * FROM Forecast_Table ) t
UNPIVOT ( Forecast FOR State IN ( SA_NT, QLD, VIC_TAS, WA, NSW_ACT ) ) AS fcst
LEFT JOIN ( SELECT *
FROM ( VALUES ( 'SA_NT', 'Some Name 1' ),
( 'QLD', 'Some Name 2' ),
( 'VIC_TAS', 'Some Name 3' )
) AS S ( OldName, ShouldBeName )
) AS S ON fcst.[State] = S.OldName
如何重命名(别名)默认州(例如:QLD 到 Quensland)作为以下查询的输出? 另外,我如何将各州的预测汇总(SUM)到更高级别(例如:国家)?
SELECT PRODUCT_BK, Month, State, Forecast
FROM
(SELECT * FROM Forecast_Table) t
UNPIVOT
(Forecast FOR State IN (SA_NT, QLD, VIC_TAS, WA, NSW_ACT)) AS fcst
谢谢
这是
您可以将 case
用于此类目的
SELECT PRODUCT_BK,
MONTH,
CASE [State] WHEN 'QLD' THEN 'Quensland'
WHEN 'SA_NT' THEN 'something else'
ELSE [State]
END AS [State],
Forecast
FROM (SELECT * FROM Forecast_Table) t
UNPIVOT (Forecast FOR State IN (SA_NT, QLD, VIC_TAS, WA, NSW_ACT)) AS fcst
或者,作为替代方法,您可以在您的数据库中创建 #temp_table
或 @variable_table
或 table,用新旧名称填充此 table 并加入 final用这个 table.
示例:
-- create variable table to store reference between old and new alias
DECLARE @Sometable AS TABLE
(
OldName NVARCHAR(10) ,
ShouldBeName NVARCHAR(20)
)
INSERT @Sometable
( OldName, ShouldBeName )
VALUES ( 'SA_NT', 'Some Name 1' ),
( 'QLD', 'Some Name 2' ),
( 'VIC_TAS', 'Some Name 3' );
--final query
SELECT fcst.PRODUCT_BK ,
fcst.Month ,
COALESCE(S.ShouldBeName, fcst.[State]) AS [State] ,
fcst.Forecast
FROM ( SELECT * FROM Forecast_Table ) t
UNPIVOT ( Forecast FOR State IN ( SA_NT, QLD, VIC_TAS, WA, NSW_ACT ) ) AS fcst
LEFT JOIN @Sometable AS S ON fcst.[State] = S.OldName
或者,甚至像这样:
SELECT fcst.PRODUCT_BK ,
fcst.Month ,
COALESCE(S.ShouldBeName, fcst.[State]) AS [State] ,
fcst.Forecast
FROM ( SELECT * FROM Forecast_Table ) t
UNPIVOT ( Forecast FOR State IN ( SA_NT, QLD, VIC_TAS, WA, NSW_ACT ) ) AS fcst
LEFT JOIN ( SELECT *
FROM ( VALUES ( 'SA_NT', 'Some Name 1' ),
( 'QLD', 'Some Name 2' ),
( 'VIC_TAS', 'Some Name 3' )
) AS S ( OldName, ShouldBeName )
) AS S ON fcst.[State] = S.OldName