For example, here's what a sample dataset looks like in which ID, A, B, C are my column names:
ID A B C
L1 1 0 1
L2 1 1 1
L3 0 0 1
Here's what I would like the output to show, when the value of that column name equates to 1:
ID Combination
L1 A
L1 C
L1 A,C
L2 A
L2 B
L2 C
L2 A,B
L2 B,C
L2 A,C
L2 A,B,C
and so on...
Please note this is just a sample representation of what my dataset looks like, and what I am hoping the output should look like. In my actual dataset, I have over a 100 columns and a few million rows.
2条答案
按热度按时间klh5stk11#
You can use
CROSS APPLY
s to effectively join your starting data with a pair of values - one null value (always present) and and one non-null value (conditionally present). Repeat for each source column and then concatenate those intermediate results.For that last step, the
CONCAT_WS()
function will nicely handle the combination of null and non-null values while inserting delimiters (commas) in the appropriate places. Add a check to exclude the empty case and you should have the desired results.Here is a more compact variation:
See this db<>fiddle for a demo.
Results:
| ID | Combination |
| ------------ | ------------ |
| L1 | A |
| L1 | C |
| L1 | A,C |
| L2 | A |
| L2 | B |
| L2 | C |
| L2 | A,B |
| L2 | A,C |
| L2 | B,C |
| L2 | A,B,C |
| L3 | C |
If you have many columns to deal with, you might consider looking into dynamic SQL techniques to generate the needed query, which might yield something like this .
bvpmtnay2#
The following code, available in a dbfiddle , demonstrates one approach. You can use the alternate
select
statements at the end of the common table expression (CTE) to examine the intermediate results and better understand the steps involved.