Coder Social home page Coder Social logo

jtornero / pivotmytable Goto Github PK

View Code? Open in Web Editor NEW
16.0 16.0 9.0 194 KB

PivotMyTable is a PL/Python PosgreSQL function for easy construction of pivot tables, generating the proper code for the crosstab functions from the PostgreSQL extension tablefunc to work.

License: GNU General Public License v3.0

PLpgSQL 100.00%

pivotmytable's People

Contributors

johndegitt avatar jtornero avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pivotmytable's Issues

Change license

Hello, would you consider changing the license to MIT or APACHE 2?

Allow materialized views as input table to pivot.

Pivotmytable function uses 'information_schema' as source to get details about tables and columns but this schema doesn't contain information about materialized views. One should use Postgresql specific catalog tables (e.g. pg_attribute) which are source of information on all relations (tables, views, materialized views, etc...).
The SQL script can be modified according to Erwin Brandstetter advices as described on http://stackoverflow.com/questions/19127561/how-to-introspect-materialized-views.

Questions about null fields.

Hi I love this function although I had a difficult time getting it working for some tables initially.

I had to make the following change to the program to get it working :

            #fieldType = ("numeric(%i, %i)" %(numericFieldWidth, columnScale))
            fieldType = ("double precision")

I am wondering now how I can get NULL values to show up?

    # This trick makes possible to get rid of the null values in the pivoted tables.
    # TODO: Consider to make it optional, with a parameter for it.

    replaceZeros = ['coalesce("{0}",0) as "{0}"'.format(destColumn["columns"]) for destColumn in destColumns]
    replaze0 = ','.join(replaceZeros)

What should I do here to make null values show up instead of '0'

Thanks for the great function and any help.

Next project for me is to pivot varchar fields.

Function not matching

I've installed the pivotmytable into my DB instance, and I've created the example myinfo table, but for some reason I can't execute the query:

# select * from pivotmytable('myinfo','pivotedinfo','player,tool','round','sum');
ERROR:  function pivotmytable(unknown, unknown, unknown, unknown, unknown) does not exist
LINE 1: select * from pivotmytable('myinfo','pivotedinfo','player,to...
                      ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

Do you have any ideas about how to debug this?

Postgres service stops while running pivotmytable

Hi,

I have been using this script for quite a while without any issue. But suddenly this script stopped working and whenever I run this select * from pivotmytable query my postgreSQL database service stops and I am not able create the pivot table. My original table almost has 1 million records, could this be the reason for this issue? Kindly confirm.

Need support for value_field to be a count(*) and not a column

Maybe there is a way to do this, but it's not obvious at first. For example: I'd like to get a count of how many procedures were done to every patient at every hospital.

create table test (patient_id int, hospital_id int, procedure_id int);
insert into test (patient_id, hospital_id, procedure_id)
values 
(1, 100, A),
(1, 200, A),
(1, 300, B),
(1, 200, A),
(2, 100, C),
(2, 200, C),
(3, 100, A)
;

And get the following output:

patient_id | 100 | 200 | 300 
-----------+-----+-----+-----
1          | 1   | 2   | 1
2          | 1   | 1   | 0
3          | 1   | 0   | 0

My first thought was:
select * from pivotmytable('test','pivotedtest','patient_id','hospital_id','procedure_id','count');
but the requirement that value_field be of type int does not make this possible. It would be useful if the count aggregator could count items that are not numeric.

Maybe if I use another query result to it, it might work? select patient_id, hospital_id, count(procedure_id) as procedure_count from test group by 1,2 order by 1,2;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.