Advertisement
StoyanGrigorov

14

Feb 11th, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.74 KB | None | 0 0
  1. ----Section 3: Querying - 14. Last Chat
  2.  
  3. DECLARE @LastChatId INT = (SELECT TOP (1) c.Id FROM Chats AS c
  4.                             ORDER BY c.StartDate DESC)
  5.  
  6. DECLARE @FirstMessage VARCHAR(200) =
  7.                                     (
  8.                                         SELECT TOP (1) m.Content
  9.                                         FROM Messages AS m
  10.                                         WHERE m.ChatId = @LastChatId
  11.                                         ORDER BY m.SentOn
  12.                                     )
  13.  
  14.  
  15. SELECT c.Title, @FirstMessage AS [Content]
  16. FROM Chats AS c
  17. WHERE c.Id = @LastChatId
  18. --2/4 IN JUDGE
  19.  
  20.  
  21.  
  22. SELECT TOP (1) c.Title, m.Content
  23. FROM Chats AS c
  24. LEFT JOIN Messages AS m
  25. ON m.ChatId = c.Id
  26. ORDER BY c.StartDate DESC
  27. --2/4 IN JUDGE
  28.  
  29.  
  30.  
  31. SELECT c.Title , m.Content
  32. FROM Chats c
  33. LEFT JOIN Messages m ON m.ChatId = c.Id
  34. WHERE c.StartDate = (SELECT MAX(StartDate) FROM Chats)
  35. --4/4 IN JUDGE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement