hnsl / colpivot Goto Github PK
View Code? Open in Web Editor NEWDynamic row to column pivotation/transpose in Postgres made simple.
Dynamic row to column pivotation/transpose in Postgres made simple.
Hi,
I'm using this function with postgres vertex reactive client. vertx-pg-client.
I'm trying to call this function with parameter but I get cannot be coerced to [] exception,
Can you guide me how can I resolve this issue.
public Completable testStreamWideTagHistoryv1(HistoryReqModel req) {
logger.info("doing stream wide tag query");
return txBegin()
.flatMapCompletable(tx ->
tx.rxPreparedQuery(
"select colpivot('test_output', 'select rt.v as agg_v, rt.t as bucket, rt.tag_id as tagged from rtdata rt where rt.asset_name = $1 ' "
+ ", array['bucket'], array['tagged'], '#.agg_v', null)",
Tuple.of(req.getAssetName()))
.flatMapCompletable(result -> tx.rxCommit()));
}
Hi there. Nice function. Just found it on Stackoverflow...
However, I get duplicate rows in some cases. Unfortunately I could not create a test case... My query:
select colpivot('_test_pivoted', 'select * from input_data.building_elements',
array['type','constr_style'], array['year'], '#.market_share', null);
select * from _test_pivoted order by type, constr_style;
Any hints would be appreciated.
I am trying to use this package, but I am running into complications.
Mainly, the colpivot function returns multiple results. Is there a way to isolate the results that are returned?
i.e if I do this...
select colpivot(params) ---> returns temp_table
select * from temp_table
END TRANSACTION
It will return multiple results --- one for colpivot and one for the temp_table.
If I try this...
with cte as (SELECT colpivot(params), * from some_table
LEFT JOIN some_table on temp_table)
END TRANSACTION
It will not allow the left join because the table does not exist before the query begins.
The only thing I can think is to modify what is being returned from the function. However, I have no idea the return type because the columns are dynamic.
Great function! If the column to be pivoted contains blank spaces the function returns an empty set. If you would include double quotation marks in line 30:
query := query || 'quote_literal("' || quote_ident(col) || '")'
this issue can be circumvented.
from this code
`select * from colpivot('_output', $$
select "StudentEnrollmentID" as a,value, name, "ComponentID" as q, "order" as o
from "university"."CourseSchedulledAssessmentComponent"
LEFT JOIN "university"."AssessmentResultPerComponent" ON "AssessmentResultPerComponent"."ComponentID" = "university"."CourseSchedulledAssessmentComponent"."ID"
where "CourseSchedulledAssessmentComponent"."UniversityID" = 1001 AND "CourseSchedulledAssessmentComponent"."CourseSchedulledID" = 1008
$$, array['a'], array['q'], '#.value', 'sum(o) asc');
select * from _output
`
is it possible to execute this in single statement?
Thanks
Hi, I really appreciate your work since it has solved a problem at my company in which we also needed to pivot an undefined number of results to columns.
Right now I am trying to investigate how can I avoid an ERROR when the table that I am querying is empty:
ERROR: upper bound of FOR loop cannot be null CONTEXT: PL/pgSQL function colpivot(character varying,character varying,character varying[],character varying[],character varying,character varying) line 67 at FOR with integer loop variable
Can you give any help with this ?
It would be good to include a note in the README about mixed-case columns.
The code does double-quote column names, so mixed-case columns can be used.
However, there are two cases where the user has to do the quoting themselves.
Example cross-tabulating grading results by lesson:
SELECT colpivot(
'_test_pivoted',
'select * FROM n_correct_for_course_by_lesson_s(''ANON'')',
array['userCode'],
array['lessonCode'],
'#."percentCorrect"',
'MAX("lessonOrder")' -- bizarre syntactical requirement
);
select * from _test_pivoted ORDER BY "userCode";
I am using intillij paid version.
[42P01] ERROR: relation "_test_pivoted" does not exist
Position: 15
Hi,
I tried with the sample attached in documentation but it doesn't work for me. The table didn't create.
PostgreSQL 9.5.5 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609, 64-bit
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.