Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpivot Multiple columns from specific Table

I am using following SQL Script to unpivot the data when I execute I am receiving this error : specified in the UNPIVOT operator conflicts with the existing column name in the UNPIVOT argument.

 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 ;

The output columns from unpivot I would like to see is HelpDeskName,ReceivedTime, Category, CategoryValue. Please advise or help with this Query.

enter image description here

enter image description here

like image 991
Justin Avatar asked Apr 19 '17 19:04

Justin


People also ask

How do I Unpivot multiple columns?

Select the columns you do want to unpivot. To select more than one column contiguously or discontiguously, press Shift+Click or CTRL+Click on each subsequent column. Select Transform > Unpivot Only Selected Columns.

How do I Unpivot selected columns in Excel?

You can select Ctrl as you select as many columns as you need. For this scenario, you want to select all the columns except the one named Country. After selecting the columns, right-click any of the selected columns, and then select Unpivot columns.

How do I Unpivot data from a table in Excel?

Unpivot your data. In the "Query Editor," right-click on the first column and click "Unpivot Other Columns." This unpivots the data in your other columns within your Excel table.

What is the difference between Unpivot columns and Unpivot other columns?

Unpivot produces the opposite result than what we just experienced with Pivot. Unpivot will convert you column name into one column into rows and your values into another column.


1 Answers

Two Quick Options

Option 1 - Via Cross Apply

The Cross Apply approach offers a bit more freedom/flexibility. For example you can easily rename the Categories or re-cast the values.

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)

Option 2 - Via UnPivot

Select HelpDeskName,RecievedTime,Category,CategoryValue
 From  YourTable
UnPivot ( CategoryValue For Category in (LoggedInAgents,AvailableAgents,UnavailableAgents,TotalCalls,CallsHandled) ) u;

Both would return something like this

enter image description here

like image 89
John Cappelletti Avatar answered Oct 18 '22 20:10

John Cappelletti