Based on the context from your previous question SQL query for combinations without repitition I think you are looking for a way to find combinations of users and include both the name and ID in the result set. The following script demonstrates one way to achieve that:
Sample data:
DECLARE @Users AS TABLE
(
UserID integer,
UserName nvarchar(50)
);
INSERT @Users
(UserID, UserName)
VALUES
(12, N'Tom'),
(132, N'Ann'),
(32, N'Dina'),
(2, N'Mark');
Load the source data into a table set up to make it easier to find combinations:
-- Working table to find combinations
DECLARE @Combination AS TABLE
(
item_id tinyint IDENTITY(1,1) PRIMARY KEY NONCLUSTERED,
item nvarchar(500) NOT NULL,
item_value integer NOT NULL,
bit_value AS
CONVERT
(
integer,
POWER(2, item_id - 1)
)
PERSISTED UNIQUE CLUSTERED
);
-- Add user details to the working table
INSERT @Combination
(item, item_value)
SELECT
u.UserName,
u.UserID
FROM @Users AS u;
Use the fact that the natural integers have a pattern of bits that match the combinations we want to find. Determine the maximum integer value we need based on the number of items in the working table:
-- Maximum integer needed for combination bit values
DECLARE
@max integer =
POWER(2,
(
SELECT COUNT_BIG(*)
FROM @Combination
)
) - 1;
Instead of concatenating group members using FOR XML PATH
, list the combinations in rows with a group_id
to identify each group:
-- Find combinations
SELECT
group_id = N.n,
c.item,
c.item_value
FROM @Combination AS c
JOIN dbo.Numbers AS N ON
n.n & c.bit_value = c.bit_value
WHERE
N.n BETWEEN 1 AND @max
ORDER BY
group_id;
Partial output:
╔══════════╦══════╦════════════╗
║ group_id ║ item ║ item_value ║
╠══════════╬══════╬════════════╣
║ 1 ║ Tom ║ 12 ║
║ 2 ║ Ann ║ 132 ║
║ 3 ║ Ann ║ 132 ║
║ 3 ║ Tom ║ 12 ║
║ 4 ║ Dina ║ 32 ║
║ 5 ║ Dina ║ 32 ║
║ 5 ║ Tom ║ 12 ║
║ 6 ║ Ann ║ 132 ║
║ 6 ║ Dina ║ 32 ║
║ 7 ║ Dina ║ 32 ║
║ 7 ║ Ann ║ 132 ║
║ 7 ║ Tom ║ 12 ║
║ 8 ║ Mark ║ 2 ║
║ 9 ║ Mark ║ 2 ║
║ 9 ║ Tom ║ 12 ║
╚══════════╩══════╩════════════╝