SQL Server query unpivot

NeophyteSQL 241 Reputation points
2024-02-27T21:03:20.11+00:00

The data in the column is in the form of rows like below . How to convert to columns

RIGHT ankle:

Migration Percentage (MP): 36.6%

Previous MP: NA%

NULL

LEFT ankle:

Migration Percentage (MP): 35.2%

Previous MP: NA%

the expected result is in the column values below and the column names as below

right ankle migration pct Left Ankle Migration Pct

36 % 35.2%

SQL Server Other
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2024-02-28T22:13:02.9+00:00

    I'm afraid that you lose. Recall that a table is a set, that is, an unordered object. So what you have are just some dotted random unrelated annotations. There are two ankles, there are two migration percentages and some more junk. But there is nothing that says which migration percentages is related to which ankle. If there is any relation to any ankle at all, that is.

    You need to go back to the data source and make sure that data is stored in a manageable way that upholds any relations that might have been there initially.

    0 comments No comments

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.