tags:

views:

27

answers:

1

I have a table with multiple rows for each user. Each user will have the same number of rows but the row ids are all different for each user, so how I do get just the Nth row for a given user?

Assuming an order by user_id, row_id clause so you can be guaranteed a consistent order for any given user.

This is sybase 12 I'm working with here.

A: 

if you can add seq_id column to that table that started from 0 for each user_id. You can query with this column.

Burçin Yazıcı
can't change the data, not an option. good idea though.
stu
@stu: Can you add another table (keyed on combination of user_id and row_id of your existing table) with a seq_id on it? Unless you can add some kind of data somewhere you are out of luck on this, because your database design is inadequate for what you need to do.
Nathan Hughes
well that kinda sucks. changing these tables (there's a bunch of them) would involve fixing lots of legacy applications which is why it's not really an option just for my piddly little project.The only thing I came up with was to make a select from select type of thing where I did select top 3 then outside of that do a select top 1 with the order reversed. Extremely ugly but it will work. I was hoping for something simpler.It would go like this:select field from (select top 1 field from (select top 3 field from table) order by field) order by field desc
stu