[英]SQL query to identify pairs with duplicates
I'm trying to write a query which will pair up impressions and conversions from the same IP address in my database. 我正在尝试编写一个查询,该查询将对来自数据库中相同IP地址的展示和转化进行配对。 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'). 我有一张印象及其IP地址的表格,印象可以是“视图”或“转换”类型(在“名称”列中定义)。
So basically, I need to identify groups of records with the same IP address, which contain both a view and a conversion. 因此,基本上,我需要标识具有相同IP地址的记录组,其中既包含视图又包含转换。
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? 谁能建议最好的方法吗?
You need to use the HAVING
clause with a conditional count. 您需要将HAVING
子句与条件计数一起使用。 You also need to remove name
from the GROUP BY
as this will treat your two different types separately. 您还需要从GROUP BY
删除name
,因为这将分别处理您的两种不同类型。
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;
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. 您将得到一个用逗号分隔的名称列表,其中包含“视图”和“转换”及其单独的计数。
Please try... 请试试...
SELECT ip_address, name, COUNT(1) cnt as CNT FROM Impressions GROUP BY name,IP_address Having count(IP_address) > 1; 选择ip_address,名称,COUNT(1)cnt作为CNT FROM Impressions GROUP BY名称,IP_address的count(IP_address)> 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.