How to calculate the number of valid replies within one day? a reply table has a field Author ID, posting Time: 1108-01-0115:001208-01-0116:001308-01-0117:001208-01-0118:001208-01-0216:00
There is a reply table
Fields
Author ID, posting time
11 08-01-01
12 08-01-01 16: 00
13 08-01-01 17: 00
12 08-01-01
12 08-01-02 16: 00
I want to count valid replies now
A reply from the same person within 24 hours is a valid reply.
Calculate the number of replies
11 (once)
12 (2 times), one of which was rejected within 24 hours.
13 (once)
Is there any better algorithm available? I first saved it to an array and compared it by constantly traversing the array, which is too cumbersome.
Thank you, prawns.
------ Solution --------------------
Once every 24 hours, you have set it to once. What else do you want to count ???
Either use time to do subtraction (order by posting time, use TOP2 to get the last two values for a ratio, it will be done, nothing to do with traversal), or lock the 24-hour ID with a reply
------ Solution --------------------
SQL code
SELECT *, count (DISTINCT (first 8 digits of the posting time) AS times FROM table group by id
------ Solution --------------------
SELECT author id, posting time
FROM table
WHERE (NOT EXISTS
(SELECT author id
FROM table B
WHERE B. author id = a. Author id AND B. Posting time> a. Posting time ))
Order by author id, posting time DESC
------ Solution --------------------
SQL code
Declare @ t table ([author ID] int, [posting time] datetime) insert into @ t select 11, '08-01-01 'union select 12, '08-01-01 'union select 13, '08-01-01' union select 12, '08-01-01 'union select 12, '08-01-02 'select id, count (*) as [posting times] from (select [author ID] as id from @ t group by [author ID], CONVERT (varchar (10), [posting time], 120) T group by T. id
------ Solution --------------------
Select count (ID) as cnt, ID from table where str_date = "xxxx-xx" group by ID order by cnt
------ Solution --------------------
SELECT author id, posting time
FROM table
WHERE (NOT EXISTS
(SELECT author id
FROM table B
WHERE B. author id = a. Author id AND B. Posting time> a. Posting time ))
Order by author id, posting time DESC