Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7050333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:07:50+00:00 2026-05-28T03:07:50+00:00

Most databases have something like a GREATEST function, which can be useful some times.

  • 0

Most databases have something like a GREATEST function, which can be useful some times. At least these databases don’t have such a function:

  • Derby
  • SQL Server
  • Sybase ASE
  • Sybase SQL Anywhere

For SQL Server and Sybase SQL Anywhere, the function can be simulated using subqueries and UNION ALL, as can be seen in this question here. An example:

-- SELECT GREATEST(field1, field2, field3) FROM my_table
SELECT (SELECT MAX(c) FROM 
                     (SELECT my_table.field1 AS c UNION ALL 
                      SELECT my_table.field2      UNION ALL
                      SELECT my_table.field3) T) AS greatest
FROM my_table

But this doesn’t work in Sybase ASE. Apparently, the subqueries don’t have access to the outer query’s my_table reference. The error I get is

The column prefix ‘my_table’ does not match with a table name or alias name used in the query. Either the table is not specified in the FROM clause or it has a correlation name which must be used instead

Note, this problem does not appear with Sybase SQL Anywhere. Any idea what’s wrong here and how I could re-write the query?

I’d like to avoid

  • Stored functions, as I may not have the necessary grants to create them
  • Lengthy CASE expressions, as the expression length of the combined permutation of all comparisons needed with nested CASE expressions is at least O(n^2) when n is the number of parameters for GREATEST
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:07:51+00:00Added an answer on May 28, 2026 at 3:07 am

    As I understand it, the logic (ignoring nulls) is

    SELECT CASE 
              WHEN field1 >= field2 
                   AND field1 >= field3
                 THEN field1
              WHEN field2 >= field3
                 THEN field2
              ELSE field3
           END AS greatest
      FROM my_table;
    

    …but should only return null when all values are null.


    I think this is more how I’d like to be able do things (although, Sybase ASE does not support common table expressions):

    WITH my_table
         AS 
         (
          SELECT * 
            FROM (
                  VALUES ('A', 1, 2, 3), 
                         ('B', 2, 3, 1), 
                         ('C', 3, 1, 2),
                         ('D', NULL, 2, 3), 
                         ('E', NULL, NULL, 3), 
                         ('F', NULL, 3, NULL), 
                         ('G', 1, NULL, 3), 
                         ('H', 1, 3, NULL), 
                         ('X', NULL, NULL, NULL)
                 ) AS T (ID, field1, field2, field3)
         ), 
         T1
         AS
         (
          SELECT ID, field1 AS field_n
            FROM my_table
          UNION
          SELECT ID, field2 AS field_n
            FROM my_table
          UNION
          SELECT ID, field3 AS field_n
            FROM my_table
         )        
    SELECT ID, MAX(field_n) AS greatest
      FROM T1
     GROUP 
        BY ID;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Android projects and most of them are connected with SQLite databases
We have a database in which all the PKs are GUIDs, and most of
Most databases support some form of insert into select... statement. insert into a select
I have a situation in which we have two production databases that synchronize with
I have a database that looks something like this: user_id photo_id 1 1 1
Let's say I have two Databases like so : DatabaseA create table Table1 (
I have a list of fields which a user can sort using jquery. I
I have a database where most tables have a delete flag for the tables.
This is probably the most classic database problem. I have an E-commerce software solution
I have a MySQL database with 60 tables. Most of the tables have primary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.