row and column conversion

sghssh-2725 40 Reputation points
2023-05-29T02:38:54.9+00:00

Hello.

I need help with row and column conversion issues.

User's image

Output:

User's image

Many thanks.

SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-05-29T02:46:55.6466667+00:00

    Hi @sghs sh

    Try this query.

    create table test(Id int,Name varchar(20),Dept int,Season1 int,Season2 int,Season3 int,Season4 int)
    insert into test values
    (1,'SWER',20,7,6,10,3),
    (2,'MARS',30,4,8,7,2);
    
    ;with CTE as(
      select * from test
      unpivot(Amount for Season in(Season1,Season2,Season3,Season4)) t)
    select Id,Name,Dept,right(Season,1) as Season,Amount from CTE;
    

    Output:

    User's image

    Best regards,

    Percy Tang


    If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.