Preface: That's one of the SQL pitfalls I've stumbeld over too often...

Well, sometimes I've to turn expressions from an ordinary programming language into a SQL query, and sometimes these expressions contain the classical scalar max() function, say like "where max(expr1, expr2) > 0".

The following (absolutely senseless) query would be a sample:

select * from systab
where max(table_page_count, ext_page_count) > 10;

Of course, using max() that way in a SQL query is doomed to fail, as MAX() is an aggregate function and works on rows of data, not on two (or more) separate expressions, so this raises

alt text

Question: Is there a way to use max() as intended here?

asked 18 Mar '13, 17:04

Volker%20Barth's gravatar image

Volker Barth
30.0k294448654
accept rate: 32%

edited 18 Mar '13, 17:13


Well, you cannot use MAX() for that - but of course SQL Anywhere has according support:

Use GREATER() as a scalar MAX() and LESSER() as a scalar MIN(), such as

select * from systab
where greater(table_page_count, ext_page_count) > 10

And now I hope I won't forget that anymore:)

permanent link

answered 18 Mar '13, 17:10

Volker%20Barth's gravatar image

Volker Barth
30.0k294448654
accept rate: 32%

edited 18 Mar '13, 17:11

1

Thanks for the votes: You like to learn from my mistakes, don't you:)

Feel free to tell about your favourite pitfallslearning experiences, too...

(19 Mar '13, 12:47) Volker Barth
1

It should be noted that GREATER() and LESSER() are vendor extensions (as documented), so for portable SQL, one would probably need to

  • use a CASE expression ("case when isnull(table_page_count, 0) > isnull(ext_page_count, 0) then ...") or
  • try to re-state the logic by ORing expressions or the like.

Aside: I had thought of suggesting an IF expression here - but that's another one of these very helpful vendor extension itself:)

(21 Mar '13, 06:44) Volker Barth
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×13
×6
×5

question asked: 18 Mar '13, 17:04

question was seen: 959 times

last updated: 21 Mar '13, 06:44