我在执行时使用以下SQL脚本来取消数据我收到此错误:在UNPIVOT运算符中指定与UNPIVOT参数中的现有列名冲突。
SELECT
HelpDeskName
, TRY_CONVERT(NUMERIC(10,4),loggedInAgents) AS loggedInAgents
, TRY_CONVERT(NUMERIC(10,4),AvailableAgents) AS AvailableAgents
, TRY_CONVERT(NUMERIC(10,4),UnAvailableAgent) AS UnAvailableAgent
, TRY_CONVERT(NUMERIC(10,4),TotalCalls) AS TotalCalls
, TRY_CONVERT(NUMERIC(10,4),CallsHandled) AS CallsHandled
, ReceivedTime
FROM [Final].[UCCX] AS U
UNPIVOT
(
HelpDeskName,
ReceivedTime
for Category in (LoggedInAgents,AvailableAgents,UnAvailableAgent,TotalCalls,CallsHandled)
) Z ;
我希望看到的unpivot的输出列是HelpDeskName,ReceivedTime,Category,CategoryValue。请提供建议或帮助解决此查询。
答案 0 :(得分:5)
两个快速选项
选项1 - 通过交叉申请
Cross Apply方法提供了更多的自由度/灵活性。例如,您可以轻松地重命名类别或重新投射值。
Select A.HelpDeskName
,A.RecievedTime
,B.*
From YourTable A
Cross Apply (values ('LoggedInAgents' ,A.LoggedInAgents)
,('AvailableAgents' ,A.AvailableAgents)
,('UnavailableAgents',A.UnavailableAgents)
,('TotalCalls' ,A.TotalCalls)
,('CallsHandled' ,A.CallsHandled)
) B (Category,CategoryValue)
选项2 - 通过UnPivot
Select HelpDeskName,RecievedTime,Category,CategoryValue
From YourTable
UnPivot ( CategoryValue For Category in (LoggedInAgents,AvailableAgents,UnavailableAgents,TotalCalls,CallsHandled) ) u;
两者都会返回类似
的内容