M
M
Max2015-07-16 11:52:12
SQL
Max, 2015-07-16 11:52:12

How to group?

There is a table like [Date],[Time],[Name],[IP], which registers user logins on the PC.
For 1 day, a user can log in several times from different PCs. The task is to pull out only the first entries from the list.
With Toaster, there is already a script that pulls this set, but without [IP]

select dateadd(day, datediff(day, 0, CONVERT(datetime, LEFT(CONVERT(VARCHAR, [Date]+' '+[Time], 103), 19))), 0) [Date], convert (char(8), min(CONVERT(datetime, LEFT(CONVERT(VARCHAR, [Date]+' '+[Time], 103), 19))), 108) [Time], 
[Name]
from [OVB].[dbo].[Logon]
group by dateadd(day, datediff(day, 0, CONVERT(datetime, LEFT(CONVERT(VARCHAR, [Date]+' '+[Time], 103), 19))), 0), [Name]
order by [Name],[Date]

So far, without [IP] - everything is cool, only the first inputs are in the sample. As soon as I add [IP] to the request
select dateadd(day, datediff(day, 0, CONVERT(datetime, LEFT(CONVERT(VARCHAR, [Date]+' '+[Time], 103), 19))), 0) [Date], convert (char(8), min(CONVERT(datetime, LEFT(CONVERT(VARCHAR, [Date]+' '+[Time], 103), 19))), 108) [Time], [Name], [IP]
from [OVB].[dbo].[Logon]
group by dateadd(day, datediff(day, 0, CONVERT(datetime, LEFT(CONVERT(VARCHAR, [Date]+' '+[Time], 103), 19))), 0), [Name], [IP]
order by [Name],[Date]

- in the sample, the first input is obtained for each person for each [IP]. How to select only the first entry in the day, ignoring [IP]? Intuitively I understand that it should not be in a grouping

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Lastochkin, 2015-07-16
@MaxDukov

SELECT * 
FROM Logon
JOIN 
(
SELECT [Date] D, MIN([Time]) T
FROM Logon
GROUP BY [Date]
) X
ON [Date]=D AND [Time]=T

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question