Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 26F132E0057 for ; Wed, 16 Apr 2008 12:07:47 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 60268-05 for ; Wed, 16 Apr 2008 12:07:43 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.245]) by developer.postgresql.org (Postfix) with ESMTP id CD5A12E004A for ; Wed, 16 Apr 2008 12:07:43 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c36so705599ana.33 for ; Wed, 16 Apr 2008 08:07:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:from; bh=zyONa7t72sbsXOaW4o5r65QVy1TL06cXKfJ82w7ZKFA=; b=bHYFHaY1TFuNjJREju1yYx+iHN/o1XLWBA54gaTx0eb2kvIVCM7T9o/s74sfIqVf+YWL8MYoziWr7mPxEZNfHmlv29pYYFpN/itZSpApkgbQqhcHK84XfHD5D9ryWcNYaTHbRJVw7mit+bfAOQx/XGas0nLbJxkBep/NoH/nukk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:from; b=s/iXjiz51ZEhRWJZV3BsJ/F16VAMXqrCZkAcKZf1z29yp63axE0MHP/5WKwBHkcJohk2lWZS378ooqeNCnLJr8FgXGWoyVpaNOHJTN3t3AwzewibeLC/EphKrvADEd/K1S1EeWBbt/m8Xbm6eAsKNcDZC3XzkbvPcVCuIDoaVY4= Received: by 10.100.251.8 with SMTP id y8mr108465anh.133.1208358463257; Wed, 16 Apr 2008 08:07:43 -0700 (PDT) Received: from ?10.2.200.151? ( [189.25.14.179]) by mx.google.com with ESMTPS id 1sm11439838agb.8.2008.04.16.08.07.37 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 16 Apr 2008 08:07:40 -0700 (PDT) Message-ID: <4806168D.6080802@gmail.com> Date: Wed, 16 Apr 2008 12:09:01 -0300 User-Agent: Thunderbird 1.5.0.12 (X11/20070530) MIME-Version: 1.0 To: Bryan Emrys CC: pgsql-sql@postgresql.org Subject: Re: Data Comparison Single Table Question References: <200804160634.27304.bryan.emrys@gmail.com> In-Reply-To: <200804160634.27304.bryan.emrys@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit From: Osvaldo Rosario Kussama X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/98 X-Sequence-Number: 30637 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