on 2011 Jun 23 11:50 AM
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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> )
Yes, now that I figured out how to control the select list, it is obvious.
User | Count |
---|---|
81 | |
11 | |
10 | |
10 | |
10 | |
8 | |
7 | |
7 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.