Sql server 如何使用sql server为表中的数据加边距?
您好,我正在为获取数据和发送推送通知创建一个存储过程。我想对这些数据进行标记并将其放入一个原始数据。但我不知道怎么能做到。我需要帮助请帮助我让我知道如何做到这一点。这是我的问题,我在这里写下: 这是查询=>Sql server 如何使用sql server为表中的数据加边距?,sql-server,tsql,Sql Server,Tsql,您好,我正在为获取数据和发送推送通知创建一个存储过程。我想对这些数据进行标记并将其放入一个原始数据。但我不知道怎么能做到。我需要帮助请帮助我让我知道如何做到这一点。这是我的问题,我在这里写下: 这是查询=> SELECT N.NotificationId, N.UserId, N.ActionUserId, (CASE WHEN N.NotificationTypeId = 1 THEN 1 WHEN N.NotificationTypeId = 7
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
这是我预期的o/p=>
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
我想喜欢这个marge这个数据任何人都知道怎么做请告诉我。你可以用一个派生表和一些窗口函数来做这件事。我还添加了一些逻辑,以确保通知文本具有正确的英语,具体取决于包含的其他用户的数量:
-- Create test data
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');
-- Specify UserID
declare @UserID int = 10;
-- Create Notification
with d as
(
select n.NotificationID
,n.UserID
,n.ActionUserID
,au.UserName
,n.NotificationTypeID
,n.InsertDateTime
,au.ProfileImage
,row_number() over (partition by n.UserID order by n.InsertDateTime desc) as rn
,count(*) over (partition by n.UserID) as c
from @Notifications n
join @Users au
on(n.ActionUserID = au.UserID)
)
select d.NotificationID
,d.UserID
,d.ActionUserID
,d.UserName
,d.NotificationTypeID
,d.InsertDateTime
,d.ProfileImage
,d.UserName
+ isnull(case when d2.c = 2
then ' and '
else ', '
end
+ d2.UserName
,'')
+ case when d2.c > 2
then ' and ' + cast(d2.c-2 as nvarchar(10)) + ' other users'
else ''
end
+ ' followed you' as NotificationText
from d
left join d as d2
on(d.UserID = d2.UserID
and d2.rn = 2
)
where d.rn = 1;
输出:
+----------------+--------+--------------+----------+--------------------+-------------------------+------------------------+-----------------------------------------+
| NotificationID | UserID | ActionUserID | UserName | NotificationTypeID | InsertDateTime | ProfileImage | NotificationText |
+----------------+--------+--------------+----------+--------------------+-------------------------+------------------------+-----------------------------------------+
| 3 | 10 | 11 | abc | 1 | 2017-06-22 16:14:16.803 | 20170416032403867.jpeg | abc, xyz and 1 other users followed you |
| 6 | 20 | 15 | hbc | 1 | 2017-06-22 17:14:16.803 | 20170416032403869.jpeg | hbc, tyu and 1 other users followed you |
+----------------+--------+--------------+----------+--------------------+-------------------------+------------------------+-----------------------------------------+
您可以尝试以下方法:
SELECT Q.NotificationId,
Q.UserId,
Q.ActionUserId,
(CASE WHEN Q.NotificationTypeId = 1 THEN 1
WHEN Q.NotificationTypeId = 7 THEN 3
ELSE
2
END) AS TypeId,
Q.ProfileImage,
Q.UserName,
Q.IsRead,
(CASE WHEN Q.NotificationTypeId = 1 THEN 1
WHEN Q.NotificationTypeId = 7 THEN 3
ELSE
2
END) AS TypeId2,
Q.NotificationTypeId,
Q.InsertDateTime
FROM ( SELECT N.UserId, N.ActionUserId, N.NotificationTypeId,
AU.ProfileImage, AU.UserName, N.IsRead, N.InsertDateTime, N.NotificationID
FROM Notifications N
INNER JOIN Users U ON N.UserId = U.UserId
INNER JOIN Users AU ON N.ActionUserId = AU.UserId
WHERE N.UserId = @UserId ) Q
INNER JOIN (SELECT MAX(NotificationID) AS MaxNotifID, UserID FROM
dbo.Notifications
WHERE UserID = @userID GROUP BY UserID ) R ON
Q.NotificationID = R.MaxNotifID AND Q.UserID = R.USerID
ORDER BY Q.InsertDateTime DESC
您的输出与您的查询不匹配…我知道查询中没有提到NotificationText列,但这是我期望的o/P例如InsertDateTime从何而来?@iamdave现在它是正确的,我添加了日期列。您能帮我做些什么吗that@iamdave如何做到这一点任何想法请让我知道或在没有给出任何用户ID的情况下哪里的条件,然后它是给更多的用户?你好,你能给我提示,在没有条件的情况下,我需要2个用户的用户id 10和用户id 20,所以请给我提示和帮助me@coderwill我已经更新了我的答案。请注意,堆栈溢出不是免费的代码编写服务,我们不为您工作。请不要继续追逐更新。是的,我知道这件事,为此非常抱歉,非常感谢你的帮助