2017-06-22 33 views
0

嗨,我爲數據創建一個存儲過程,併發送推送notification.and我想這些數據marge並獲得一個原始。但我怎麼能這樣做,我不知道。我需要幫助,請幫助我,讓我知道該怎麼做that.this是我在這裏查詢下面我有寫:如何使用sql server從表格中查找數據?

這是查詢=>

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 

我想喜歡這種瑪吉這個數據的任何一個知道如何能做到這一點認罪讓我知道。

+0

您的輸出結果與您的查詢不匹配... – iamdave

+0

我知道NotificationText列未在查詢中提及,但它是我的預期o/p – coderwill

+0

Where Insert DateTime'來自例如? – iamdave

回答

1

你可以用派生表和一些窗口函數來做到這一點。我曾經也有點邏輯的補充,以確保Notification Text有正確的英文取決於這些用戶的數字包括:

-- 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 | 
+----------------+--------+--------------+----------+--------------------+-------------------------+------------------------+-----------------------------------------+ 
+0

條件或與任何userid在哪裏條件,那麼它給更多的用戶? – coderwill

+0

你好,你可以請給我暗示與條件我需要2用戶爲用戶ID 10和用戶ID 20所以請給我提示,並幫助我 – coderwill

+0

@coderwill我已經更新了我的答案。請注意,堆棧溢出不是免費的代碼寫入服務,我們不爲你工作。請不要繼續追蹤更新。如果我們要幫助你,我們將在我們自己的時間內這樣做。 – iamdave

0

你可以試試這個:

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 
相關問題