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 6747733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:28:45+00:00 2026-05-26T12:28:45+00:00

I want to return four conditional aggregates from a function. I tried using CASE

  • 0

I want to return four conditional aggregates from a function. I tried using CASE expressions. My SQL:

CREATE OR REPLACE FUNCTION get_grade(IN integer, out integer,out integer,out      integer,out integer)  AS
$BODY$
begin
    select 
    sum(case when t.pirority = 66 then 1 else 0 end) as I ,
    sum(case when t.pirority = 67 then 1 else 0 end) as II,
    sum(case when t.pirority = 68 then 1 else 0 end) as III,
    sum(case when t.pirority = 225 then 1 else 0 end) as IIII 
    from dt_info t 
    where t.registrant = $1
end
$BODY$
LANGUAGE 'plpgsql' VOLATILE

When I use:

select * from get_grade(22);

it doesn’t work as expected.

Also tried:

CREATE OR REPLACE FUNCTION get_grade(IN integer) returns setof record AS
$BODY$
    select
    sum(case when t.pirority = 66 then 1 else 0 end) as I,
    sum(case when t.pirority = 67 then 1 else 0 end) as II,
    sum(case when t.pirority = 68 then 1 else 0 end) as III,
    sum(case when t.pirority = 225 then 1 else 0 end) as IIII
    from dt_info t 
    where t.registrant = $1
$BODY$
LANGUAGE 'sql' VOLATILE;

Then I execute:

select * from get_grade(25) as (v1 integer, v2 integer, v3 integer, v4 integer)

Error:

ERROR: function return row and query-specified return row do not match

How to get this right?

  • 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-26T12:28:46+00:00Added an answer on May 26, 2026 at 12:28 pm

    Try this:

    CREATE OR REPLACE FUNCTION get_grade(int)
      RETURNS TABLE (i int, ii int, iii int, iiii int)
      LANGUAGE sql AS
    $func$
    SELECT count(t.priority =  66 OR NULL)::int  -- AS I
         , count(t.priority =  67 OR NULL)::int  -- AS II
         , count(t.priority =  68 OR NULL)::int  -- AS III
         , count(t.priority = 225 OR NULL)::int  -- AS IIII
    FROM   dt_info t 
    WHERE  t.registrant = $1;
    $func$;
    

    You can write a simple query like this with LANGUAGE plpgsql. You can also just use LANGUAGE sql. Either has pros and cons. Showing an SQL function. Don’t quote the language name. The manual:

    Enclosing the name in single quotes is deprecated and requires matching case.

    Column aliases inside the function body are not visible outside. They only serve as documentation in this case. Use named OUT parameters or they get default names. RETURNS TABLE makes it a set-returning function and requires columns names in any case.

    OUT parameters are visible inside every DML SQL statement inside the function body. Table-qualify column names that would otherwise conflict with OUT parameter names. Don’t use aliases that would conflict. I commented out your aliases to be clear (though modern versions of Postgres wouldn’t conflict there).

    Data types of returned columns have to match the declaration in the header exactly. sum() or count() return bigint. You have to cast to integer explicitly to match the return type.

    Unquoted upper case identifiers are folded to lower case in Postgres anyway, and only serve to confuse.

    I used shorter (and a bit faster) expressions for your conditional count. In Postgres 9.4 or later use an aggregate FILTER instead. See:

    • For absolute performance, is SUM faster or COUNT?
    • Conditional SQL count
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to return a set of values from function till the point they
I want to return a private field from a remoted object but i get
I want to return ArrayList<HashMap<String,String>> in EL function with three String argument. How to
I want to return some files in a HttpResponse and I'm using the following
i have four queries that return intergers. select listOfIntegers from [something]... ( edit: the
I have four tables I want to join and get data from. The tables
I want to build a search with four search terms. I'm using Kohana 3.0.7
I have four fields, and I the function to return true if atleast one
I create a table with two sections. Each section has four cells.I want to
I want to return StudentId to use elsewhere outside of the scope of the

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.