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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:28:00+00:00 2026-05-18T11:28:00+00:00

I often find myself adding expressions in the group by clause that I am

  • 0

I often find myself adding expressions in the group by clause that I am sure are unique. It sometimes turns out I am wrong – because of an error in my SQL or a mistaken assumption, and that expression is not really unique.

There are many cases when I would much rather this would generate a SQL error rather than expanding my result set silently and sometimes very subtly.

I would love to be able to do something like:

select product_id, unique description from product group by product_id

but obviously I can’t implement that myself – but something nearly as concise can be implemented with user defined aggregates on some databases.

Would a special aggregate that only allows one unique input value be generally helpful in all versions of SQL? If so, could such a thing be implemented now on most databases? null values should be considered just like any other value – unlike the way the built-in aggregate avg typically works. (I have added answers with ways of implementing this for postgres and Oracle.)

The following example is intended to show how the aggregate would be used, but is a simple case where it is obvious which expressions should be unique. Real usage would more likely be in larger queries where it is easier to make mistaken assumptions about uniqueness

tables:

 product_id | description
------------+-------------
          1 | anvil
          2 | brick
          3 | clay
          4 | door

 sale_id | product_id |  cost
---------+------------+---------
       1 |          1 | £100.00
       2 |          1 | £101.00
       3 |          1 | £102.00
       4 |          2 |   £3.00
       5 |          2 |   £3.00
       6 |          2 |   £3.00
       7 |          3 |  £24.00
       8 |          3 |  £25.00

queries:

> select * from product join sale using (product_id);

 product_id | description | sale_id |  cost
------------+-------------+---------+---------
          1 | anvil       |       1 | £100.00
          1 | anvil       |       2 | £101.00
          1 | anvil       |       3 | £102.00
          2 | brick       |       4 |   £3.00
          2 | brick       |       5 |   £3.00
          2 | brick       |       6 |   £3.00
          3 | clay        |       7 |  £24.00
          3 | clay        |       8 |  £25.00

> select product_id, description, sum(cost) 
  from product join sale using (product_id) 
  group by product_id, description;

 product_id | description |   sum
------------+-------------+---------
          2 | brick       |   £9.00
          1 | anvil       | £303.00
          3 | clay        |  £49.00

> select product_id, solo(description), sum(cost) 
  from product join sale using (product_id) 
  group by product_id;

 product_id | solo  |   sum
------------+-------+---------
          1 | anvil | £303.00
          3 | clay  |  £49.00
          2 | brick |   £9.00

error case:

> select solo(description) from product;
ERROR:  This aggregate only allows one unique input
  • 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-18T11:28:01+00:00Added an answer on May 18, 2026 at 11:28 am

    Here is my implementation for postgres (edited to treat null as a unique value too):

    create function solo_sfunc(inout anyarray, anyelement) 
           language plpgsql immutable as $$
    begin
      if $1 is null then
        $1[1] := $2;
      else
        if ($1[1] is not null and $2 is null) 
             or ($1[1] is null and $2 is not null) 
             or ($1[1]!=$2) then 
          raise exception 'This aggregate only allows one unique input'; 
        end if;
      end if;
      return;
    end;$$;
    
    create function solo_ffunc(anyarray) returns anyelement 
           language plpgsql immutable as $$
    begin
      return $1[1];
    end;$$;
    
    create aggregate solo(anyelement)
                         (sfunc=solo_sfunc, stype=anyarray, ffunc=solo_ffunc);
    

    example tables for testing:

    create table product(product_id integer primary key, description text);
    
    insert into product(product_id, description)
    values (1, 'anvil'), (2, 'brick'), (3, 'clay'), (4, 'door');
    
    create table sale( sale_id serial primary key, 
                       product_id integer not null references product, 
                       cost money not null );
    
    insert into sale(product_id, cost)
    values (1, '100'::money), (1, '101'::money), (1, '102'::money),
           (2, '3'::money), (2, '3'::money), (2, '3'::money),
           (3, '24'::money), (3, '25'::money);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.