pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
crosstab function
23+ messages / 11 participants
[nested] [flat]

* crosstab function
@ 2010-12-14 07:17  Sim Zacks <sim@compulab.co.il>
  0 siblings, 1 reply; 23+ messages in thread

From: Sim Zacks @ 2010-12-14 07:17 UTC (permalink / raw)
  To: pgsql-general

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?



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

* Re: crosstab function
@ 2010-12-14 08:27  Filip Rembiałkowski <filip.rembialkowski@gmail.com>
  parent: Sim Zacks <sim@compulab.co.il>
  0 siblings, 1 reply; 23+ messages in thread

From: Filip Rembiałkowski @ 2010-12-14 08:27 UTC (permalink / raw)
  To: Sim Zacks <sim@compulab.co.il>; +Cc: pgsql-general

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/pgsql-general
>

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

* Re: crosstab function
@ 2010-12-14 10:45  Sim Zacks <sim@compulab.co.il>
  parent: Filip Rembiałkowski <filip.rembialkowski@gmail.com>
  0 siblings, 2 replies; 23+ messages in thread

From: Sim Zacks @ 2010-12-14 10:45 UTC (permalink / raw)
  To: Filip Rembiałkowski <filip.rembialkowski@gmail.com>; +Cc: pgsql-general

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=1
group by customername,productname order by productname',
'select productname from sales.products where producttypeid=1 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łkowski 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 <mailto: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
>     <mailto:pgsql-general@postgresql.org>)
>     To make changes to your subscription:
>     http://www.postgresql.org/mailpref/pgsql-general
>
>

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

* Re: crosstab function
@ 2010-12-14 13:09  Sim Zacks <sim@compulab.co.il>
  parent: Sim Zacks <sim@compulab.co.il>
  1 sibling, 0 replies; 23+ messages in thread

From: Sim Zacks @ 2010-12-14 13:09 UTC (permalink / raw)
  To: Filip Rembiałkowski <filip.rembialkowski@gmail.com>; +Cc: pgsql-general

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=1
> group by customername,productname order by productname',
> 'select productname from sales.products where producttypeid=1 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łkowski 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 <mailto: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
>>     <mailto:pgsql-general@postgresql.org>)
>>     To make changes to your subscription:
>>     http://www.postgresql.org/mailpref/pgsql-general
>>
>>
>

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

* Re: crosstab function
@ 2010-12-14 15:00  Filip Rembiałkowski <filip.rembialkowski@gmail.com>
  parent: Sim Zacks <sim@compulab.co.il>
  1 sibling, 1 reply; 23+ messages in thread

From: Filip Rembiałkowski @ 2010-12-14 15:00 UTC (permalink / raw)
  To: Sim Zacks <sim@compulab.co.il>; +Cc: pgsql-general

http://www.postgresql.org/docs/8.4/interactive/tablefunc.html says:


> The crosstab function produces one output row for each consecutive group
> of input rows with the same row_name value. It fills the output valuecolumns, left to right, with the
> value fields from these rows. If there are fewer rows in a group than
> there are output value columns, the extra output columns are filled with
> nulls; if there are more rows, the extra input rows are skipped.
>
> In practice the SQL query *should always specify ORDER BY 1,2* to ensure
> that the input rows are properly ordered, that is, values with the same
> row_name are brought together and correctly ordered within the row. Notice
> that crosstab itself does not pay any attention to the second column of
> the query result; it's just there to be ordered by, to control the order in
> which the third-column values appear across the page.
>

I don't have time to verify this but I guess this is your problem, try and
test
"group by customername,productname order by customername, productname"
instead of
"group by customername,productname order by productname"

HTH

Filip




W dniu 14 grudnia 2010 11:45 użytkownik Sim Zacks <sim@compulab.co.il>napisał:

>  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=1
> group by customername,productname order by productname',
> 'select productname from sales.products where producttypeid=1 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łkowski 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/pgsql-general
>>
>
>
>

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

* Re: crosstab function
@ 2010-12-14 15:17  Sim Zacks <sim@compulab.co.il>
  parent: Filip Rembiałkowski <filip.rembialkowski@gmail.com>
  0 siblings, 0 replies; 23+ messages in thread

From: Sim Zacks @ 2010-12-14 15:17 UTC (permalink / raw)
  To: Filip Rembiałkowski <filip.rembialkowski@gmail.com>; +Cc: pgsql-general

That did it.It's the smallest things that always kill you.


Thanks

Sim


On 12/14/2010 05:00 PM, Filip Rembiałkowski wrote:

>
> http://www.postgresql.org/docs/8.4/interactive/tablefunc.html says:
>
>     The |crosstab| function produces one output row for each
>     consecutive group of input rows with the same row_name value. It
>     fills the output value columns, left to right, with the value
>     fields from these rows. If there are fewer rows in a group than
>     there are output value columns, the extra output columns are
>     filled with nulls; if there are more rows, the extra input rows
>     are skipped.
>
>     In practice the SQL query *should always specify ORDER BY 1,2* to
>     ensure that the input rows are properly ordered, that is, values
>     with the same row_name are brought together and correctly ordered
>     within the row. Notice that |crosstab| itself does not pay any
>     attention to the second column of the query result; it's just
>     there to be ordered by, to control the order in which the
>     third-column values appear across the page.
>
>
> I don't have time to verify this but I guess this is your problem, try 
> and test
> "group by customername,productname order by customername, productname"
> instead of
> "group by customername,productname order by productname"
>
> HTH
>
> Filip
>
>
>
>
> W dniu 14 grudnia 2010 11:45 użytkownik Sim Zacks <sim@compulab.co.il 
> <mailto:sim@compulab.co.il>> napisał:
>
>     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=1
>     group by customername,productname order by productname',
>     'select productname from sales.products where producttypeid=1
>     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łkowski 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 <mailto: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
>>         <mailto:pgsql-general@postgresql.org>)
>>         To make changes to your subscription:
>>         http://www.postgresql.org/mailpref/pgsql-general
>>
>>
>
>

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

* Crosstab function
@ 2014-05-05 06:49  Hengky Liwandouw <hengkyliwandouw@gmail.com>
  0 siblings, 1 reply; 23+ messages in thread

From: Hengky Liwandouw @ 2014-05-05 06:49 UTC (permalink / raw)
  To: pgsql-general

Hi Friends, 

 

Could somebody help me with crosstab function ?  I have warehouse table:

 

CREATE TABLE tblwarehouse (

    id integer NOT NULL,

    warehousename character varying(20)

);

 

COPY tblwarehouse (id, warehousename) FROM stdin;

2     OFFICE

3     STORE2

\.

 

And product table :

 

CREATE TABLE tblproduct (

    id serial NOT NULL,

    produkid text,

    warehouseid integer,

    onhand integer

);

 

COPY tblproduct (produkid, warehouseid, onhand) FROM stdin;

2791404000014     2     10

2791404000021     3     10

2791404000014     3     45

\.

 

I need crosstab function to display record from tblproduct like this :

 

   PRODUKID    | OFFICE | STORE2 | TOTAL

---------------+--------+--------+ ------

 2791404000014 |     10 |     45 |   55

 2791404000021 |      0 |     10 |   10

 

The crosstab warehouse column name (OFFICE & Store2) is taken from
tblwarehouse so when user add warehouse, crosstab column name will change
automatically. And also each row has total qty.

 

 

 

Thanks in advance !

 

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

* Re: Crosstab function
@ 2014-05-05 14:25  David G Johnston <david.g.johnston@gmail.com>
  parent: Hengky Liwandouw <hengkyliwandouw@gmail.com>
  0 siblings, 1 reply; 23+ messages in thread

From: David G Johnston @ 2014-05-05 14:25 UTC (permalink / raw)
  To: pgsql-general

Hengky Lie wrote
> The crosstab warehouse column name (OFFICE & Store2) is taken from
> tblwarehouse so when user add warehouse, crosstab column name will change
> automatically. And also each row has total qty.

In what programming language?  The only way to do this is to dynamically
construct the appropriate query, with the correct number of columns,
on-the-fly in the client application and send it as a normal query to the
server.  There is no pure SQL solution.

For the total column you will need a virtual warehouse that holds those
values.  Likely the easiest way to get that will be to UNION ALL the main
real warehouse query and another query the groups by product and sum-counts
that values from the individual warehouses.

IIRC you've already been shown how to write the basic crosstab query; this
really isn't any different but you will need procedural logic and some way
to dynamically build a SQL query string based upon how many warehouses you
have at the time you run the query.

I am assuming you know how to write the basic join query to get the general
form needed for the real warehouse data.

David J.



--
View this message in context: http://postgresql.1045698.n5.nabble.com/Crosstab-function-tp5802402p5802456.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


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



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

* Re: Crosstab function
@ 2014-05-06 02:37  Hengky Liwandouw <hengkyliwandouw@gmail.com>
  parent: David G Johnston <david.g.johnston@gmail.com>
  0 siblings, 2 replies; 23+ messages in thread

From: Hengky Liwandouw @ 2014-05-06 02:37 UTC (permalink / raw)
  To: pgsql-general

Hi David,

Are you sure that there is no pure sql solution for this ? 

I think (with my very limited postgres knowledge), function can solve this.

So far i can use command:

select *
from crosstab
(
  'select produkid, warehouseid, onhand
   from tblproduct order by 1',
  'select distinct warehouseid from tblproduct order by 1'
)
as ct (produkid text, office int, store2 int);

and I have this result :

   PRODUKID    | OFFICE | STORE2 |
---------------+--------+--------+
 2791404000014 |     10 |     45 |  
 2791404000021 |        |     10 |

The problem is the column header is static. If I have new warehouse, I
should manually add it in the column header.

IF I use command : select 'Produk ID text, ' || array_to_string(array(select
warehousename from tblwarehouse), ' int, ') || ' int';

I can get : "Produk ID text, OFFICE int, STORE2 int"

Which is the column header I need but I really have no idea how to use this
as column header.

Anyway, If i can't do this in postgres, I will try to build sql string in
the client application (Windev) and send the fixed sql to the server

Thanks




-----Original Message-----
From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of David G Johnston
Sent: Monday, May 05, 2014 10:25 PM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] Crosstab function

Hengky Lie wrote
> The crosstab warehouse column name (OFFICE & Store2) is taken from 
> tblwarehouse so when user add warehouse, crosstab column name will 
> change automatically. And also each row has total qty.

In what programming language?  The only way to do this is to dynamically
construct the appropriate query, with the correct number of columns,
on-the-fly in the client application and send it as a normal query to the
server.  There is no pure SQL solution. 

For the total column you will need a virtual warehouse that holds those
values.  Likely the easiest way to get that will be to UNION ALL the main
real warehouse query and another query the groups by product and sum-counts
that values from the individual warehouses.

IIRC you've already been shown how to write the basic crosstab query; this
really isn't any different but you will need procedural logic and some way
to dynamically build a SQL query string based upon how many warehouses you
have at the time you run the query.

I am assuming you know how to write the basic join query to get the general
form needed for the real warehouse data.

David J.





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



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

* Re: Crosstab function
@ 2014-05-06 02:49  Paul Jungwirth <pj@illuminatedcomputing.com>
  parent: Hengky Liwandouw <hengkyliwandouw@gmail.com>
  1 sibling, 1 reply; 23+ messages in thread

From: Paul Jungwirth @ 2014-05-06 02:49 UTC (permalink / raw)
  To: Hengky Liwandouw <hengkyliwandouw@gmail.com>; +Cc: pgsql-general

> Are you sure that there is no pure sql solution for this ?

There is no pure SQL solution because a SQL query always gives a fixed
number of columns. You could compose the SQL in your client app and
vary the columns by the current warehouses. Or you could say GROUP BY
produkit, tblwarehouse.id and rearrange the result client-side. I'd
say those are the practical approaches, but if they aren't possible,
you may also be able to use Postgres's array feature, so that your
result columns are:

produkid
warehouse_totals[]
total

Someone asked a similar question about a year ago, and I wrote up how
to solve it with arrays and a recursive CTE here:

http://www.illuminatedcomputing.com/posts/2013/03/fun-postgres-puzzle/

Good luck,
Paul


On Mon, May 5, 2014 at 7:37 PM, Hengky Liwandouw
<hengkyliwandouw@gmail.com> wrote:
> Hi David,
>
> Are you sure that there is no pure sql solution for this ?
>
> I think (with my very limited postgres knowledge), function can solve this.
>
> So far i can use command:
>
> select *
> from crosstab
> (
>   'select produkid, warehouseid, onhand
>    from tblproduct order by 1',
>   'select distinct warehouseid from tblproduct order by 1'
> )
> as ct (produkid text, office int, store2 int);
>
> and I have this result :
>
>    PRODUKID    | OFFICE | STORE2 |
> ---------------+--------+--------+
>  2791404000014 |     10 |     45 |
>  2791404000021 |        |     10 |
>
> The problem is the column header is static. If I have new warehouse, I
> should manually add it in the column header.
>
> IF I use command : select 'Produk ID text, ' || array_to_string(array(select
> warehousename from tblwarehouse), ' int, ') || ' int';
>
> I can get : "Produk ID text, OFFICE int, STORE2 int"
>
> Which is the column header I need but I really have no idea how to use this
> as column header.
>
> Anyway, If i can't do this in postgres, I will try to build sql string in
> the client application (Windev) and send the fixed sql to the server
>
> Thanks
>
>
>
>
> -----Original Message-----
> From: pgsql-general-owner@postgresql.org
> [mailto:pgsql-general-owner@postgresql.org] On Behalf Of David G Johnston
> Sent: Monday, May 05, 2014 10:25 PM
> To: pgsql-general@postgresql.org
> Subject: Re: [GENERAL] Crosstab function
>
> Hengky Lie wrote
>> The crosstab warehouse column name (OFFICE & Store2) is taken from
>> tblwarehouse so when user add warehouse, crosstab column name will
>> change automatically. And also each row has total qty.
>
> In what programming language?  The only way to do this is to dynamically
> construct the appropriate query, with the correct number of columns,
> on-the-fly in the client application and send it as a normal query to the
> server.  There is no pure SQL solution.
>
> For the total column you will need a virtual warehouse that holds those
> values.  Likely the easiest way to get that will be to UNION ALL the main
> real warehouse query and another query the groups by product and sum-counts
> that values from the individual warehouses.
>
> IIRC you've already been shown how to write the basic crosstab query; this
> really isn't any different but you will need procedural logic and some way
> to dynamically build a SQL query string based upon how many warehouses you
> have at the time you run the query.
>
> I am assuming you know how to write the basic join query to get the general
> form needed for the real warehouse data.
>
> David J.
>
>
>
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general



-- 
_________________________________
Pulchritudo splendor veritatis.


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



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

* Re: Crosstab function
@ 2014-05-06 02:58  Dann Corbit <DCorbit@connx.com>
  parent: Paul Jungwirth <pj@illuminatedcomputing.com>
  0 siblings, 0 replies; 23+ messages in thread

From: Dann Corbit @ 2014-05-06 02:58 UTC (permalink / raw)
  To: Paul Jungwirth <pj@illuminatedcomputing.com>; Hengky Liwandouw <hengkyliwandouw@gmail.com>; +Cc: pgsql-general

Why not use the crosstab stuff in contrib?
http://www.postgresql.org/docs/9.3/static/tablefunc.html

Has it been removed or something?

-----Original Message-----
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Paul Jungwirth
Sent: Monday, May 5, 2014 7:49 PM
To: Hengky Liwandouw
Cc: pgsql-general General
Subject: Re: [GENERAL] Crosstab function

> Are you sure that there is no pure sql solution for this ?

There is no pure SQL solution because a SQL query always gives a fixed number of columns. You could compose the SQL in your client app and vary the columns by the current warehouses. Or you could say GROUP BY produkit, tblwarehouse.id and rearrange the result client-side. I'd say those are the practical approaches, but if they aren't possible, you may also be able to use Postgres's array feature, so that your result columns are:

produkid
warehouse_totals[]
total

Someone asked a similar question about a year ago, and I wrote up how to solve it with arrays and a recursive CTE here:

http://www.illuminatedcomputing.com/posts/2013/03/fun-postgres-puzzle/

Good luck,
Paul


On Mon, May 5, 2014 at 7:37 PM, Hengky Liwandouw <hengkyliwandouw@gmail.com> wrote:
> Hi David,
>
> Are you sure that there is no pure sql solution for this ?
>
> I think (with my very limited postgres knowledge), function can solve this.
>
> So far i can use command:
>
> select *
> from crosstab
> (
>   'select produkid, warehouseid, onhand
>    from tblproduct order by 1',
>   'select distinct warehouseid from tblproduct order by 1'
> )
> as ct (produkid text, office int, store2 int);
>
> and I have this result :
>
>    PRODUKID    | OFFICE | STORE2 |
> ---------------+--------+--------+
>  2791404000014 |     10 |     45 |
>  2791404000021 |        |     10 |
>
> The problem is the column header is static. If I have new warehouse, I 
> should manually add it in the column header.
>
> IF I use command : select 'Produk ID text, ' || 
> array_to_string(array(select warehousename from tblwarehouse), ' int, 
> ') || ' int';
>
> I can get : "Produk ID text, OFFICE int, STORE2 int"
>
> Which is the column header I need but I really have no idea how to use 
> this as column header.
>
> Anyway, If i can't do this in postgres, I will try to build sql string 
> in the client application (Windev) and send the fixed sql to the 
> server
>
> Thanks
>
>
>
>
> -----Original Message-----
> From: pgsql-general-owner@postgresql.org
> [mailto:pgsql-general-owner@postgresql.org] On Behalf Of David G 
> Johnston
> Sent: Monday, May 05, 2014 10:25 PM
> To: pgsql-general@postgresql.org
> Subject: Re: [GENERAL] Crosstab function
>
> Hengky Lie wrote
>> The crosstab warehouse column name (OFFICE & Store2) is taken from 
>> tblwarehouse so when user add warehouse, crosstab column name will 
>> change automatically. And also each row has total qty.
>
> In what programming language?  The only way to do this is to 
> dynamically construct the appropriate query, with the correct number 
> of columns, on-the-fly in the client application and send it as a 
> normal query to the server.  There is no pure SQL solution.
>
> For the total column you will need a virtual warehouse that holds 
> those values.  Likely the easiest way to get that will be to UNION ALL 
> the main real warehouse query and another query the groups by product 
> and sum-counts that values from the individual warehouses.
>
> IIRC you've already been shown how to write the basic crosstab query; 
> this really isn't any different but you will need procedural logic and 
> some way to dynamically build a SQL query string based upon how many 
> warehouses you have at the time you run the query.
>
> I am assuming you know how to write the basic join query to get the 
> general form needed for the real warehouse data.
>
> David J.
>
>
>
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To 
> make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general



--
_________________________________
Pulchritudo splendor veritatis.


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

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


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

* Re: Crosstab function
@ 2014-05-06 03:00  David G Johnston <david.g.johnston@gmail.com>
  parent: Hengky Liwandouw <hengkyliwandouw@gmail.com>
  1 sibling, 1 reply; 23+ messages in thread

From: David G Johnston @ 2014-05-06 03:00 UTC (permalink / raw)
  To: pgsql-general

Hengky Lie wrote
> Hi David,
> 
> Are you sure that there is no pure sql solution for this ? 
> 
> I think (with my very limited postgres knowledge), function can solve
> this.
> 
> Which is the column header I need but I really have no idea how to use
> this
> as column header.
> 
> Anyway, If i can't do this in postgres, I will try to build sql string in
> the client application (Windev) and send the fixed sql to the server

Positive.

You could build the necessary string in a pl/pgsql language function but you
would still have trouble getting the data out of the function the way you
want; unless you output a single text column no matter the original data (
basically output a cvs version of the crosstab result).

There is no dynamic execution in SQL so even though you can get a string
that looks like what you want you cannot do anything with it.  Only data is
allowed to be dynamic; the engine has to know the names and types of all
schema objects before it can start so there is no way a query can retrieve
these things from its own data. It's the whole cart-horse thing...

The solution is to build the query in the client and send it.  Make sure you
look at the various "quote_" functions in order to minimize the risk of SQL
injection attacks.  These are especially useful for pl/pgsql functions but
you might be able to use them in your first query so that you can avoid
coding all the quoting and escaping rules into your application.  At minimum
double-quote all your identifiers and make sure there are no unescaped
embedded double-quotes.  If the only variables are from data in tables
putting constraints on those tables would probably be useful as well - you
limit valid identifiers but minimized risk of bad data causing an issue.

David J.








--
View this message in context: http://postgresql.1045698.n5.nabble.com/Crosstab-function-tp5802402p5802601.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


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



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

* Re: Crosstab function
@ 2014-05-06 03:37  Hengky Liwandouw <hengkyliwandouw@gmail.com>
  parent: David G Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 23+ messages in thread

From: Hengky Liwandouw @ 2014-05-06 03:37 UTC (permalink / raw)
  To: 'David G Johnston' <david.g.johnston@gmail.com>; pgsql-general

Very Clear instruction !

Thank you very much David. I will do it in my client app and follow your
guidance.


-----Original Message-----
From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of David G Johnston
Sent: Tuesday, May 06, 2014 11:01 AM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] Crosstab function

Hengky Lie wrote
> Hi David,
> 
> Are you sure that there is no pure sql solution for this ? 
> 
> I think (with my very limited postgres knowledge), function can solve
> this.
> 
> Which is the column header I need but I really have no idea how to use
> this
> as column header.
> 
> Anyway, If i can't do this in postgres, I will try to build sql string in
> the client application (Windev) and send the fixed sql to the server

Positive.

You could build the necessary string in a pl/pgsql language function but you
would still have trouble getting the data out of the function the way you
want; unless you output a single text column no matter the original data (
basically output a cvs version of the crosstab result).

There is no dynamic execution in SQL so even though you can get a string
that looks like what you want you cannot do anything with it.  Only data is
allowed to be dynamic; the engine has to know the names and types of all
schema objects before it can start so there is no way a query can retrieve
these things from its own data. It's the whole cart-horse thing...

The solution is to build the query in the client and send it.  Make sure you
look at the various "quote_" functions in order to minimize the risk of SQL
injection attacks.  These are especially useful for pl/pgsql functions but
you might be able to use them in your first query so that you can avoid
coding all the quoting and escaping rules into your application.  At minimum
double-quote all your identifiers and make sure there are no unescaped
embedded double-quotes.  If the only variables are from data in tables
putting constraints on those tables would probably be useful as well - you
limit valid identifiers but minimized risk of bad data causing an issue.

David J.








--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Crosstab-function-tp5802402p5802601.
html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


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



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



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

* Re: Crosstab function
@ 2014-05-07 11:32  Sim Zacks <sim@compulab.co.il>
  parent: Hengky Liwandouw <hengkyliwandouw@gmail.com>
  0 siblings, 1 reply; 23+ messages in thread

From: Sim Zacks @ 2014-05-07 11:32 UTC (permalink / raw)
  To: pgsql-general

<html style="direction: ltr;">
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
    <style type="text/css">body p { margin-bottom: 0cm; margin-top: 0pt; } </style>
  </head>
  <body style="direction: ltr;"
    bidimailui-detected-decoding-type="latin-charset" bgcolor="#FFFFFF"
    text="#000000">
    <div class="moz-cite-prefix">What I have done in the past to build a
      generic reporting application is to have the function write the
      results you want in a table and return the tablename and then have
      the client code call select * from that table. <br>
      <br>
      My standard report tablename is tblreport || userid;<br>
      It gets dropped at the beginning of the function, so it is
      basically a temp table that doesn't interfere with any other
      users.<br>
      <br>
      Example:<br>
      &nbsp;&nbsp;&nbsp; execute 'drop table if exists reports.tblreport' || v_userid ;<br>
      &nbsp;&nbsp;&nbsp; execute 'drop sequence if exists reports.tblreport' ||
      v_userid || '_id_seq; create sequence reports.tblreport' ||
      v_userid || '_id_seq';<br>
      &nbsp;&nbsp;&nbsp; v_sql=' create table reports.tblreport' || v_userid || ' as ';<br>
      <br>
      Sim<br>
      <br>
      On 05/06/2014 06:37 AM, Hengky Liwandouw wrote:<br>
    </div>
    <blockquote cite="mid:004601cf68dc$848e8420$8dab8c60$@com"
      type="cite">
      <pre wrap="">Very Clear instruction !

Thank you very much David. I will do it in my client app and follow your
guidance.


-----Original Message-----
From: <a class="moz-txt-link-abbreviated" href="mailto:pgsql-general-owner@postgresql.org">pgsql-general-owner@postgresql.org</a>
[<a class="moz-txt-link-freetext" href="mailto:pgsql-general-owner@postgresql.org">mailto:pgsql-general-owner@postgresql.org</a>] On Behalf Of David G Johnston
Sent: Tuesday, May 06, 2014 11:01 AM
To: <a class="moz-txt-link-abbreviated" href="mailto:pgsql-general@postgresql.org">pgsql-general@postgresql.org</a>
Subject: Re: [GENERAL] Crosstab function

Hengky Lie wrote
</pre>
      <blockquote type="cite">
        <pre wrap="">Hi David,

Are you sure that there is no pure sql solution for this ? 

I think (with my very limited postgres knowledge), function can solve
this.

Which is the column header I need but I really have no idea how to use
this
as column header.

Anyway, If i can't do this in postgres, I will try to build sql string in
the client application (Windev) and send the fixed sql to the server
</pre>
      </blockquote>
      <pre wrap="">
Positive.

You could build the necessary string in a pl/pgsql language function but you
would still have trouble getting the data out of the function the way you
want; unless you output a single text column no matter the original data (
basically output a cvs version of the crosstab result).

There is no dynamic execution in SQL so even though you can get a string
that looks like what you want you cannot do anything with it.  Only data is
allowed to be dynamic; the engine has to know the names and types of all
schema objects before it can start so there is no way a query can retrieve
these things from its own data. It's the whole cart-horse thing...

The solution is to build the query in the client and send it.  Make sure you
look at the various "quote_" functions in order to minimize the risk of SQL
injection attacks.  These are especially useful for pl/pgsql functions but
you might be able to use them in your first query so that you can avoid
coding all the quoting and escaping rules into your application.  At minimum
double-quote all your identifiers and make sure there are no unescaped
embedded double-quotes.  If the only variables are from data in tables
putting constraints on those tables would probably be useful as well - you
limit valid identifiers but minimized risk of bad data causing an issue.

David J.








--
View this message in context:
<a class="moz-txt-link-freetext" href="http://postgresql.1045698.n5.nabble.com/Crosstab-function-tp5802402p5802601">http://postgresq...;.
html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


</pre>
    </blockquote>
    <br>
  </body>
</html>




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

* Re: Crosstab function
@ 2014-05-09 02:44  Hengky Liwandouw <hengkyliwandouw@gmail.com>
  parent: Sim Zacks <sim@compulab.co.il>
  0 siblings, 0 replies; 23+ messages in thread

From: Hengky Liwandouw @ 2014-05-09 02:44 UTC (permalink / raw)
  To: 'Sim Zacks' <sim@compulab.co.il>; pgsql-general

Thanks Sim, a very usefull information.

 

 

From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of Sim Zacks
Sent: Wednesday, May 07, 2014 7:33 PM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] Crosstab function

 

What I have done in the past to build a generic reporting application is to
have the function write the results you want in a table and return the
tablename and then have the client code call select * from that table. 

My standard report tablename is tblreport || userid;
It gets dropped at the beginning of the function, so it is basically a temp
table that doesn't interfere with any other users.

Example:
    execute 'drop table if exists reports.tblreport' || v_userid ;
    execute 'drop sequence if exists reports.tblreport' || v_userid ||
'_id_seq; create sequence reports.tblreport' || v_userid || '_id_seq';
    v_sql=' create table reports.tblreport' || v_userid || ' as ';

Sim



 

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

* crosstab function
@ 2019-02-27 01:51  Martin Mueller <martinmueller@northwestern.edu>
  0 siblings, 4 replies; 23+ messages in thread

From: Martin Mueller @ 2019-02-27 01:51 UTC (permalink / raw)
  To: pgsql-general

I run Postgres 10.5.  I understand that there is something called tablefunc and it includes a crosstab function. On Stack Overflow I learn that you import this function. But from where and how?  The Postgres documentation is quite clear and intelligible to a retired English professor like me, but there is nothing in the Postgres documentation about how to do the import, and a search for ‘import modules’ yields nothing.

I tried to emulate a Stack overflow query that does what I want to do, but got an error message saying that the crosstab function doesn’t exist. I tried

CREATE EXTENSION IF NOT EXISTS tablefunc;

but it did nothing. It doesn’t seem to work  as import statements in Python do
-----
Martin Mueller
Professor emeritus of English and Classics
Northwestern University




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

* Re: crosstab function
@ 2019-02-27 02:04  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Martin Mueller <martinmueller@northwestern.edu>
  3 siblings, 1 reply; 23+ messages in thread

From: Adrian Klaver @ 2019-02-27 02:04 UTC (permalink / raw)
  To: Martin Mueller <martinmueller@northwestern.edu>; pgsql-general

On 2/26/19 5:51 PM, Martin Mueller wrote:
> I run Postgres 10.5.  I understand that there is something called 
> tablefunc and it includes a crosstab function. On Stack Overflow I learn 
> that you import this function. But from where and how?  The Postgres 
> documentation is quite clear and intelligible to a retired English 
> professor like me, but there is nothing in the Postgres documentation 
> about how to do the import, and a search for ‘import modules’ yields 
> nothing.

tablefunc is a contrib module so we need to know something information 
about your Postgres setup:

1) Your OS ?

2) How did you install Postgres?

> 
> I tried to emulate a Stack overflow query that does what I want to do, 
> but got an error message saying that the crosstab function doesn’t 
> exist. I tried
> 
> CREATEEXTENSION IFNOTEXISTStablefunc;
> 
> but it did nothing. It doesn’t seem to work  as import statements in 
> Python do

The above depends on the module code actually existing where CREATE 
EXTENSION can find it. Whether the module exists and where it exists 
depends on the answers to the questions above.

> 
> -----
> 
> Martin Mueller
> Professor emeritus of English and Classics
> 
> Northwestern University
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




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

* Re: crosstab function
@ 2019-02-27 02:07  Andrew Gierth <andrew@tao11.riddles.org.uk>
  parent: Martin Mueller <martinmueller@northwestern.edu>
  3 siblings, 0 replies; 23+ messages in thread

From: Andrew Gierth @ 2019-02-27 02:07 UTC (permalink / raw)
  To: Martin Mueller <martinmueller@northwestern.edu>; +Cc: pgsql-general

>>>>> "Martin" == Martin Mueller <martinmueller@northwestern.edu> writes:

 Martin> I run Postgres 10.5. I understand that there is something
 Martin> called tablefunc and it includes a crosstab function. On Stack
 Martin> Overflow I learn that you import this function. But from where
 Martin> and how? The Postgres documentation is quite clear and
 Martin> intelligible to a retired English professor like me, but there
 Martin> is nothing in the Postgres documentation about how to do the
 Martin> import, and a search for ‘import modules’ yields nothing.

 Martin> I tried to emulate a Stack overflow query that does what I want
 Martin> to do, but got an error message saying that the crosstab
 Martin> function doesn’t exist. I tried

 Martin> CREATE EXTENSION IF NOT EXISTS tablefunc;

 Martin> but it did nothing. It doesn’t seem to work as import
 Martin> statements in Python do

CREATE EXTENSION causes the functions to be defined in the current
database; that's the only "import" that is needed. In psql, you can do
this:

\df+ *.crosstab*

to see what functions of that name are defined and what schema they are
in (should be "public" by default). Likewise  \dx  lists installed
extensions in the current database.

Remember that you need to do the CREATE EXTENSION command actually in
the database in which you want to use the functions, not in any other
database.

Errors about functions not existing are usually caused by a problem with
the number or type of parameters, since function names aren't unique
(overloaded functions are allowed). You didn't tell us the actual error
you got, so we can't say exactly what the problem is there.

-- 
Andrew (irc:RhodiumToad)




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

* Re: crosstab function
@ 2019-02-27 02:10  Martin Mueller <martinmueller@northwestern.edu>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 2 replies; 23+ messages in thread

From: Martin Mueller @ 2019-02-27 02:10 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

Thank you for the prompt and clear answer. I work with a Mac (OS 10.4.3) and I used the PostgresApp to install it. I am a very primitive user of Postgres and think of it as Microsoft Access on Steroids. I access it via Aqua Data, Studio, but I don't think that makes any difference

On 2/26/19, 8:04 PM, "Adrian Klaver" <adrian.klaver@aklaver.com> wrote:

    On 2/26/19 5:51 PM, Martin Mueller wrote:
    > I run Postgres 10.5.  I understand that there is something called 
    > tablefunc and it includes a crosstab function. On Stack Overflow I learn 
    > that you import this function. But from where and how?  The Postgres 
    > documentation is quite clear and intelligible to a retired English 
    > professor like me, but there is nothing in the Postgres documentation 
    > about how to do the import, and a search for ‘import modules’ yields 
    > nothing.
    
    tablefunc is a contrib module so we need to know something information 
    about your Postgres setup:
    
    1) Your OS ?
    
    2) How did you install Postgres?
    
    > 
    > I tried to emulate a Stack overflow query that does what I want to do, 
    > but got an error message saying that the crosstab function doesn’t 
    > exist. I tried
    > 
    > CREATEEXTENSION IFNOTEXISTStablefunc;
    > 
    > but it did nothing. It doesn’t seem to work  as import statements in 
    > Python do
    
    The above depends on the module code actually existing where CREATE 
    EXTENSION can find it. Whether the module exists and where it exists 
    depends on the answers to the questions above.
    
    > 
    > -----
    > 
    > Martin Mueller
    > Professor emeritus of English and Classics
    > 
    > Northwestern University
    > 
    
    
    -- 
    Adrian Klaver
    adrian.klaver@aklaver.com
    



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

* Re: crosstab function
@ 2019-02-27 03:33  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Martin Mueller <martinmueller@northwestern.edu>
  1 sibling, 0 replies; 23+ messages in thread

From: Adrian Klaver @ 2019-02-27 03:33 UTC (permalink / raw)
  To: Martin Mueller <martinmueller@northwestern.edu>; pgsql-general

On 2/26/19 6:10 PM, Martin Mueller wrote:
> Thank you for the prompt and clear answer. I work with a Mac (OS 10.4.3) and I used the PostgresApp to install it. I am a very primitive user of Postgres and think of it as Microsoft Access on Steroids. I access it via Aqua Data, Studio, but I don't think that makes any difference

My guess is PostgresApp does not include the tablefunc extension. Per 
Andrews instructions it would helpful to know what error you got when 
you tried to CREATE EXTENSION tablefunc;

> 
> On 2/26/19, 8:04 PM, "Adrian Klaver" <adrian.klaver@aklaver.com> wrote:
> 
>      On 2/26/19 5:51 PM, Martin Mueller wrote:
>      > I run Postgres 10.5.  I understand that there is something called
>      > tablefunc and it includes a crosstab function. On Stack Overflow I learn
>      > that you import this function. But from where and how?  The Postgres
>      > documentation is quite clear and intelligible to a retired English
>      > professor like me, but there is nothing in the Postgres documentation
>      > about how to do the import, and a search for ‘import modules’ yields
>      > nothing.
>      
>      tablefunc is a contrib module so we need to know something information
>      about your Postgres setup:
>      
>      1) Your OS ?
>      
>      2) How did you install Postgres?
>      
>      >
>      > I tried to emulate a Stack overflow query that does what I want to do,
>      > but got an error message saying that the crosstab function doesn’t
>      > exist. I tried
>      >
>      > CREATEEXTENSION IFNOTEXISTStablefunc;
>      >
>      > but it did nothing. It doesn’t seem to work  as import statements in
>      > Python do
>      
>      The above depends on the module code actually existing where CREATE
>      EXTENSION can find it. Whether the module exists and where it exists
>      depends on the answers to the questions above.
>      
>      >
>      > -----
>      >
>      > Martin Mueller
>      > Professor emeritus of English and Classics
>      >
>      > Northwestern University
>      >
>      
>      
>      --
>      Adrian Klaver
>      adrian.klaver@aklaver.com
>      
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




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

* Re: crosstab function
@ 2019-02-27 04:42  Ron <ronljohnsonjr@gmail.com>
  parent: Martin Mueller <martinmueller@northwestern.edu>
  3 siblings, 0 replies; 23+ messages in thread

From: Ron @ 2019-02-27 04:42 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2/26/19 7:51 PM, Martin Mueller wrote:
>
> I run Postgres 10.5.  I understand that there is something called 
> tablefunc and it includes a crosstab function. On Stack Overflow I learn 
> that you import this function. But from where and how?  The Postgres 
> documentation is quite clear and intelligible to a retired English 
> professor like me, but there is nothing in the Postgres documentation 
> about how to do the import, and a search for ‘import modules’ yields nothing.
>
> I tried to emulate a Stack overflow query that does what I want to do, but 
> got an error message saying that the crosstab function doesn’t exist. I tried
>
> CREATEEXTENSION IFNOTEXISTStablefunc;
>
> but it did nothing. It doesn’t seem to work  as import statements in Python do
>

Extensions are short bits of sql that create functions for you, and point to 
"shared object" executable files.  Quite literally, it's extending postgres.

Thus, the actual executable file needs to be somewhere on the computer's 
hard drive (specifically, where Postgres expects it to be for your OS).

In the Linux world, for example, there's typically a package you install 
named something like postgresql96-contrib-9.6.9-blahblah.rpm.  You'd install 
that using the OS' package manager, restart postgres and then CREATE 
EXTENSION should work.

You'll have to determine whether PostgreApp comes with "contribs". If not, 
you'll have to use another source for Pg, like MacPorts.

-- 
Angular momentum makes the world go 'round.

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

* Re: crosstab function
@ 2019-02-27 04:52  Ron <ronljohnsonjr@gmail.com>
  parent: Martin Mueller <martinmueller@northwestern.edu>
  1 sibling, 0 replies; 23+ messages in thread

From: Ron @ 2019-02-27 04:52 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2/26/19 8:10 PM, Martin Mueller wrote:
> Thank you for the prompt and clear answer. I work with a Mac (OS 10.4.3) and I used the PostgresApp to install it. I am a very primitive user of Postgres and think of it as Microsoft Access on Steroids. I access it via Aqua Data, Studio, but I don't think that makes any difference

Access is an application design tool with a built-in engine that's good for 
storing "small" amounts of data.

Postgresql is a database management system suitable for storing and 
replicating billions and billions and billions of large amounts of data, and 
analyzing it in a myriad of ways.  It requires "administration from the 
command line", as you're discovering.

Sincerely,
Ron

-- 
Angular momentum makes the world go 'round.




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

* Re: crosstab function
@ 2019-02-28 11:02  Morris de Oryx <morrisdeoryx@gmail.com>
  parent: Martin Mueller <martinmueller@northwestern.edu>
  3 siblings, 0 replies; 23+ messages in thread

From: Morris de Oryx @ 2019-02-28 11:02 UTC (permalink / raw)
  To: Martin Mueller <martinmueller@northwestern.edu>; +Cc: pgsql-general

Professor Mueller! I believe that we met, long ago. I graduated from your
department in 1984 where I worked closely with the wonderful, late Prof.
Dipple.

Postgres.app is a very easy way to work with Postgres, and it does include
support for tablefunc. If you ever want to check which extensions are
installed, run this line:

select * from pg_available_extensions order by name;

Your code looks correct on the face of it:

CREATE EXTENSION IF NOT EXISTS tablefunc;

Or, if you have set up schemas other than the default "public", you can
install into a specific schema:

CREATE EXTENSION IF NOT EXISTS tablefunc WITH SCHEMA extensions;

If you aren't already using custom schemas...I'll leave it alone for now.

As noted, you're installing into a specific database, so make sure that
you've connected where you expect and are in the database you mean. It's
fairly easy for a tool to default to something other than your custom
database. If it's not clear from the UI, or you just feel like testing by
hand, run this line:

SELECT current_database();

It's worth knowing that a Postgres extension is a packaging system. An
extension may include C code, setup scripts, straight SQL, a variety of
resources. Sometimes, you can open one up and harvest little bits of SQL
you want. For details:

https://www.postgresql.org/docs/10/extend-extensions.html

After a quick googling, it looks like you may be interested in textual
analysis. If so, Postgres has a *lot* of tools that can be of assistance.
Within Postgres.app, I can see at least the following:

citext
If you haven't noticed, and care, Postgres' default varchar/text field type
is case-sensitive. Ugh. The citext extension is searchable
case-insensitively out of the box. I use this for alpha/text fields when I
don't care about case-sensitive searches. For where that is, read
"everywhere".

fuzzystrmatch
https://www.postgresql.org/docs/10/fuzzystrmatch.html

Basic, name/word-matching fuzzy algorithms. The "phonetic" ones are not so
great, but Levenshtein is quite good, if a bit expensive to run.

Full Text Search
Huge subject, lots of options, modern versions of Postgres are quite strong
here.

unaccent
The description reads, "text search dictionary that removes accents." I
haven't needed it, and wonder if specifying a collation might not work
better?

pg_pgtrgm
https://www.postgresql.org/docs/10/pgtrgm.html

*N*-grams of length 3. This is a fantastic tool. N-grams have proven
themselves down the years for fuzzy string matching in multiple domains.
I've mostly used it historically on name data, but it works well on larger
text blocks as well. This holds up with many languages other than English.
It's pretty easy to use this extension.

There's another appealing extension named pg_similarity that includes a
huge range of text comparison and fuzzy ranking tools, but I do not know
how to compile it for macOS or get it to run with Postgres.app. If you are
interested in a specific algorithm, many are easily implemented in a SQL
statement or stored function. For example, Jaccard (and similar) ranking
metrics are produced arithmetically, so they're easy to reimplement.

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


end of thread, other threads:[~2019-02-28 11:02 UTC | newest]

Thread overview: 23+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-12-14 07:17 crosstab function Sim Zacks <sim@compulab.co.il>
2010-12-14 08:27 ` Filip Rembiałkowski <filip.rembialkowski@gmail.com>
2010-12-14 10:45   ` Sim Zacks <sim@compulab.co.il>
2010-12-14 13:09     ` Sim Zacks <sim@compulab.co.il>
2010-12-14 15:00     ` Filip Rembiałkowski <filip.rembialkowski@gmail.com>
2010-12-14 15:17       ` Sim Zacks <sim@compulab.co.il>
2014-05-05 06:49 Crosstab function Hengky Liwandouw <hengkyliwandouw@gmail.com>
2014-05-05 14:25 ` Re: Crosstab function David G Johnston <david.g.johnston@gmail.com>
2014-05-06 02:37   ` Re: Crosstab function Hengky Liwandouw <hengkyliwandouw@gmail.com>
2014-05-06 02:49     ` Re: Crosstab function Paul Jungwirth <pj@illuminatedcomputing.com>
2014-05-06 02:58       ` Re: Crosstab function Dann Corbit <DCorbit@connx.com>
2014-05-06 03:00     ` Re: Crosstab function David G Johnston <david.g.johnston@gmail.com>
2014-05-06 03:37       ` Re: Crosstab function Hengky Liwandouw <hengkyliwandouw@gmail.com>
2014-05-07 11:32         ` Re: Crosstab function Sim Zacks <sim@compulab.co.il>
2014-05-09 02:44           ` Re: Crosstab function Hengky Liwandouw <hengkyliwandouw@gmail.com>
2019-02-27 01:51 crosstab function Martin Mueller <martinmueller@northwestern.edu>
2019-02-27 02:04 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-02-27 02:10   ` Martin Mueller <martinmueller@northwestern.edu>
2019-02-27 03:33     ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-02-27 04:52     ` Ron <ronljohnsonjr@gmail.com>
2019-02-27 02:07 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2019-02-27 04:42 ` Ron <ronljohnsonjr@gmail.com>
2019-02-28 11:02 ` Morris de Oryx <morrisdeoryx@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