Hi @RXR ,
You could also refer below with least modification based on your query.
with visits as (
SELECT distinct VisitorID, CONVERT(date, Datetime) VDate, ROW_NUMBER() OVER (
PARTITION BY VisitorID
ORDER BY CONVERT(date, Datetime) desc
) as rownum
FROM Sample2
Group by VisitorID,CONVERT(date, Datetime)
)
SELECT distinct VisitorID,
VDate,
CASE
When rownum = 1 Then 'LastVisit'
WHEN rownum = 2 THEN 'SecondLast'
When rownum = 3 Then 'ThirdLast'
When rownum = 4 Then 'FourthLast'
ELSE 'Unknown' END as Which_Visit
FROM visits
WHERE rownum = 4 or rownum = 3 or rownum = 2 or rownum = 1
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.