How to unpivot multiple columns in same table

How to unpivot multiple columns in same table

I have a table with below structure, I need to unpivot the output so that I get one row per ID per PARAMETER and it's corresponding RATINGS

I have a table with below structure, I need to unpivot the output so that I get one row per ID per PARAMETER and it's corresponding RATINGS

create table RATINGS(ID INT,PARAMETER1 INT,PARAMETER2 INT,PARAMETER3 INT,PARAMETER4 INT)
insert into RATINGS values(1000,1,3,2,1)
insert into RATINGS values(1002,2,3,3,2)
insert into RATINGS values(1007,3,3,2,1)
insert into RATINGS values(1015,1,3,1,3)
insert into RATINGS values(1019,3,2,1,1)

expected output:

ID    PARAMETERS RATING
1000  PARAMETER1  1
1000  PARAMETER2  3
1000  PARAMETER3  2
1000  PARAMETER4  1
1002  PARAMETER1  2
1002  PARAMETER2  3
1002  PARAMETER3  3
1002  PARAMETER4  2
1007  PARAMETER1  3
1007  PARAMETER2  3
1007  PARAMETER3  2
1007  PARAMETER4  1
1015  PARAMETER1  1
1015  PARAMETER2  3
1015  PARAMETER3  1
1015  PARAMETER4  3
1019  PARAMETER1  3
1019  PARAMETER1  2
1019  PARAMETER1  1
1019  PARAMETER1  1

Later, I also need to filter so that I get only those rows that have ratings 1 and 2. So the output then must be

ID    PARAMETERS RATING
1000  PARAMETER1  1
1000  PARAMETER3  2
1000  PARAMETER4  1
1002  PARAMETER1  2
1002  PARAMETER4  2
1007  PARAMETER3  2
1007  PARAMETER4  1
1015  PARAMETER1  1
1015  PARAMETER3  1
1019  PARAMETER1  2
1019  PARAMETER1  1
1019  PARAMETER1  1

I am able to get the first two ID and PARAMETERS column using below query:

select ID,[parameters] from RATINGS
unpivot
(
[value] for [PARAMETERS] in (PARAMETER1,PARAMETER2,PARAMETER3,PARAMETER4)
) unpvt

OUTPUT:

**ID**      **PARAMETERS**
1000    PARAMETER1
1000    PARAMETER2
1000    PARAMETER3
1000    PARAMETER4
1002    PARAMETER1
1002    PARAMETER2
1002    PARAMETER3
1002    PARAMETER4
1007    PARAMETER1
1007    PARAMETER2
1007    PARAMETER3
1007    PARAMETER4
1015    PARAMETER1
1015    PARAMETER2
1015    PARAMETER3
1015    PARAMETER4
1019    PARAMETER1
1019    PARAMETER2
1019    PARAMETER3
1019    PARAMETER4

Could someone please let me know how to get the RATING column

sql sql-server

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Welcome Back the T-SQL Debugger with SQL Complete – SQL Debugger

Debug SQL stored procedures and develop your SQL database project with dbForge SQL Complete, a new add-in for Visual Studio and SSMS. When you develop large chunks of T-SQL code with the help of the SQL Server Management Studio tool, it is essential to test the “Live” behavior of your code by making sure that each small piece of code works fine and being able to allocate any error message that may cause a failure within that code.

The Easy Guide on How to Use Subqueries in SQL Server

This article provides an outlook on various types of subqueries in SQL such as select or other T-SQL statements and caveats when using them.

Learning How to Transition Your SQL Server Skills to Azure SQL

Are you interested in learning how to translate your existing SQL Server expertise to Azure SQL including Azure SQL Database and Azure SQL Managed Instance? In this episode, Bob Ward, Anna Hoffman, and Marisa Brasile announce all-new content on YouTube, Github, and Microsoft Learn to help you become an Azure SQL professional.