Sql server 是否使用SQL Server将多行合并为一行?
我想使用SQL Server中的存储过程合并通知数据。我已经编写了查询,但是在这个查询中,我得到的不仅仅是多行中的用户数据 我想合并跟踪通知。像用户a跟随用户b,然后用户b得到通知,就像用户a开始跟随你一样。用户c再次跟随用户b,然后用户b再次收到类似用户c跟随您的通知 用户d再次跟随用户b,然后用户b与获取通知相同。但现在我想像这样合并通知用户a、b和另外一个用户开始跟踪您 以下是我的存储过程:Sql server 是否使用SQL Server将多行合并为一行?,sql-server,Sql Server,我想使用SQL Server中的存储过程合并通知数据。我已经编写了查询,但是在这个查询中,我得到的不仅仅是多行中的用户数据 我想合并跟踪通知。像用户a跟随用户b,然后用户b得到通知,就像用户a开始跟随你一样。用户c再次跟随用户b,然后用户b再次收到类似用户c跟随您的通知 用户d再次跟随用户b,然后用户b与获取通知相同。但现在我想像这样合并通知用户a、b和另外一个用户开始跟踪您 以下是我的存储过程: SELECT N.NotificationId, N.UserId, N
SELECT
N.NotificationId,
N.UserId,
N.ActionUserId,
(CASE WHEN N.NotificationTypeId = 1 THEN 1
WHEN N.NotificationTypeId = 7 THEN 3
ELSE
2
END) AS TypeId,
AU.ProfileImage,
AU.UserName,
N.IsRead,
(CASE WHEN N.NotificationTypeId = 1 THEN 1
WHEN N.NotificationTypeId = 7 THEN 3
ELSE 2
END) AS TypeId,
N.NotificationTypeId,
N.InsertDateTime
FROM
Notifications N
INNER JOIN
Users U ON N.UserId = U.UserId
INNER JOIN
Users AU ON N.ActionUserId = AU.UserId
ORDER BY
N.InsertDateTime DESC
这是我当前的o/p=>
NotificationId | UserId | ActionUserId | UserName | NotificationTypeId | InsertDateTime | ProfileImage
6 20 15 hbc 1 2017-06-22 17:14:16.803 20170416032403869.jpeg
5 20 16 tyu 1 2017-06-22 17:12:12.297 20170416031522534.jpeg
4 20 17 opl 1 2017-06-22 17:11:58.060 20170416031250102.jpeg
3 10 11 abc 1 2017-06-22 16:14:16.803 20170416032403867.jpeg
2 10 12 xyz 1 2017-06-22 16:14:12.297 20170416031522533.jpeg
1 10 13 rty 1 2017-06-22 16:13:58.060 20170416031250101.jpeg
这是我的例外订单=>
NotificationId | UserId | ActionUserId | UserName | NotificationTypeId | InsertDateTime | ProfileImage | NotificationText
6 20 15 hbc 1 2017-06-22 17:14:16.803 20170416032403869.jpeg hbc,tyu and 1 other users followed you
3 10 11 abc 1 2017-06-22 16:14:16.803 20170416032403867.jpeg abc,xyz and 1 other users followed you
创建测试数据=>
declare @Notifications table(NotificationID int, UserID int, ActionUserID int, NotificationTypeID int, InsertDateTime datetime);
declare @Users table(UserID int, UserName nvarchar(10), ProfileImage nvarchar(50))
insert into @Notifications values (6,20,15,1,'2017-06-22 17:14:16.803'),(5,20,16,1,'2017-06-22 17:12:12.297'),(4,20,17,1,'2017-06-22 17:11:58.060'),(3,10,11,1,'2017-06-22 16:14:16.803'),(2,10,12,1,'2017-06-22 16:14:12.297'),(1,10,13,1,'2017-06-22 16:13:58.060');
insert into @Users values (15,'hbc','20170416032403869.jpeg'),(16,'tyu','20170416031522534.jpeg'),(17,'opl','20170416031250102.jpeg'),(10,'aaa',''),(11,'abc','20170416032403867.jpeg'),(12,'xyz','20170416031522533.jpeg'),(13,'rty','20170416031250101.jpeg');
试试这个:
declare @Notifications table
(NotificationID int,
UserID int,
ActionUserID int,
NotificationTypeID int,
InsertDateTime datetime);
declare @Users table
(UserID int,
UserName nvarchar(10),
ProfileImage nvarchar(50))
insert into @Notifications values
(6,20,15,1,'2017-06-22 17:14:16.803'),
(5,20,16,1,'2017-06-22 17:12:12.297'),
(4,20,17,1,'2017-06-22 17:11:58.060'),
(3,10,11,1,'2017-06-22 16:14:16.803'),
(2,10,12,1,'2017-06-22 16:14:12.297'),
(1,10,13,1,'2017-06-22 16:13:58.060');
insert into @Users values (15,'hbc','20170416032403869.jpeg'),
(16,'tyu','20170416031522534.jpeg'),
(17,'opl','20170416031250102.jpeg'),
(10,'aaa',''),
(20,'bbb',''),
(11,'abc','20170416032403867.jpeg'),
(12,'xyz','20170416031522533.jpeg'),
(13,'rty','20170416031250101.jpeg');
declare @followCount table
(
userid int,
cnt int
)
INSERT INTO @followCount
select N.UserID, count(*)
FROM
@Notifications N
INNER JOIN
@Users U ON N.UserId = U.UserId
INNER JOIN
@Users AU ON N.ActionUserId = AU.UserId
GROUP BY n.UserID
declare @msg table
(
userid int,
NotificationMsg varchar(15)
)
INSERT INTO @msg
SELECT DISTINCT N.UserID, Stuff((SELECT ', ' + UserName
FROM
(SELECT UserID, UserName FROM (SELECT N.UserId,
AU.UserName,
ROW_NUMBER() OVER (PARTITION BY N.UserID ORDER BY N.InsertDateTime DESC) as rn
FROM
@Notifications N
INNER JOIN
@Users U ON N.UserId = U.UserId
INNER JOIN
@Users AU ON N.ActionUserId = AU.UserId
INNER JOIN
@followCount C on C.userid = N.UserID) t2 WHERE rn < 3) t2
WHERE t2.UserID = N.UserID
FOR XML PATH('')), 1, 2, '') AS NotificationMsg
FROM @Notifications N
; with cte as
(SELECT N.NotificationId,
N.UserId,
N.ActionUserId,
(CASE WHEN N.NotificationTypeId = 1 THEN 1
WHEN N.NotificationTypeId = 7 THEN 3
ELSE
2
END) AS TypeId,
AU.ProfileImage,
AU.UserName,
N.NotificationTypeId,
N.InsertDateTime,
m.NotificationMsg + CASE WHEN c.cnt > 2 THEN ' + '
+ FORMAT(c.cnt - 2, '0') + ' other users' END + ' followed you.' AS NotificationText,
ROW_NUMBER() OVER (PARTITION BY N.UserID ORDER BY N.InsertDateTime DESC) as rn
FROM
@Notifications N
INNER JOIN
@Users U ON N.UserId = U.UserId
INNER JOIN
@Users AU ON N.ActionUserId = AU.UserId
INNER JOIN
@followCount C on C.userid = N.UserID
INNER JOIN @msg M ON m.userid = N.UserID)
SELECT NotificationID, UserID, ActionUserID, UserName,
NotificationTypeID, InsertDateTime, ProfileImage, NotificationText
FROM cte WHERE rn = 1
请注意,我必须为userid20添加一个条目。您提供的示例数据只生成一条记录
特别棘手的是,要求两个名字加上x个其他名字。这涉及到几个中间步骤。很可能是某个比我更精通SQL的人可以缩短这个时间。另外请注意,我只测试了这些数据。你需要检查大于或小于3个其他用户的数字,以确保一切正常。@TimBiegeleisen你能帮助我如何合并我的数据吗?@marc_s你能帮助我吗?你能帮助我吗当问这样的问题时,你应该提供:1为涉及的表创建语句,如果不是所有字段,则是所有相关字段;2用足够的测试数据填充这些表的插入语句不需要是测试结果的真实数据;和3个给定测试数据的样本期望输出。那么你就有更好的机会获得帮助。@JonathanWillcock我已经编辑了我的问题,请查看我当前的o/p和我的例外o/pSo,你已经处理了我的3个问题。我们现在只需要1和2。