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

  • Home
  • SEARCH
  • 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 6254081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:07:38+00:00 2026-05-24T14:07:38+00:00

I want to generate SQL like this: SELECT coalesce(max(a.a_index) + 1, 0) as a_index

  • 0

I want to generate SQL like this:

SELECT coalesce(max(a.a_index) + 1, 0) as a_index FROM table1 a

I can get everything but the “+ 1”. Is there a way to add this to the query?

use strict;

use Fey::Schema;
use Fey::Loader;

use DBI;

my $dbh = DBI->connect("DBI:mysql:....", "...", "...");

my $loader = Fey::Loader->new( dbh => $dbh );

my $s = $loader->make_schema();

my $q = Fey::SQL->new_select();

my $a_table = $s->table('table1')->alias('a');

my $a_index_col = $a_table->column('a_index');

my $max_a_index_func = Fey::Literal::Function->new('max', $a_index_col);
$max_a_index_func->set_alias_name('max_a_index');

my $c_a_index_func = Fey::Literal::Function->new('coalesce', $max_a_index_func, "0");
$c_a_index_func->set_alias_name('a_index');

$q->select($c_a_index_func);
$q->from($a_table);

print $q->sql($dbh);
print "\n";

Output

SELECT coalesce(max(`a`.`a_index`), 0) AS `a_index` FROM `table1` AS `a`
  • 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-24T14:07:38+00:00Added an answer on May 24, 2026 at 2:07 pm

    I don’t think you can since Column, Literal and Function objects don’t have operations to link them together (nor do I see any classes besides Function that can do that).

    The only work-around I can see with code generation is to create a stored function:

    CREATE FUNCTION plus_one (n INT)
    RETURNS INT DETERMINISTIC
    RETURN COALESCE(n + 1, 0);
    

    Then use this function instead of the coalesce:

    my $c_a_index_func = Fey::Literal::Function->new('plus_one', $max_a_index_func);
    $c_a_index_func->set_alias_name('a_index');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate this SQL statement in LINQ: select * from Foo where
This is the pseudo-SQL I want to generate: SELECT * FROM Table WHERE Column1
When I generate entity classes using LINQ to SQL I get what I want
I know that I can use IN(1,2,3) to select items from an SQL database
I want to generate some formatted output of data retrieved from an MS-Access database
i have a query like below return this.Context.Modules .Select(p => new { Module =
I'm having troubles with HQL again :( My desired SQL is this: select employee.idemployee
I'm work on project in c# ,and I want to generate data from my
Is there any effective way to generate JSON out of MYSQL query like this?
i want to generate a unique number from a table. It has to be

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.