0

We have a backend table that stores details of transaction including seconds since epoch. I am creating a UI where I collect from-to dates to display counts of transaction occurred in-between the dates.

Assuming that the date range is from 07/01/2012 - 07/30/2012, I am unable to establish a logic that will increment a counter for records that happened within the time period. I should hit the DB only once as hitting for each day will give poor performance.

I am stuck at a logic:

Convert 07/01/2012 & 07/30/2012 to seconds since epoch.
Get the records for start date - end date [as converted to seconds since epoch] 
   For each record get the month / date 
     -- now how will we add counters for each date in between 07/01/2012 - 07/30/2012 
3
  • Assign each date range a key in a dictionary, +1 to the value each time you find an item within that range? Commented Jul 27, 2012 at 9:31
  • @cdarke: you can do it in MySQL altogether.. Commented Jul 27, 2012 at 9:32
  • @Prakash: sorry, I don't know (looks like you have the answer anyway) Commented Jul 27, 2012 at 9:47

1 Answer 1

1

MySQL has the function FROM_UNIXTIME which will convert your seconds since epoch into datetime and you can then extract the DATE part of it (YYYY-MM-DD format) and group according to it.

SELECT DATE(FROM_UNIXTIME(timestamp_column)), COUNT(*)
FROM table_name
GROUP BY DATE(FROM_UNIXTIME(timestamp_column))

This will return something like

2012-07-01  2
2012-07-03  4
…

(no entries for days without transactions)

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot but one question - if in case for a date there are no records I should get something like : 2012-07-04 0 also. Thanks its a great help
Perfect :-) You already had my upvote, I saw no sense in scooping you on that small issue alone..
@Prakash: That'll complicate things enormously. Use python logic for that part (parse the whole thing into a dictionary, for example, then handle missing dates via get(date, 0)).
@Prakash - you will need some datetime logic there (which knows there is 2012-02-29 but no 2012-02-30, and so on). You can build it in Python with dateutil.rrule, which is simpler than doing the same thing in MySQL.
@eumiro: A simple date += timedelta(days=1) loop should suffice should it not?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.