0

关于问题......如果发生在同一日期,请选择列......我收到了这个答案

SELECT a.*
FROM LicenseHistory a
JOIN (  SELECT LicenseID, date as date, COUNT(DISTINCT IPAddress,LicenseID)
    FROM LicenseHistory 
    WHERE (LicenseID= 24965)
    GROUP BY LicenseID,Date
    /*HAVING COUNT(DISTINCT IPAddress) > 1*/
     )b
ON a.LicenseID = b.LicenseID
AND a.date = b.date
order by date desc, LicenseID desc

哪个返回了这些结果......

Date            UniqueID      Users       IPAddress      Country      Count
2013-05-14         24965       15         70.60.96.98      US          1455
2013-05-14         24965       15         72.252.247.148   US           111
2013-03-29         24965       15         184.39.241.223   US            14

计数是非常错误的,因为一个简单的选择,比如......

SELECT COUNT(Distinct IPAddress) AS DistinctIPCount
FROM LicenseHistory WHERE (LicenseID=24965)

显示计数为 123。我需要此 SQL 来计算该 ID 在当天从该 IP 地址出现的次数。换句话说“ 24965 在 2013-05-14 使用 IP 70.60.96.98 出现了多少次

4

1 回答 1

0

如果要计算给定 ID、日期和 IP 的行数,则内部查询中有错误,所有这些列都必须在 GROUP BY 语句中。试试这个查询:

SELECT *
FROM LicenseHistory a
JOIN (  SELECT LicenseID, date as date, IPAddress, COUNT(*)
    FROM LicenseHistory 
    WHERE (LicenseID= 24965)
    GROUP BY LicenseID,Date,IPAddress
     )b
ON a.LicenseID = b.LicenseID
AND a.date = b.date
order by date desc, LicenseID desc
于 2013-06-04T08:02:43.007 回答