This is an alternative that basically divides a set of rows into two columns, it also allows the amount to be odd.
DECLARE @Personas TABLE (
Nombre VARCHAR(255)
)
INSERT INTO @Personas (Nombre)
SELECT 'David Lopez' UNION
SELECT 'danilo cruz' UNION
SELECT 'Carlos perez' UNION
SELECT 'sebastian corea' UNION
SELECT 'Juana maria' UNION
SELECT 'maria dinora' UNION
SELECT 'Patricia aguilar' UNION
SELECT 'kurt heiz' UNION
SELECT 'Luis Perez'
;WITH CTE AS (
SELECT Nombre,
ROW_NUMBER() OVER(ORDER BY Nombre) AS RN,
(SELECT COUNT(1) FROM @Personas) AS CT
FROM @Personas
)
SELECT T1.Nombre,
T2.Nombre
FROM CTE T1
LEFT JOIN CTE T2
ON T2.rn = T1.RN + CEILING(T1.CT/2.0)
WHERE T1.RN <= CEILING(T1.CT/2.0)
Exit:
+--------------+------------------+
| Carlos perez | Luis Perez |
+--------------+------------------+
| danilo cruz | maria dinora |
+--------------+------------------+
| David Lopez | Patricia aguilar |
+--------------+------------------+
| Juana maria | sebastian corea |
+--------------+------------------+
| kurt heiz | NULL |
+--------------+------------------+
Explanation:
- First of all I create a temporary table
@Personas
with the data you gave, plus an additional case to make the odd list and test the operation in these cases
- Then we use a CTE recursively to make
JOIN
between the registers on the left and the right side