pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Campbell, Lance <lance@illinois.edu>
To: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: Group By aggregate string function
Date: Thu, 21 Feb 2019 18:59:18 +0000
Message-ID: <04E1F4B6-7092-4338-B97D-EC495C5F1B1E@illinois.edu> (raw)

PostgreSQL 10.x

Below is my situation.  I need some time of aggregate string function that when it finds multiple string values it will order them based on a preferred preference.  Example:  “admin”, then “manager” then “…”.

Table T
fk_id int – foreign key
user_id  text
role text  - possible values could be “admin” and “manager”

Primary key (fk_id, user_id, role)

Sample data:

  1.  lance  admin
1     lance manager
87   bob   manager
98   tom admin
104 tom manager

SELECT fk_id, user_id, some-aggregate-string-function(role, “admin”, “manager”)  FROM T  WHERE user_id = ‘lance’ GROUP BY fk_id, user_id;

When selecting data if there are multiple rows within the group by then aggregate the result based on a priority for role of “admin” first, “manager” second, etc.

Expected Result:
1 lance admin

Ignores the second record with lance in it because it contains admin.

THANKS!


LANCE CAMPBELL<https://directory.illinois.edu/person/lance;
Software Architect

Web Services<https://webtools.illinois.edu/;
Public Affairs<https://publicaffairs.illinois.edu/;
Contact the Webtools Team<https://go.illinois.edu/contactUs;
217.333.0382
lance@illinois.edu<mailto:lance@illinois.edu>


[/var/folders/wp/1f6l7hw95y718z976kgnl5f9kr5rtc/T/com.microsoft.Outlook/WebArchiveCopyPasteTempFiles/signature_logo.png]<http://illinois.edu/;

Under the Illinois Freedom of Information Act any written communication to or from university employees regarding university business is a public record and may be subject to public disclosure.



Attachments:

image001.png (image/png, 2.5K) download view image

view thread (4+ messages)  latest in thread

Message-ID: <04E1F4B6-7092-4338-B97D-EC495C5F1B1E@illinois.edu>
Permalink:  ../04E1F4B6-7092-4338-B97D-EC495C5F1B1E@illinois.edu/
Also on:    postgresql.org/message-id/04E1F4B6-7092-4338-B97D-EC495C5F1B1E@illinois.edu

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: lance@illinois.edu
  Subject: Re: Group By aggregate string function
  In-Reply-To: <04E1F4B6-7092-4338-B97D-EC495C5F1B1E@illinois.edu>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox