我有一个名为messages
的表,其中包含发件人和收件人之间的IM记录。
这4个字段是:Date,Timestamp Senderid,Recipientid。
如何在一分钟内找到收到回复的邮件百分比?
答案 0 :(得分:0)
Declare @RespondedMessagesCount INT =
(Select Count(*) from messages m1
INNER JOIN messages m2
on m1.sender = m2.recipient
and m1.recipient = m1.sender
WHERE ABS(DateDiff(minute, m1.Date, m2.Date)) < 1),
@AllMessagesCount INT = (Select count(*) from messages)
Select Cast(@RespondedMessagesCount as float)/@AllMessagesCount * 100
答案 1 :(得分:0)
我不是专家,但这是我对MySQL的回答
架构:
CREATE TABLE messages
(`Date` varchar(25), `Timestamp` varchar(25), `Senderid` Int, `Recipientid` Int )
;
INSERT INTO messages
(`Date`, `Timestamp`, `Senderid`, `Recipientid` )
VALUES
('2017-02-27', '21:28:23', 1, 2),
('2017-02-27', '21:28:26', 2, 1),
('2017-02-27', '21:28:27', 1, 4),
('2017-02-27', '21:29:28', 4, 1),
('2017-02-27', '21:28:28', 3, 1)
;
查询:
select
100*(
SELECT count(*) FROM
(
SELECT
msg.Recipientid as Senderid,
msg.Senderid as Recipientid,
TIMESTAMPDIFF(SECOND, CONCAT(messages.Date,' ',messages.Timestamp), CONCAT(msg.Date,' ',msg.Timestamp)) as seconds
FROM messages
INNER JOIN messages msg ON messages.Recipientid=msg.Senderid
WHERE
msg.Recipientid=messages.Senderid
) as results
WHERE seconds<=60 AND seconds>0
)/ (SELECT count(*) FROM messages)
以下是工作示例fiddle