一天中多少次

时间:2013-06-03 22:15:34

标签: sql date unique

有问题...... select columns if happened on same date ...我收到了这个答案

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地址出现的次数。换句话说“在2013-05-14使用IP 70.60.96.98 出现24965次多少次”

1 个答案:

答案 0 :(得分: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