How to calculate valid replies within one day

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.