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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

not so simple sql-question?: SELECT iSession FROM O_Sessions ORDER BY

Status
Not open for further replies.

robosoft

Programmer
Jul 16, 2002
2
DE
I want the latest (highest) iSession to be selected:

SELECT iSession FROM O_Sessions GROUP BY iUser

Because of GROUP BY, allways the first(!) recordset for iUser is selected.
But i want the last recordset to be selected :-(

How can this be done ?
(mySQL)
(iSession ist the primary key => latest=highest. filling the primary key from 4294967295 down to 0
does not work)

---------

i have allready posted in the ansi-sql forum, but it also seems to be a mysql specific question:

this is the setting (also look below for example table):
O_Sessions contains multilpe recordsets for iUser (let' say iUser=42).
I want the last RECORDSET of this iUser=42, not the first.
With GROUP BY, all the various recordsets with iUser=42 are thrown together, and mysql simply takes
the first one (=>lowest iSession, as iSession is the increasing primary index.)
But i need the last one of iUser=42 and the last one of iUser=42 and so on.

SELECT iUser, MAX(iSession) FROM O_Sessions GROUP BY iUser;
will not do, it will only select the greatest iSession for every iUser, not the latest RECORDSET.
I need the recordset, as there is more data in O_Sessions like sSession, which holds the name for
the session...

SELECT iSession FROM O_Sessions GROUP BY iUser ORDER BY iSession DESC
will also not do, as it only orderes the output and does nothing about the "pre-selection" during
grouping.

Maybe I could do with a sub-select, but mysql does not offer that ? And the querry would blow up
anyway.

I have demands like this from time to time, and have never found a satisfying solution.
I fear, that sql does not support my wants and there is no way to tell the sql server which
recordset to select when grouping the table.
I think, grouping is implemented by simply advancing the recordsets an ignoring iUser=42 when one
is found. That way, allways the first recordset is selected. But is there no way to tell the system
to search the table downwards ?

Something like this:
SELECT iSession FROM O_Sessions ORDER BY iSession DESC GROUP BY iUser
(not sql)


well, i am currently making a halt in programming, hoping for you to find a solution :)

deep in the night, with a german Sky and soft triphop
spreading in my room, heading for bed, counting on you,
roland (bollmann) :)


------------------------------ O_Sessions:
| iSession | iUser | sSession|
| 1 | 41 | "not wanted" |
| 2 | 41 | "wanted" |
| 3 | 42 | "not wanted" |
| 4 | 42 | "wanted" |
| 5 | 43 | "not wanted" |
| 6 | 43 | "wanted" |
-------------------


 
Try:

SELECT MAX(isession), iuser FROM o_sessions GROUP BY iuser;
______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top