Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results for dynamic pivot tagged with
Search options not deleted

All versions of PostgreSQL. Add an additional version-specific tag like [postgresql-13] if that context is relevant.

20 votes
4 answers
11k views

How do I generate a pivoted CROSS JOIN where the resulting table definition is unknown?

int FROM generate_series(1,10) AS t(x); CREATE TEMP TABLE bar AS SELECT chr(x+72) AS name, x::int FROM generate_series(1,5) AS t(x); I think this would be easily do-able with a CROSSTAB capable of a dynamic … tables are of undefined length, Then the cross-join represents a cube of undefined dimension (because of above), The catagory-names (cross-tab parlance) are in the table What's the best we can do in PostgreSQL
Evan Carroll's user avatar
  • 64.7k
16 votes

How do I generate a pivoted CROSS JOIN where the resulting table definition is unknown?

Unknown return type, dynamic SQL Column names and types cannot be dynamic. SQL demands to know number, names and types of resulting columns at call time. … Related: PostgreSQL convert columns to rows? Transpose? Dynamic alternative to pivot with CASE and GROUP BY …
Erwin Brandstetter's user avatar
7 votes
Accepted

Dynamically convert hstore keys into columns for an unknown set of keys

Read this first if you are not familiar: PostgreSQL Crosstab Query Step 2a: Generate query SELECT format( $s$SELECT * FROM crosstab( $$SELECT h.id, kv.* FROM hstore_test h, each(hstore_col … Related: Dynamic alternative to pivot with CASE and GROUP BY Step 2b: Execute query This generates a query of the form: SELECT * FROM crosstab( $$SELECT h.id, kv.* FROM hstore_test h …
Erwin Brandstetter's user avatar
5 votes

Count where two or more columns in a row are over a certain value [basketball, double double...

Unfortunately PostgreSQL doesn't provide nice tools to do that, so without getting into dynamic SQL generation in PL/PgSQL, we can at least do: SELECT player_id, seasonday, 'points' AS scoretype, points …
Craig Ringer's user avatar
  • 57.3k
5 votes

Query to get counts of values per column

DISTINCT column2) AS column2, count(DISTINCT column3) AS column3, count(DISTINCT column4) AS column4 FROM ecarroll.public.foo; Which is pretty much what you want, except you need to pivot … to generate dynamic SQL to do the count() wrap that with more dynamic sql to do the pivot. …
Evan Carroll's user avatar
  • 64.7k
5 votes

Is it possible to create a dynamic join?

Basically it's a crosstab query: PostgreSQL Crosstab Query The dynamic result type is a problem, though. ... is it possible to create a dynamic query which will output the right thing regardless … I have answered similar questions on SO before: Dynamic alternative to pivot with CASE and GROUP BY Refactor a PL/pgSQL function to return the output of various SELECT queries …
Erwin Brandstetter's user avatar
5 votes
Accepted

Find changed columns for near-identical rows across tables

See: Table name as a PostgreSQL function parameter Simple dynamic solution The difficulty is to return varying row types. SQL demands to know the return type at call time. … in this case): Dynamic alternative to pivot with CASE and GROUP BY …
Erwin Brandstetter's user avatar
4 votes

Pivot time series into date period columns with array aggregate cells

You need some aggregation of your data, and a pivot table. … argument of CROSSTAB can be something like $$ WITH n(ow) AS (VALUES(DATE_TRUNC('day', NOW()))) SELECT GENERATE_SERIES(n.ow + '-2 days', n.ow, '1 day') FROM n $$ and the query would always return a dynamic
Dario's user avatar
  • 748
4 votes
Accepted

How to transpose values from one column into columns with a value from a different column?

First of all you will need to install the tablefunc extension using the create extension tablefunc; command, otherwise the pivot function crosstab will not work. … A more dynamic version of the above (although not perfect by any means): create or replace function get_dynamic_transpose() returns text language plpgsql as $$ declare v_output_columns text; begin …
Chessbrain's user avatar
  • 1,223
3 votes
Accepted

Create 2D truth table from "cross product" of two tables via PostgreSQL view or function

B')::int as "B" ,bool_or(group_name='C')::int as "C" ,bool_or(group_name='Z')::int as "Z" FROM group_membership GROUP BY member ORDER BY member; Postgres isn't structured to make dynamicpivot tables easy. …
Jasen's user avatar
  • 3,622
3 votes

Rotating a table with multiple value columns

There are some ways to avoid that: generate the query that pivots all columns with dynamic SQL. … With a client-side pivot (psql) A client-side, psql-only solution is more straightforward with \crosstabview (psql 9.6+). …
Daniel Vérité's user avatar
3 votes
1 answer
6k views

Add column with a sum total to crosstab() query in PostgreSQL 9.0

Following on from my previous question: Creating crosstab() pivot table in PostgreSQL 9.0 I managed to create a pivot table for ageband using the crosstab() function. … If this can be done then I can create dynamic views on different factors using one geometry-less table and unit_postcode. …
daniel franklin's user avatar
3 votes
3 answers
11k views

Pivot time series into date period columns with array aggregate cells

Given a time series of events where each event has a successful or unsuccessful outcome, how do I pivot the ratio of events by entity and time period columns with an aggregate array cell value? … [0 0 0] ... | +-----------------------------------------------+ To keep things simple, this report will never need more than 10 date window columns and I can dynamically template the SQL pivot
Petrus Theron's user avatar
2 votes
Accepted

How to set the column names dynamically in crosstab?

generate the crosstab query with another query execute the crosstab query Or the problem must be changed to accept results embedded in a JSON column fed with json_object_agg (as illustrated in Static and dynamicDynamic alternative to pivot with CASE and GROUP BY provides quite a bit to read and pointers to other Q/As. …
Daniel Vérité's user avatar
1 vote

Write a dynamic query of nested subqueries

rdbms=postgres_11&fiddle=407a37686238bb3fbcbc4285d1705871 Unfortunately it turns out using a Pivot/CrossTab is much more complex than dynamically generating the original query with code: SELECT employeeid …
Jeremy Thompson's user avatar

15 30 50 per page