简体   繁体   中英

How to Get multiple user update history in SQL Server 2008

Please give some idea about how to get the users update history of particular id in SQL Server 2008. I need to display their user names in (update) username column.

Example:

In database I have update username column in z table. The usernames are like shown here:

username
1.x
2.y

What I need is:

first x username going to update some values for ID:1 in z table and save the details. again y username also update some values for same ID in z Table and save details.

In the end, I need an output that looks like this

Z table

    ID | column1| column2| update_username
     1 |    a   |  b     |    x,y

Use this query to ge the expected result in MSSQL server .

CREATE TABLE #Z
(
 ID INT,
 Name VARCHAR(50)
 )

 INSERT INTO #Z 
 SELECT 1, '1.x' UNION
 SELECT 1, '2.y' UNION
 SELECt 2, '1.x' UNION
 SELECT 2, '2.y'

  SELECT A.ID,  Update_username = STUFF(
    (SELECT ', ' + SUBSTRING(B.Name, CHARINDEX('.', B.Name) + 1, LEN(B.Name))  
     FROM #Z AS B 
     WHERE B.ID = A.ID 
     GROUP BY B.Name 
     FOR XML PATH(''), type).value('.', 'varchar(max)'), 1,2,'') 
 FROM #Z AS A
 GROUP BY A.ID  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM