**Using pivot to get that result :**
CREATE TABLE #details(Id INT,NAME VARCHAR(100), RIGHTS VARCHAR(100))
INSERT INTO #details(Id ,NAME , RIGHTS)
SELECT 1,'Steven','add' UNION ALL
SELECT 1,'Steven','update' UNION ALL
SELECT 1,'Steven','delete' UNION ALL
SELECT 1,'Mark','update' UNION ALL
SELECT 1,'Mark','delete' UNION ALL
SELECT 1,'Joseph','don’t have Rights' UNION ALL
SELECT 1,'Spike','add' UNION ALL
SELECT 1,'Spike','update' UNION ALL
SELECT 1,'Spike','delete'
SELECT *
FROM
(
SELECT NAME , RIGHTS ,Id
FROM #details
)A
PIVOT
(
MAX(Id) FOR RIGHTS IN ([don’t have Rights],[add],[update],[delete])
)pvt
**For Dynamic pivot use below query :**
DECLARE @DynamicCol VARCHAR(MAX) = '',@DynamicPvt VARCHAR(MAX) = ''
SELECT @DynamicCol =
(
SELECT STUFF ((SELECT DISTINCT ',' + '[' + RTRIM(RIGHTS) + ']' FROM
#details FOR XML PATH('')),1,1,'')
)
SET @DynamicPvt = 'SELECT *
FROM
(
SELECT NAME , RIGHTS ,Id
FROM #details
)A
PIVOT
(
MAX(Id) FOR RIGHTS IN ('[email protected]+')
)pvt'
EXEC (@DynamicPvt)
這是任何特定的數據庫? SQL Server? MySQL的?等等? –
使用SQL Server – SAURABH
Saurabh,Name&Rights是兩個不同的列? –