Hi,
Can someone please help me in order to create unique combinations of 6 names from total 10 names. Names are given in a single column.
Hi,
Can someone please help me in order to create unique combinations of 6 names from total 10 names. Names are given in a single column.
You need to provide more information that that. Please provide DDL, data and expected outcome
homework?
Hi,
DROP TABLE DaTable;
GO
CREATE TABLE dbo.DaTable(
id int IDENTITY(1, 1) NOT NULL,
name VARCHAR(20) NOT NULL,
CONSTRAINT pk_DaTable PRIMARY KEY (id)
);
INSERT INTO dbo.DaTable(name) VALUES
('Me'),
('Myself'),
('And'),
('Ai'),
('Running'),
('Out'),
('Of'),
('Ideas'),
('For'),
('Names')
SELECT * FROM dbo.DaTable
GO
--first select 6 names from the tabel with more than 6 names
WITH top6names AS (
SELECT top 6 id, name
FROM dbo.DaTable
ORDER BY id
)
-- create all possible combinations of those 6 names
-- by adding DISTINCT you are sure that the same combination of two names will not occur in the result
--(John, Doe), (John, Doe), (John, Doe) will result in (John, Doe)
SELECT DISTINCT FirstNames.name, SecondNames.name
FROM top6names as FirstNames,
top6names as SecondNames
--remove the next comments if you want to exclude the combination of a name with itself, so (John, John) will not occur in the result
--WHERE FirstNames.name <> SecondNames.name