2

I have the following table:

+------------+-------+------------+-----------+
| date       | time  | custom_id  | Number    |
+------------+-------+------------+-----------+
| 01/17/2014 | 00:00 | ABC123     | 15        |
| 01/17/2014 | 00:00 | HEY567     | 25        |
| 01/17/2014 | 00:30 | ABC123     | 17        |
| 01/17/2014 | 00:30 | HEY567     | 28        |
| 01/17/2014 | 01:00 | ABC123     | 17        |
| 01/17/2014 | 01:00 | HEY567     | 28        |
| 01/17/2014 | 01:30 | ABC123     | 17        |
| 01/17/2014 | 01:30 | HEY567     | 28        |
+------------+-------+------------+-----------+

How can I sum 2 rows (Number) with the same CUSTOM_ID & DATE and when the given time is only :30 minutes of difference round it to :00 and delete the :30? It should look like this in the end:

+------------+-------+------------+-----------+
| date       | time  | custom_id  | Number    |
+------------+-------+------------+-----------+
| 01/17/2014 | 00:00 | ABC123     | 32        |
| 01/17/2014 | 00:00 | HEY567     | 53        |
| 01/17/2014 | 01:00 | ABC123     | 34        |
| 01/17/2014 | 01:00 | HEY567     | 56        |
+------------+-------+------------+-----------+

How can I accomplish this with PHP or possibly with a MySQL query?

SQLFiddle: http://sqlfiddle.com/#!2/cc529/1

6
  • Can you provide sqlfiddle, so we could test the solution? Commented Feb 9, 2014 at 18:50
  • Sure I will create it right now Commented Feb 9, 2014 at 18:55
  • Here is the SQLFiddle: sqlfiddle.com/#!2/cb8efb/1 Commented Feb 9, 2014 at 19:08
  • 1
    it would be better if you put different dates into the example Commented Feb 9, 2014 at 19:14
  • 1
    why date is varchar and not date? And time is also varchar and not time? Commented Feb 9, 2014 at 19:17

1 Answer 1

1

This should work

SELECT date, MIN(time) as time, custom_id, SUM(Number) as Number
FROM table
GROUP BY date, custom_id, HOUR(time)
Sign up to request clarification or add additional context in comments.

1 Comment

WOW. I'm amazed how simple it is, THANKS ALOT you just saved my job! here's the final result if someone else needs it: sqlfiddle.com/#!2/913b4/1

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.