Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort out some rows with a query

Status
Not open for further replies.

lechuck

Programmer
Sep 4, 2000
26
SE
I need some help whith an sql query.

the query "select date, user, location, ip from myTable"
returns this
Code:
date                     user    location    ip
-----------------------------------------------------
2003-11-08 23:32:12.000  bob     Uptime      1.2.3.4
2003-11-08 23:31:52.000  guest   Forum 	     1.2.3.99
2003-11-08 23:31:49.000  bob     Forum 	     1.2.3.4
2003-11-08 23:25:23.000  john    Guestbook   1.2.3.10
2003-11-08 23:25:20.000  guest   Guestbook   1.2.3.99
2003-11-08 23:25:17.000  john    Forum 	     1.2.3.10
2003-11-08 23:25:07.000  john    Uptime      1.2.3.10
2003-11-08 23:23:12.000  guest   Forum 	     1.2.3.88
2003-11-08 23:24:52.000  bob     Forum       1.2.3.4

Now I want a query that filter out the last place an user where (location).
So the result whould be like this:
Code:
date                     user    location    ip
-----------------------------------------------------
2003-11-08 23:32:12.000  bob     Uptime      1.2.3.4
2003-11-08 23:31:52.000  guest   Forum 	     1.2.3.99
2003-11-08 23:25:23.000  john    Guestbook   1.2.3.10
2003-11-08 23:23:12.000  guest   Forum 	     1.2.3.88

Any idea of query to fix this?
Thanks
 
Haven't tried this but it should be something like::

Select max(date), user, location, ip from mytable
group by user

Mark

The key to immortality is to make a big impression in this life!!
 
select date, user, location, ip from myTable t
where date = (select max(t2.date) from myTable t2 where t.location = t2.location)


======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Try this:

select * from mytable M
Inner join
(SELECT Min(date) dt,[user],ip From myTable
group by [user],ip) TBL on M.date=Tbl.dt and m.[user]=tbl.[user] and m.ip=tbl.ip

Sunil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top