dimanche 21 décembre 2014

Group MySQL result only if next row has the same key

I'm trying to mimic the way Apple's iOS is sorting the call history on the Phone App.


The way they're sorting the calls is as the following:



(original list)
—————————————————————
John - (Incoming) - 10.30 am
John - (Missing) - 10.00 am
John - (Outgoing) (3) - 09.00 am
John - (Outgoing) - Wednesday

(An incoming call is done)
—————————————————————
John - (Incoming) (2) - 10.30 am <-- this row gets grouped (2)
John - (Missing) - 10.00 am
John - (Outgoing) (3) - 09.00 am
John - (Outgoing) - Wednesday

(A missed call came)
—————————————————————
John - (Missing) - 10.35 am <-- this row gets added (but not grouped with (*))
John - (Incoming) (2) - 10.30 am
John - (Missing) - 10.00 am --> (*)
John - (Outgoing) (3) - 09.00 am
John - (Outgoing) - Wednesday


Basically, the thing this App is doing is the following:


It gets all the calls from the database, if two or more calls are equals in type (missing,incoming or outgoing), and also are the next row, they get grouped into one row and a numeric indicator shows the grouped amount, otherwise, the row is simply added to the table view without any strange action.


The thing I'm trying to do is that grouping by type when next row is same type as previous row. I know how to do it with code, but I want to know if there is any way to do it via ONLY SQL.


Thanks guys.


Aucun commentaire:

Enregistrer un commentaire