Calculate average days between events?

Kaitlyn2004

If I have a table that includes:

user_id | event_time

How can I calculate the average days between events? To get something like:

days_diff | count
        1 | 100
        2 | 90
        3 | 20

A user may have 1 day between events, but may also have 3 days between to subsequent events. How can I count them in both buckets?

Sample data (note in this case the DAY DIFF is 0/1 but this is just a small subset of data)

user_id | event_time
82770 2015-05-04 02:34:53
1 2015-05-04 08:45:53
82770 2015-05-04 20:38:24
82770 2015-05-04 20:38:24
82770 2015-05-04 20:38:24
1 2015-05-05 09:31:42
82770 2015-05-05 13:33:36
82770 2015-05-05 13:33:53
1 2015-05-06 09:53:59
1 2015-05-06 23:31:18
1 2015-05-06 23:31:35
1 2015-05-07 12:31:41
82770 2015-05-07 16:01:16
Zsuzsa

Here's a solution without using a temporary table:

select daybetweenevents as days_diff,
   count(daybetweenevents) as count
from (select t1.user_id,
    t1.event_time,
    datediff(day, t1.event_time, min(t2.event_time)) as daybetweenevents
    from yourtable t1
    inner join yourtable t2
       on t1.user_id = t2.user_id
    and t1.event_time < t2.event_time
    group by t1.user_id, t1.event_time) temp
group by daybetweenevents

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Calculate average of values between 2 columns sql

분류에서Dev

Calculate closest days count, between each row

분류에서Dev

Trying to calculate the average in an ArrayList

분류에서Dev

Calculate average with xslt

분류에서Dev

Calculate month with days in javascript

분류에서Dev

List of days with events that occur on that day

분류에서Dev

How to calculate an average value based on duplicate groups?

분류에서Dev

Method to calculate the average without storing individual values

분류에서Dev

Elapsed days between two dates always giving one days difference

분류에서Dev

Distinguish between arguments and react events

분류에서Dev

Find Average Interval between Dates in a Pivot Table

분류에서Dev

SQL - difference and average between more records

분류에서Dev

Calculate average conditionally based on an attribute in a rake task (Rails)

분류에서Dev

Calculate average values for rows with different ids in MS Excel

분류에서Dev

Calculating the days between two given dates

분류에서Dev

How to count fractional days between 2 dates

분류에서Dev

extracting days between a dataframe date and today

분류에서Dev

How to set class between two days to datepicker?

분류에서Dev

Calculate Number of Days under Contract from List of Contracts

분류에서Dev

Calculate average for a sequence of strings, then remove anything greater than 2SD of the average in R

분류에서Dev

Jquery calculate height difference between .ready and .resize

분류에서Dev

How to calculate the Euclidean distance between vectors?

분류에서Dev

how to receive all days between 2 dates in laravel

분류에서Dev

Number of working days between 2 dates according JIRA's configuration

분류에서Dev

How to find data between two days BUT excluding the data in the weekends

분류에서Dev

How many days between two dates are in a certain month?

분류에서Dev

SQL to get number of working days between two dates

분류에서Dev

How to calculate 'single' average/mean value of pixels in a set ROI using cvAvg or mean?

분류에서Dev

MongoDB: calculate average value for the document & then do the same thing across entire collection

Related 관련 기사

  1. 1

    Calculate average of values between 2 columns sql

  2. 2

    Calculate closest days count, between each row

  3. 3

    Trying to calculate the average in an ArrayList

  4. 4

    Calculate average with xslt

  5. 5

    Calculate month with days in javascript

  6. 6

    List of days with events that occur on that day

  7. 7

    How to calculate an average value based on duplicate groups?

  8. 8

    Method to calculate the average without storing individual values

  9. 9

    Elapsed days between two dates always giving one days difference

  10. 10

    Distinguish between arguments and react events

  11. 11

    Find Average Interval between Dates in a Pivot Table

  12. 12

    SQL - difference and average between more records

  13. 13

    Calculate average conditionally based on an attribute in a rake task (Rails)

  14. 14

    Calculate average values for rows with different ids in MS Excel

  15. 15

    Calculating the days between two given dates

  16. 16

    How to count fractional days between 2 dates

  17. 17

    extracting days between a dataframe date and today

  18. 18

    How to set class between two days to datepicker?

  19. 19

    Calculate Number of Days under Contract from List of Contracts

  20. 20

    Calculate average for a sequence of strings, then remove anything greater than 2SD of the average in R

  21. 21

    Jquery calculate height difference between .ready and .resize

  22. 22

    How to calculate the Euclidean distance between vectors?

  23. 23

    how to receive all days between 2 dates in laravel

  24. 24

    Number of working days between 2 dates according JIRA's configuration

  25. 25

    How to find data between two days BUT excluding the data in the weekends

  26. 26

    How many days between two dates are in a certain month?

  27. 27

    SQL to get number of working days between two dates

  28. 28

    How to calculate 'single' average/mean value of pixels in a set ROI using cvAvg or mean?

  29. 29

    MongoDB: calculate average value for the document & then do the same thing across entire collection

뜨겁다태그

보관