I have a sessions table in my SQL2000 database which lists the current logged on sessions. I have found that some users have logged on twice, some legitimately and possibly others who have not, the cut down table would look like this:
Fred.....2010-11-12 09:05:01.200
Bob......2010-11-12 10:05:01.200
Fred.....2010-11-12 08:11:01.200
As you can see Fred's account is logged on twice, I would like to extract the user's name and log on time (second column) using a query from the table that is the earliest date or time in this example:
Fred.....2010-11-12 08:11:01.200
But I do not want the records where the user is only logged in once. I think that makes sense but please write back if you need clarification.
smallest date query
-
- 5StarLounger
- Posts: 847
- Joined: 24 Jan 2010, 06:35
- Location: A Magic Forest in Deepest, Darkest, Kent
smallest date query
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it
-
- Administrator
- Posts: 78483
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: smallest date query
Let's say the table is named tblLogon with fields UserName and Logon. Create a totals query like this:
SELECT UserName, Min(Logon) AS EarliestLogon
FROM tblLogon
GROUP BY UserName
HAVING Count(UserName)>1
SELECT UserName, Min(Logon) AS EarliestLogon
FROM tblLogon
GROUP BY UserName
HAVING Count(UserName)>1
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 847
- Joined: 24 Jan 2010, 06:35
- Location: A Magic Forest in Deepest, Darkest, Kent
Re: smallest date query
Thank you
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it
-
- 5StarLounger
- Posts: 847
- Joined: 24 Jan 2010, 06:35
- Location: A Magic Forest in Deepest, Darkest, Kent
Re: smallest date query
Sorry as an addition to this requirement, how would I delete these particular records as I can't seem to get the syntax right?
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it
-
- Administrator
- Posts: 78483
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: smallest date query
What if someone has logged in 3 times - do you want to delete only the earliest time, or all but the most recent one?
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 847
- Joined: 24 Jan 2010, 06:35
- Location: A Magic Forest in Deepest, Darkest, Kent
Re: smallest date query
Thanks for getting back as I have just found that problem exists, I think it best to say delete all but the most recent one.
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it
-
- Administrator
- Posts: 78483
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: smallest date query
With the same names as before:
DELETE Logon
FROM tblLogon
WHERE Logon<(SELECT Max(t.Logon) FROM tblLogon AS t WHERE t.UserName=tblLogon.UserName)
The alias t is required to distinguish between the username in the subquery and that from the main query.
DELETE Logon
FROM tblLogon
WHERE Logon<(SELECT Max(t.Logon) FROM tblLogon AS t WHERE t.UserName=tblLogon.UserName)
The alias t is required to distinguish between the username in the subquery and that from the main query.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 847
- Joined: 24 Jan 2010, 06:35
- Location: A Magic Forest in Deepest, Darkest, Kent
Re: smallest date query
That's the baby, thanks
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it