cancel
Showing results for 
Search instead for 
Did you mean: 
Subscribe

I never thought I'd be asking an SQL question here. However today is the day!

Say I have a table with numeric columns A, B populated as follows. We can think of column A being a foreign key to a parent table, and column A and B together being the primary key of its child table:

A  B
1  1
1  2
2  1
2  2
3  1
3  2

I would like a SQL Select to return all records after, for example, (2, 1). I.e. (2, 2), (3, 1), and (3, 2).

Obviously the following won't work (it won't return (3, 1)):

select A, B from mytable when A > 2 and B > 1

I wish there were a way to write "A > 2 and B > 1" in a way that indicates B is "is a breakdown" of A, for lack of a better way to express it.

Of course I could create a derived column with the two numbers concatenated together, padded with enough zeros to accomodate maximum number size. Something like:

A  B  AandB
1  1  0101
1  2  0102
2  1  0201
2  2  0202
3  1  0301
3  2  0302

... Then I could write the SQL I want as:

select A, B from mytable when AandB > '0201'

However it would be wonderful if I could write a Where clause operating on the original numbers.

Maybe it would have been best to avoid multiple numeric columns making up a child table's key, although I'm not sure avoiding such would always eliminate the need for what I'm asking about.

This has been a tough one to Google search for solutions to. Thoughts and ideas are welcome!

0 Likes
View Entire Topic
johnsmirnios
Product and Topic Expert
Product and Topic Expert

That would be a tuple comparison. The following would work:

WHERE (A = 2 AND B > 1) OR A > 2

johnsmirnios
Product and Topic Expert
Product and Topic Expert
0 Likes

I'm a little fuzzy on arrays but the following might also work and is easily extended to more columns:

WHERE ARRAY( A, B ) > ARRAY( 2, 1 )

dhkom
Participant
0 Likes

Thanks, John. I probably should have figured that out too - sometimes we're just blocked. I've not used this ARRAY syntax and will definitely investigate.