0

I'm trying to convert the column data to rows.

For example:

**ID          Date      Name     Work Desc      Morning Hrs AfterNoon Hrs   Night Hrs Misc Hrs**
201345  12/18/2019  Kobe    Done 24 tickets     4          6              2          0
201346  12/18/2019  Lebron  Need to complete    3          5              6          0
201347  1/2/2020    Steph   Do something        4          6              7          0
121     12/19/2019  Lebron  Leave               0          0              0          8

I need the records as below

**ID          Date      Name     Work Desc       Task Hrs   Activity**
201345  12/18/2019  Kobe    Done 24 tickets     4       Morning
201345  12/18/2019  Kobe    Done 24 tickets     6       Afternoon
201345  12/18/2019  Kobe    Done 24 tickets     2       Night
201346  12/18/2019  Lebron  Need to complete    3       Morning
201346  12/18/2019  Lebron  Need to complete    5       Afternoon
201346  12/18/2019  Lebron  Need to complete    6       Night
201347  1/2/2020    Steph   Do something        4       Morning
201347  1/2/2020    Steph   Do something        6       Afternoon
201347  1/2/2020    Steph   Do something        7       Night
121     12/19/2019  Lebron  Leave               8       Leave

In an SQL Query. Please help me through this. Thanks in advance!!

Larnu
  • 61,056
  • 10
  • 27
  • 50
  • Dear @Larnu can you please provide the sql query for this example? I'm facing some issues while writing the same which you have suggested. I need only one record for "LEAVE". Please help me Thanks in Advance :) – samhith gardas Feb 05 '20 at 09:03
  • All the information you need is in the marked duplicate. have a read of the duplicate, and try implementing it yourself. If you fail, post a *new* question, with your attempts, and explain why they aren't working. – Larnu Feb 05 '20 at 09:11
  • select ID, Date, Name, Work_Desc , TaskHrs from myTable unpivot ( Taskhrs for Activity in (MorningHrs, AfternoonHrs, NightHrs) ) P; then I'm getting the result with row having Leave in WorkDesc as Morning,afternoon and night. If you don't mind can you write the query and give it to me? TIA – samhith gardas Feb 05 '20 at 09:14

0 Answers0