Reputation: 859
I'm trying to write a query which will pair up impressions and conversions from the same IP address in my database. I have a table of impressions and their IP addresses, and an impression can be of the type 'view' or 'conversion' (defined in the column 'name').
So basically, I need to identify groups of records with the same IP address, which contain both a view and a conversion.
After an hour of Googling I've got as far as the below, which isn't very far but should give an idea of the objects involved:
SELECT ip_address, name, COUNT(1) as CNT
FROM Impressions
GROUP BY ip_address, name;
Can anyone advise on the best way to do this?
Upvotes: 0
Views: 65
Reputation: 6236
You can try this:
SELECT
i.ip_address AS ip,
GROUP_CONCAT(DISTINCT CAST(i.name AS CHAR)) AS nameList,
SUM(IF(i.name = 'View', 1, 0)) AS viewCount,
SUM(IF(i.name = 'Conversion', 1, 0)) AS conversionCount
FROM Impressions i
GROUP BY i.ip_address;
You will get a comma delimited list of names containing both 'view' & 'conversion' and their separate counts.
Upvotes: 0
Reputation: 863
Please try...
SELECT ip_address, name, COUNT(1) cnt as CNT FROM Impressions GROUP BY name,IP_address Having count(IP_address) > 1;
Upvotes: -1
Reputation: 69819
You need to use the HAVING
clause with a conditional count. You also need to remove name
from the GROUP BY
as this will treat your two different types separately.
SELECT ip_address,
COUNT(CASE WHEN Name = 'View' THEN 1 END) AS Views,
COUNT(CASE WHEN Name = 'Conversion' THEN 1 END) AS Conversions,
COUNT(1) as CNT
FROM Impressions
GROUP BY ip_address
HAVING COUNT(CASE WHEN Name = 'View' THEN 1 END) > 0
AND COUNT(CASE WHEN Name = 'Conversion' THEN 1 END) > 0;
Upvotes: 3