I have this table in SQL Server:
| Process | ProcessedActivity | BusinessDefinition | SystemError |
| ------------ | ------------ | ------------ | ------------ |
| LADG3 | 2 | 1 | 3 |
| TEF2020 | 1 | 4 | 1 |
I need to convert it to this format:
Process | Status |
---|---|
LADG3 | ProcessedActivity |
LADG3 | ProcessedActivity |
LADG3 | BusinessDefinition |
LADG3 | SystemError |
LADG3 | SystemError |
LADG3 | SystemError |
TEF2020 | ProcessedActivity |
TEF2020 | BusinessDefinition |
TEF2020 | BusinessDefinition |
TEF2020 | BusinessDefinition |
TEF2020 | BusinessDefinition |
TEF2020 | SystemError |
I expect to convert each number from the columns ProcessedActivity
, BusinessDefinition
and SystemError
into a message and join this information into only one new column.
How can I do this double conversion in SQL Server?
1条答案
按热度按时间xfb7svmp1#
You need a numbers or tally table for this. If you are using sql server 2022 this is easily done with GENERATE_SERIES. But since that is really new at this point I will assume you are using an older version. I tackle that using this view which I keep on nearly all my databases.
Regardless of how you generate your tally table this becomes fairly simple. I did this using three queries. There are other ways you could tackle this as well but this will perform reasonably quick and the coding is simple. I used the ExplodedRows cte here so it would allow adding a sort order to all the rows once they are materialized.