sql >> Databáze >  >> RDS >> Sqlserver

SQL Server 2008 - Zobrazení položek řádků ve sloupci bez PIVOT

Můžete to zkusit?

WITH A AS (SELECT HMYPERSON, HMYTENANT, SRELATIONSHIP, P.ULASTNAME, p.SFIRSTNAME, ROW_NUMBER() OVER (PARTITION BY HMYTENANT ORDER BY HMYPERSON) AS RN
            FROM ROOM R
            INNER JOIN PERSON P ON P.HMY = R.HMYPERSON  
                                AND (R.SRELATIONSHIP <> 'Guarantor' OR P.IPERSONTYPE<>90)           
            )
SELECT T.SCODE, T.SLASTNAME + ' ' + T.SFIRSTNAME TENANTNAME
        , A1.ULASTNAME + ' ' + A1.SFIRSTNAME ROOMMATENAME_1
        , A2.ULASTNAME + ' ' + A2.SFIRSTNAME ROOMMATENAME_2
        , A3.ULASTNAME + ' ' + A3.SFIRSTNAME ROOMMATENAME_3
        , A4.ULASTNAME + ' ' + A4.SFIRSTNAME ROOMMATENAME_4
        , A5.ULASTNAME + ' ' + A5.SFIRSTNAME ROOMMATENAME_5
FROM TENANT T
LEFT JOIN A A1 ON A1.HMYTENANT = T.HMYPERSON AND A1.RN=1
LEFT JOIN A A2 ON A2.HMYTENANT = T.HMYPERSON AND A2.RN=2
LEFT JOIN A A3 ON A3.HMYTENANT = T.HMYPERSON AND A3.RN=3
LEFT JOIN A A4 ON A4.HMYTENANT = T.HMYPERSON AND A4.RN=4
LEFT JOIN A A5 ON A5.HMYTENANT = T.HMYPERSON AND A5.RN=5
WHERE T.HMYPERSON=1

Výstup:

SCODE TENANTNAME  ROOMMATENAME_1           ROOMMATENAME_2           ROOMMATENAME_3           ROOMMATENAME_4           ROOMMATENAME_5
----- ----------- ------------------------ ------------------------ ------------------------ ------------------------ ------------------------
t0011 lName fName roommate2 roommate2Fname roommate3 roommate3Fname roommate1 roommate1Fname NULL               NULL



  1. Jak používáte proměnné skriptu v psql?

  2. ORA-00979 není skupina podle výrazu

  3. PostgreSQL podřetězec získat řetězec mezi hranaté závorky

  4. Podmíněné podmínky v SQL Server