问题
I am using SQL Server 2008 R2. I have a table called Messages where I store user messages each user send to other users. The table structure is like below.
+--------+----------+-----------------+------------+
| Sender | Receiver | Message | Date |
+--------+----------+-----------------+------------+
| John | Dennis | How are you | 2015-06-06 |
| John | Dennis | Hi | 2015-06-05 |
| Tom | John | How much is it? | 2015-06-04 |
| Tom | John | Did you buy it? | 2015-06-03 |
| Robin | Tom | Hey man | 2015-06-03 |
| Dennis | John | What up | 2015-06-02 |
| John | Tom | Call me | 2015-06-01 |
+--------+----------+-----------------+------------+
I want to get the newest message and other participants' name for a selected user for each conversation. For a example there are three conversations. One is between "john-Dennis" and 2nd one is "John-Tom"and 3rd one between "Robin-Tom".
If I want to get conversations for user john, I want to get the latest conversation message with the name of other user who is in the conversation.
The expected result for the above scenario should be like this.
+-------------+-----------------+------------+
| Participant | Message | Date |
+-------------+-----------------+------------+
| Dennis | How are you | 2015-06-06 |
| Tom | How much is it? | 2015-06-04 |
+-------------+-----------------+------------+
How to achieve this using a SQL query in SQL Server. I am struggling with part for days. Please help. Thanks in advance.
回答1:
It's possible to compress this a bit, but I've split it into simple steps to hopefully make it a little easier to follow.
-- Sample data from the question.
declare @msg table (Sender varchar(32), Receiver varchar(32), [Message] varchar(max), [Date] date);
insert @msg
(Sender, Receiver, [Message], [Date])
values
('John','Dennis', 'How are you', '2015-06-06'),
('Dennis', 'John', 'Hi', '2015-06-05'),
('Tom', 'John', 'How much is it?', '2015-06-04'),
('Tom', 'John', 'Did you buy it?', '2015-06-03'),
('Robin', 'Tom', 'Hey man', '2015-06-03'),
('Dennis', 'John', 'What up', '2015-06-02'),
('John', 'Tom', 'Call me', '2015-06-01');
-- The name of the user whose conversations you want to find.
declare @UserName varchar(32) = 'John';
-- Step 1: Create columns [Participant1] and [Participant2] that will be the same for
-- each pair of users regardless of who's the sender and who the receiver.
with NameOrderCTE as
(
select
Participant1 = case when Sender < Receiver then Sender else Receiver end,
Participant2 = case when Sender < Receiver then Receiver else Sender end,
*
from
@msg
),
-- Step 2: For each distinct pair of participants, create a [Sequence] number that
-- puts the messages in reverse chronological order.
MessageSequenceCTE as
(
select
*,
[Sequence] = row_number() over (partition by Participant1, Participant2 order by [Date] desc)
from
NameOrderCTE
)
-- Step 3: Get the most recent ([Sequence] = 1) messages for each conversation
-- involving the target user.
select
Participant = case @UserName when Sender then Receiver else Sender end,
[Message],
[Date]
from
MessageSequenceCTE
where
@UserName in (Sender, Receiver) and
[Sequence] = 1;
回答2:
I think it should give you what you need :
SELECT a.Sender, a.Receiver, a.Message, a.[Date]
FROM
(
SELECT m.* , ROW_NUMBER() OVER (PARTITION BY
CASE
WHEN m.Sender = 'John' THEN 1
ELSE 2
END
ORDER BY [Date] DESC) AS rn
FROM message m
WHERE m.Sender = 'John' or m.Receiver ='John'
)a WHERE rn= 1
回答3:
Try this query
select a.Reciver as Participant,a.message,a.Date from message a
join(select sender,Receiver,max(date) from message where Sender = 'John' group by sender,Receiver) b
on a.sender=b.sender and a.Receiver=b.Receiver and a.Date=b.date
回答4:
Base on my understanding, this do the job. Although it isn't the most performing solution because of tempDb access.
DECLARE @SelectedUser NVARCHAR(100) = 'John'
SELECT TOP 1 Participant = Receiver,
[Message],
[Date]
INTO #tmp
FROM [Messages]
WHERE Sender = @SelectedUser
ORDER BY [Date]
SELECT *
FROM #tmp
UNION ALL
SELECT TOP 1 Participant = Sender,
[Message],
[Date]
FROM [Messages]
WHERE Receiver = @SelectedUser
ORDER BY [Date]
来源:https://stackoverflow.com/questions/31146053/group-result-by-matching-two-columns-in-sql-server