By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,007 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,007 IT Pros & Developers. It's quick & easy.

SQL Question

P: n/a
Hello,

DB2 V8 LUW.

I have a simple table A with fields ID and SESSION_NUMBER (integer).
One ID can have multiple session numbers.

For a given ID, I can find the last two sessions with a simple SQL:

select * from table where ID='xyz' order by SESSION_NUMBER desc fetch
first two rows only

This may be rather simple, but I am stuck:

How do I return only the last two sessions for each ID that this table
has?

TIA, Michel

Feb 15 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Just did something similar. Look up the OLAP rownumber() function.
Partition by your ID field and order by your SESSION_NUMBER
descending. Feed the results of this to an outer query and look for
rows having a rownumber of 2 or less. Something like:

select id, session_number
from (
select id, session_number, rownumber() over (partition by id order
by sess desc) as rn
from table_A
) as tt
where tt.rn <=2
HTH,
Mike
On Feb 15, 2:23 pm, "Michel Esber" <mic...@us.automatos.comwrote:
Hello,

DB2 V8 LUW.

I have a simple table A with fields ID and SESSION_NUMBER (integer).
One ID can have multiple session numbers.

For a given ID, I can find the last two sessions with a simple SQL:

select * from table where ID='xyz' order by SESSION_NUMBER desc fetch
first two rows only

This may be rather simple, but I am stuck:

How do I return only the last two sessions for each ID that this table
has?

TIA, Michel

Feb 15 '07 #2

P: n/a
On Feb 15, 12:23 pm, "Michel Esber" <mic...@us.automatos.comwrote:
Hello,

DB2 V8 LUW.

I have a simple table A with fields ID and SESSION_NUMBER (integer).
One ID can have multiple session numbers.

For a given ID, I can find the last two sessions with a simple SQL:

select * from table where ID='xyz' order by SESSION_NUMBER desc fetch
first two rows only

This may be rather simple, but I am stuck:

How do I return only the last two sessions for each ID that this table
has?

TIA, Michel
select * from table a
where SESSION_NUMBER in
(select b.SESSION_NUMBER from table b
where b.ID = a.ID
order by b.SESSION_NUMBER desc
fetch first 2 rows only)

Feb 16 '07 #3

P: n/a
On 15 Feb 2007 11:23:05 -0800, "Michel Esber"
<mi****@us.automatos.comwrote:
>Hello,

DB2 V8 LUW.

I have a simple table A with fields ID and SESSION_NUMBER (integer).
One ID can have multiple session numbers.

For a given ID, I can find the last two sessions with a simple SQL:

select * from table where ID='xyz' order by SESSION_NUMBER desc fetch
first two rows only

This may be rather simple, but I am stuck:

How do I return only the last two sessions for each ID that this table
has?

TIA, Michel
By adding a DATE COLUMN, then use ORDER BY. Without an ORDER BY in the
query, there is no real order, and the results may change in between
executions.

B.
Feb 16 '07 #4

P: n/a
On Feb 16, 8:04 am, Brian Tkatch <N/Awrote:
>
By adding a DATE COLUMN, then use ORDER BY. Without an ORDER BY in the
query, there is no real order, and the results may change in between
executions.
I think it is reasonable to assume that the SESSION_NUMBER is assigned
sequentially for each ID, and the two highest values are the last two
inserted for a given ID.

Feb 16 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.