cancel
Showing results for 
Search instead for 
Did you mean: 

Updating or selecting every second row

philippefbertrand
Participant
1,670

My ultimate goal is to select every second row (by primary key order) without any additional columns in the result set. I want to do any manipulation in dbisql but the final SELECT will be in a MobiLink script. I need to repeat this on multiple tables and can make no assumptions about the pkey values or type. I can add a column to each table if needed.

Can it be done in a single select statement (without adding a column)? (I know SELECT * FROM Foo WHERE mod(number(*),2) = 0 ORDER BY num does not work)

If I add a column to all the tables, can a single update be written to update every second column?

If the rows are inserted in primary key order, chances are adding a column with default autoincrement should number them in order and then I select every odd or even value but there is no guarantee of the order.

I'm working with SQL Anywhere 11 in this particular problem.

philippefbertrand
Participant

Yes, I could have just walked down the hallway but when I first pondered this, the first answers I got weren't satisfactory. Besides, there aren't enough question here - perhaps its just because our docs are so great!

Accepted Solutions (1)

Accepted Solutions (1)

philippefbertrand
Participant

After messing up the syntax a few times, I got a single SELECT to work:

SELECT Ba.num, Ba.MyID FROM
   (SELECT num, MyID, row_number() OVER( ORDER BY num ) From Foo) AS Ba(num,MyID,bar)
WHERE mod(Ba.bar,2) = 0

Still curious if a single UPDATE could be written to update every second row.

MarkCulp
Participant

The update part is simple once you have the query that selects the PKs of the rows to be updated - the general form is:

UPDATE T SET ... WHERE T.pk in ( <your-query-that-selects-the-pks-to-be-updated> )

philippefbertrand
Participant

Yes, now that I figured out how to control the select list, it is obvious.

Answers (0)