pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Lee Harr <missive@frontiernet.net>
To: pgsql-sql@postgresql.org
Subject: Re: Diffcult query
Date: Sat, 22 Mar 2003 03:59:18 +0000 (UTC)
Message-ID: <b5gn2l$1s2o$2@news.hub.org> (raw)
References: <d9Iea.117330$JE5.48428@news2.central.cox.net>

In article <d9Iea.117330$JE5.48428@news2.central.cox.net>, Dan Winslow wrote:
> Hi folks. This is giving me fits. I feel like it oughta be simple, but
> apparantly its not...I think.
> 
> Given a table :
> 
> create table tablea (
>   code1 varchar(32),
>   code2 varchar(32),
>   cost    int
> );
> 
> and the rows
> 
> code1    code2    cost
> ----------------------------------
> "aaa"        "bbb"    2
> "ddd"        "eee"    3
> "bbb"        "aaa"    6
> "ggg"         "hhh"    4
> 
> I need a ( preferably single ) query that will sum the costs for all unique
> pairs of codes, without regard to column order. That is, for summing
> purposes, rows 1 and 3 are identical, and should produce the sum of 8 for
> the unordered pair("aaa","bbb"). It should also, of course, prevent the case
> where ("bbb","aaa") is considered a seperate pair. Any ideas would be much
> appreciated.
> 
> 


Maybe you could create a function that will return all of the rows
with a particular pair of codes.

Then I think you could GROUP BY that function...




view thread (5+ messages)  latest in thread

Message-ID: <b5gn2l$1s2o$2@news.hub.org>
Permalink:  ../b5gn2l$1s2o$2@news.hub.org/
Also on:    postgresql.org/message-id/b5gn2l$1s2o$2@news.hub.org

 · 

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: missive@frontiernet.net
  Subject: Re: Diffcult query
  In-Reply-To: <b5gn2l$1s2o$2@news.hub.org>

* 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