I am working with visitor log data and need to summarize it by IP address. The data looks like this:
id | ip_address | type | message | ... ----------+----------------+----------+---------------- 1 | 1.2.3.4 | purchase | ... 2 | 1.2.3.4 | visit | ... 3 | 3.3.3.3 | visit | ... 4 | 3.3.3.3 | purchase | ... 5 | 4.4.4.4 | visit | ... 6 | 4.4.4.4 | visit | ...
And should summarize with:
type="purchase" DESC, type="visit" DESC, id DESC
The yield:
chosenid | ip_address | type | message | ... ----------+----------------+----------+---------------- 1 | 1.2.3.4 | purchase | ... 4 | 3.3.3.3 | purchase | ... 6 | 4.4.4.4 | visit | ...
Is there an elegant way to get this data?
An ugly approach follows:
set @row_num = 0; CREATE TEMPORARY TABLE IF NOT EXISTS tt AS SELECT *,@row_num:=@row_num+1 as row_index FROM log ORDER BY type="purchase" DESC, type="visit" DESC, id DESC ORDER BY rating desc;
Then get the minimum row_index and id for each ip_address (https://stackoverflow.com/questions/121387/fetch-the-row-which-has-the-max-value-for-a-column)
Then join those id's back to the original table