pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Data Comparison Single Table Question
2+ messages / 2 participants
[nested] [flat]

* Data Comparison Single Table Question
@ 2008-04-16 13:34  Bryan Emrys <bryan.emrys@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Bryan Emrys @ 2008-04-16 13:34 UTC (permalink / raw)
  To: pgsql-sql

I can handle this outside sql, but it seems like I should be able to do this in sql as well. 

1 table: countries. 
3 columns: id, name, price

What I'm trying to get is a result of the price differences between every country.

So if the data looks like (ignoring the id field)

Taiwain 30
UK 50
US 40

I'm trying to build a matrix that looks like:
		Taiwan	UK	US
Taiwan	0		-20	-10
UK		20		0	10
US		10		-10	0

Any pointers would be appreciated.

Bryan



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Data Comparison Single Table Question
@ 2008-04-16 15:09  Osvaldo Rosario Kussama <osvaldo.kussama@gmail.com>
  parent: Bryan Emrys <bryan.emrys@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Osvaldo Rosario Kussama @ 2008-04-16 15:09 UTC (permalink / raw)
  To: Bryan Emrys <bryan.emrys@gmail.com>; +Cc: pgsql-sql

Bryan Emrys escreveu:
> I can handle this outside sql, but it seems like I should be able to do this in sql as well. 
> 
> 1 table: countries. 
> 3 columns: id, name, price
> 
> What I'm trying to get is a result of the price differences between every country.
> 
> So if the data looks like (ignoring the id field)
> 
> Taiwain 30
> UK 50
> US 40
> 
> I'm trying to build a matrix that looks like:
> 		Taiwan	UK	US
> Taiwan	0		-20	-10
> UK		20		0	10
> US		10		-10	0
> 


Not in a matrix form:

bdteste=# SELECT f1.name,f1.price,f2.name,f1.price-f2.price AS 
difference FROM foo f1 CROSS JOIN foo f2;
   name   | price |  name   | difference
---------+-------+---------+------------
  Taiwain |    30 | Taiwain |          0
  Taiwain |    30 | UK      |        -20
  Taiwain |    30 | US      |        -10
  UK      |    50 | Taiwain |         20
  UK      |    50 | UK      |          0
  UK      |    50 | US      |         10
  US      |    40 | Taiwain |         10
  US      |    40 | UK      |        -10
  US      |    40 | US      |          0
(9 registros)

Osvaldo



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2008-04-16 15:09 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2008-04-16 13:34 Data Comparison Single Table Question Bryan Emrys <bryan.emrys@gmail.com>
2008-04-16 15:09 ` Osvaldo Rosario Kussama <osvaldo.kussama@gmail.com>

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