I'm running the following script.
select PlateID1, PlateID2, Max(TimeStamp) as LastVisit, Min(TimeStamp) as FirstVisit,
count(PlateID1) as VisitCount
from ActivityLog
where (TimeStamp > '9/15/2017 12:00:00 AM' and TimeStamp < '9/18/2017 12:00:00 AM')
group by PlateID1, PlateID2
having count(PlateID1) > 1
order BY VisitCount desc
Returns the following results:
PlateID1 PlateID2 LastVisit FirstVisit VisitCount
7BAY665 _blank_ 2017-09-15 19:28:20.457 2017-09-15 13:24:25.770 3621
_blank_ _blank_ 2017-09-17 15:48:48.753 2017-09-15 12:55:46.557 305
7BAY665 7BAY665 2017-09-15 19:26:26.040 2017-09-15 19:05:21.627 5
7BAY665 _blank_ 2017-09-15 19:17:03.170 2017-09-15 19:16:41.943 2
I need to omit the blanks, and combine the results for PlateID1 and PlateID2. So, every time a plate repeats in PlateID1 OR PlateID2, I need to group/count it.
The results I want, again, sorry for the poor formatting should be like this.
PlateID LastVisit FirstVisit VisitCount
7BAY665 2017-09-15 19:28:20.457 2017-09-15 13:24:25.770 3628
Based on Tab's comment, I came up with the following solution. Thanks!
SELECT t1.plateid,
Max(t1.timestamp) AS LastVisit,
Min(timestamp) AS FirstVisit,
Count(plateid) AS VisitCount
FROM ((SELECT plateid1 AS PlateID,
timestamp
FROM activitylog
WHERE ( timestamp > '9/15/2017 12:00:00 AM'
AND timestamp < '9/18/2017 12:00:00 AM' )
AND plateid1 IS NOT NULL
AND plateid1 <> '')
UNION
(SELECT plateid2 AS PlateID,
timestamp
FROM activitylog
WHERE ( timestamp > '9/15/2017 12:00:00 AM'
AND timestamp < '9/18/2017 12:00:00 AM' )
AND plateid2 IS NOT NULL
AND plateid2 <> '')) AS t1
GROUP BY t1.plateid
HAVING Count(plateid) > 1