I’d like to get all the records from a huge table where any of the number columns countains a value greater than 0. What’s the best way to do it?
E.g.:
/* table structure*/
create table sometable (id number,
somestring varchar2(12),
some_amount_1 number(17,3),
some_amount_2 number(17,3),
some_amount_3 number(17,3),
...
some_amount_xxx number(17,3));
/* "xxx" > 100, and yeah I did not designed that table structure... */
And I want any row where any of the some_amount_n > 0 (even better solution is to add a field in the first place to show which field(s) are greater than zero).
I know I can write this with a huge some_amount_1 > 0 OR some_amount_2 > 0 OR ... block (and the field names with some case when but is there should be some more elegant solution, isn’t there?
Possible solutions:
Normalize the table. You said you are not allowed to. Try to convince those that forbid such a change by explaining the benefits (performance, ease of writing queries, etc).
Write the huge ugly
ORquery. You could also print it along with the version of the query for the normalized tables. Add performance tests (you are allowed to create another test table or database, I hope.)Write a program (either in PL/SQL or in another procedural language) that produces the horrible
ORquery. (Again, print along with the elegant version)Add a new column, say called
Any_x_bigger_than_zerowhich is automatically filled with either0or1via a trigger (that uses a huge uglyOR). Then you just need to check:WHERE Test_x_bigger_than_zero = 1to see if any of the rows is> 0Similar to previous but even better, create a materialized view with such a column.