Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 977991337B90 for ; Tue, 14 Dec 2010 09:10:56 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 47057-02 for ; Tue, 14 Dec 2010 13:10:48 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from compulab.co.il (compulab.co.il [67.18.134.219]) by mail.postgresql.org (Postfix) with ESMTP id A81151337B77 for ; Tue, 14 Dec 2010 09:10:48 -0400 (AST) Received: from [62.90.235.247] (helo=zimbra-mta.compulab.co.il) by compulab.site5.com with esmtp (Exim 4.69) (envelope-from ) id 1PSUeV-0000mh-Gc; Tue, 14 Dec 2010 07:10:48 -0600 Received: from localhost (localhost.localdomain [127.0.0.1]) by zimbra-mta.compulab.co.il (Postfix) with ESMTP id 92CA07E8B59; Tue, 14 Dec 2010 15:10:46 +0200 (IST) X-Virus-Scanned: amavisd-new at compulab.co.il Received: from zimbra-mta.compulab.co.il ([127.0.0.1]) by localhost (zimbra-mta.compulab.co.il [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id a83BB4TPX8su; Tue, 14 Dec 2010 15:10:45 +0200 (IST) Received: from [10.1.1.27] (sim-pc.compulab.local [10.1.1.27]) by zimbra-mta.compulab.co.il (Postfix) with ESMTP id 634377E915B; Tue, 14 Dec 2010 15:10:45 +0200 (IST) Message-ID: <4D076C9F.2060404@compulab.co.il> Date: Tue, 14 Dec 2010 15:09:51 +0200 From: Sim Zacks User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14pre) Gecko/20101211 Lightning/1.0b2 Shredder/3.1.8pre MIME-Version: 1.0 To: =?UTF-8?B?RmlsaXAgUmVtYmlhxYJrb3dza2k=?= CC: PostgreSQL general Subject: Re: crosstab function References: <4D071A18.2060205@compulab.co.il> <4D074AAF.7000807@compulab.co.il> In-Reply-To: <4D074AAF.7000807@compulab.co.il> Content-Type: multipart/alternative; boundary="------------020607000107090608060808" X-ACL-Warn: { X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - compulab.site5.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - compulab.co.il X-Source: X-Source-Args: X-Source-Dir: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.909 tagged_above=-5 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201012/457 X-Sequence-Number: 170748 This is a multi-part message in MIME format. --------------020607000107090608060808 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable Just to clarify, there aren't duplicate rows. But if the customer=20 purchased two products there are 2 rows for that customer, one with the=20 quantity sum filled in for the first product and the second row with the=20 quantity sum filled in for the second product. I want one customer row=20 with all the items purchased filled in. Sim On 12/14/2010 12:45 PM, Sim Zacks wrote: > I rechecked and with products as columns it has duplicate customers.=20 > My goal is one row per customer with the sum of quantity filled in for=20 > each product they purchased. > > create table customers(customerid serial primary key, customername text= ); > create table products(productid serial primary key, productname text); > create table quotations(quotationid serial primary key, customerid=20 > int, orderdate timestamp); > create table quotationitems(quotationitemid serial primary key,=20 > quotationid int, productid int, quantity int, unitprice numeric(9,2)); > > select * from crosstab(' > select customername,productname as bucket,sum(quantity) as bucketvalue > from quotationitems a join quotations b using(quotationid) > join customers c using(customerid) > join sales.products d using (productid) > where orderdate between ''1/1/2009'' and ''1/1/2010'' > and producttypeid=3D1 > group by customername,productname order by productname', > 'select productname from sales.products where producttypeid=3D1 order b= y=20 > productname') > as rpt(customername text,"ATX" int, > "CM-A510" int, > "CM-F82" int, > "CM-i586" int, > "CM-i686B" int, > "CM-i686M" int, > "CM-iAM" int, > "CM-iGLX" int, > "CM-iPM" int, > "CM-iTC" int, > "CM-T3530" int, > "CM-X255" int, > "CM-X270" int, > "CM-X300" int, > "CM-XAM" int > ) > order by customername > > > On 12/14/2010 10:27 AM, Filip Rembia=C5=82kowski wrote: >> please show >> >> - source data structures (in form of CREATE TABLE please) >> >> - actual whole query that creates duplicates >> >> >> >> >> >> 2010/12/14 Sim Zacks > >> >> postgres 8.2.17 >> >> I am trying out the crosstab function (tablefunc contrib) for >> reporting needs and I'm having a problem. >> I have customers and products and the data is the quantity >> purchased. I am grouping by customername, productname in the >> source sql. My category sql depends if I want the products or >> customers to be the columns. >> >> When I make customers the rows and products the columns, it works >> fine. But when I make customers the columns and products the >> rows, there are duplicate product rows. >> >> Is there a way to group the product rows so that the data results >> come back correct? >> >> --=20 >> Sent via pgsql-general mailing list (pgsql-general@postgresql.org >> ) >> To make changes to your subscription: >> http://www.postgresql.org/mailpref/pgsql-general >> >> > --------------020607000107090608060808 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Just to clarify, there aren't duplicate rows. But if the customer purchased two products there are 2 rows for that customer, one with the quantity sum filled in for the first product and the second row with the quantity sum filled in for the second product. I want one customer row with all the items purchased filled in.

Sim

On 12/14/2010 12:45 PM, Sim Zacks wrote:
I rechecked and with products as columns it has duplicate customers. My goal is one row per customer with the sum of quantity filled in for each product they purchased.

create table customers(customerid serial primary key, customername text);
create table products(productid serial primary key, productname text);
create table quotations(quotationid serial primary key, customerid int, orderdate timestamp);
create table quotationitems(quotationitemid serial primary key, quotationid int, productid int, quantity int, unitprice numeric(9,2));

select * from crosstab('
select customername,productname as bucket,sum(quantity) as bucketvalue
from quotationitems a join quotations b using(quotationid)
join customers c using(customerid)
join sales.products d using (productid)
where orderdate between ''1/1/2009'' and ''1/1/2010''
and producttypeid=3D1
group by customername,productname order by productname',
'select productname from sales.products where producttypeid=3D1 order by productname')
as rpt(customername text,"ATX" int,
"CM-A510" int,
"CM-F82" int,
"CM-i586" int,
"CM-i686B" int,
"CM-i686M" int,
"CM-iAM" int,
"CM-iGLX" int,
"CM-iPM" int,
"CM-iTC" int,
"CM-T3530" int,
"CM-X255" int,
"CM-X270" int,
"CM-X300" int,
"CM-XAM" int
)
order by customername


On 12/14/2010 10:27 AM, Filip Rembia=C5=82kowski wrote:
please show

- source data structures (in form of CREATE TABLE please)

- actual whole query that creates duplicates





2010/12/14 Sim Zacks <sim@compulab.co.il>
postgres 8.2.17

I am trying out the crosstab function (tablefunc contrib) for reporting needs and I'm having a problem.
I have customers and products and the data is the quantity purchased. I am grouping by customername, productname in the source sql. My category sql depends if I want the products or customers to be the columns.

When I make customers the rows and products the columns, it works fine. But when I make customers the columns and products the rows, there are duplicate product rows.

Is there a way to group the product rows so that the data results come back correct?

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsq= l-general



--------------020607000107090608060808--