Converting a Column to Multiple Rows Using SQL Server
In this article, we’ll explore how to convert a column in a SQL Server table into multiple rows using a single query. We’ll cover the basics of SQL and provide an example to illustrate this concept.
Understanding SQL Tables
A SQL table is a collection of data organized into rows and columns. Each row represents a single record or entry, while each column represents a field or attribute of that record. In our case, we have a table called StudesntMarkList with several columns:
StudentIDStudentNamePerformanceclassSection- Several subject-specific fields (e.g.,
subject1,subject2, etc.)
The Problem
The problem at hand is to transform this table so that each value in the long list of columns becomes a separate row. In other words, we want to convert the following data:
| StudentID | StudentName | Performance | class | Section | subject1 | subject mark1 | subject2 | subject mark2 | subject3 | subject mark3 | subject4 | subject mark4 | subject5 | subject mark5 | subject6 | subject mark6 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | shiva | not bad | 10th | c | science | 58 | Social | English | 70 | maths | biology | 67 | 50 |
into the following:
| ID | Att | Att Val | Val |
|---|---|---|---|
| 1 | Student Name | Shiva | |
| 1 | Performance | not bad | |
| 1 | class | 10th | |
| 1 | Section | c | |
| 1 | subject1 | science | 58 |
| 1 | subject2 | Social | |
| 1 | subject3 | English | 70 |
| 1 | subject4 | maths | |
| 1 | subject5 | biology | 67 |
| 1 | subject6 | 50 |
The Solution
To achieve this transformation, we’ll use a technique called CROSS APPLY in combination with the VALUES function.
Understanding CROSS APPLY
CROSS APPLY is a SQL Server feature that allows you to join two tables together when one of them has only one column. In our case, we want to apply some logic to each row of the table and generate new rows as a result.
The Solution Query
Here’s the query that achieves the desired transformation:
SELECT StudentID,
v.*
FROM StudesntMarkList
CROSS APPLY (VALUES
('StudentName',StudentName,''),
('Performance',Performance,''),
('class',class,''),
('subject1',subject1,subjectmark1),
('subject2',subject2,subjectmark2),
('subject3',subject3,subjectmark3),
('subject4',subject4,subjectmark4),
('subject5',subject5,subjectmark5),
('subject6',subject6,subjectmark6)
) v(att,attval,Val)
Let’s break this query down:
CROSS APPLYis used to join theStudesntMarkListtable with a derived table created by theVALUESfunction.- The
VALUESfunction returns multiple values as separate rows. In our case, we have nine values (one for each subject field). - Each value returned by
VALUEShas three parts: an attribute name (att), a value name (attval), and a value itself (Val). We’re interested in the value part. - The
SELECTstatement then selects all columns from the derived table (v) using the attribute names as aliases.
Explanation and Example
Here’s a step-by-step explanation of how this query works:
- The
CROSS APPLYoperator applies the logic defined by theVALUESfunction to each row of theStudesntMarkListtable. - For each row, it generates nine new rows using the values returned by
VALUES. - Each new row represents a separate attribute-value pair (e.g.,
StudentName,StudentID, etc.). - The
SELECTstatement then selects all columns from these derived tables, using the attribute names as aliases.
Let’s take a closer look at how this works with our example data:
| StudentID | StudentName | Performance | class | Section |
|---|---|---|---|---|
| 1 | shiva | not bad | 10th | c |
When we run the query, it generates the following nine new rows:
| ID | Att | Att Val | Val |
|---|---|---|---|
| 1 | Student Name | Shiva | |
| 1 | Performance | not bad | |
| 1 | class | 10th | |
| 1 | Section | c | |
| 1 | subject1 | science | 58 |
| 1 | subject2 | Social | |
| 1 | subject3 | English | 70 |
| 1 | subject4 | maths | |
| 1 | subject5 | biology | 67 |
| 1 | subject6 | 50 |
As you can see, each value from the original table has been converted into a separate row.
Conclusion
In this article, we’ve seen how to convert a column in a SQL Server table into multiple rows using the CROSS APPLY function and the VALUES clause. This technique is useful when you need to transform data in a flexible and efficient way.
Last modified on 2024-09-18