If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > group by help?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-03-05, 21:36
pebkac pebkac is offline
Registered User
 
Join Date: Jan 2004
Posts: 35
Question group by help?

I am trying to get the last occurance of a display name in a login user log database. Basically I have a table that looks like this:

id user fname lname
-----------------------------------
1 jdoe Jane Doe
2 jdoe John Doe
3 jdoe Fred Flinstone


I run the MySQL query: SELECT name, max(id) as max_id, user FROM `logins` GROUP BY user

I get back:
id user fname lname
-----------------------------------
3 jdoe Jane Doe


I actually want:
id user fname lname
-----------------------------------
3 jdoe Fred Flinstone


Any that can help it would be greatly Appreciated!!
Reply With Quote
  #2 (permalink)  
Old 10-03-05, 22:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
by "last" occurrence you mean the one with the largest id?
Code:
select id
     , user
     , fname
     , lname 
  from logins as ZZ
 where id 
     = ( select max(id)
           from logins
          where user = ZZ.user )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-20-05, 19:45
pebkac pebkac is offline
Registered User
 
Join Date: Jan 2004
Posts: 35
I tried this result and am still having difficulties? Do you know if this works with all versions of MySQL? I get the following error from phpmyadmin:

You have an error in your SQL syntax near 'select max(id) from logins where user=ZZ.user

Any other thoughts?
Reply With Quote
  #4 (permalink)  
Old 10-20-05, 19:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
good guess -- subqueries are not supported prior to version 4.1

how come it took you two and a half weeks to try my solution?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-23-05, 14:54
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
If a correlated subquery is not supported, let's hope a join (and a group by) is?
Could you try this one:
Code:
  select a.id, a.user, a.fname, a.lname
    from logins as a, logins as b
   where a.user = b.user
     and a.id <= b.id
group by a.id, a.user, a.fname, a.lname
  having count(*) = 1
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On