I am not sure if you can do a direct database query to find that, but if you can dump the ResultSet to a file sorted by timestamp, it should be easy to figure it out.
Think of it this way. Assume that a song is a character and the timestamp sorted list of songs is a String. E.g. you have a songlist represented by the following where A B and C are unique songs:
ABCACBABC (Ignore ads for now)
Now you can break this into subsequences of adjacent two characters (the are called bigrams).
The bigrams you get are:
AB, BC, CA, AC, CB, BA, BC
Now you can clearly see that the bigram BC is repeated. To do this programmatically, you can throw every bigram into a HashMap (or Hashtable/HashSet) and query every new bigram against the HashMap to see if it contains it. If the map already contains it, then it is a repeat. If not, then it is a new one, so put it into the map.
At the end of this exercise, you will know which combinations are repeated.
Now coming to the case where ads are present, lets call all ad as X. Consider the sequence
ABCXABCXXABCABC
Wherever you have more than one ad successively, (replace that with a hard line break (you are dividing the string into multiple strings). You will get
ABCXABC
<XX
- is replaced by a new line>
ABCAB
Now replace all single ads by empty strings. You will get
ABCABC
ABCAB
Now process each String independently to identify repetitions.
Hope it works.