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.

1 vote
1 answer
4k views

Dynamic pivoting in PostgreSQL

I have a scenario where I have to pivot a list of rows to columns dynamically .For a particular order_number there might be several products. …
Sandeep's user avatar
  • 113
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
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
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
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
1 vote
Accepted

What is the most efficient way, to create this output, from a single SQL table?

In general I recommend to do this kind of pivot/crosstab in the frontend (UI) Possible alternatives are to aggregate into a JSON value: select y, jsonb_object_agg(concat('x',x, 'z'), y) as xz from … Dynamic view example …
user avatar
-1 votes
2 answers
1k views

EAV in postgresql

However, crosstab requires a custom type for the output of the pivot, with all field names and types that will go in column. I have all the information stored in the field table. … My question is then: can I create a custom type dynamically in PostgreSQL, from the information I store in my field table? …
phme's user avatar
  • 1
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
0 votes
0 answers
140 views

Query to get the "Item -- (1,n) --> Attributes" relation table?

| A | B | C | D | -------------------- a | x | x | y | | b | x | z | | | c | w | | | | d | | | | | Unfortunately my SQL knowledge is rusty and Postgres does not support PIVOT
j0j0j0's user avatar
  • 1
0 votes
Accepted

EAV in postgresql

Certainly not the most elegant way, but it works with dynamic sql: DO $$ DECLARE sql1 text := '''SELECT user_id AS row_name, field_name AS category, field_value AS value FROM user_data a JOIN … , sql2, ') AS ct(row_name int, ', sql3, ');'); EXECUTE sql4; END $$; SELECT * FROM tmp_data; All fields are typed it's reasonably fast with the small amount of data I have (less than 10k lines to pivot
phme's user avatar
  • 1
1 vote
Accepted

Add fields to view from select (transpose fields)

Detailed instructions: PostgreSQL Crosstab Query Still not "dynamic". A completely dynamic function is hardly possible since SQL demands to know the return type at the time of the function call. … If the return type is dynamic, you need two steps: build your query dynamically execute it. But there may be something for you here: Dynamic alternative to pivot with CASE and GROUP BY …
Erwin Brandstetter's user avatar
0 votes

Add fields to view from select (transpose fields)

I think you are looking for something like pivot. PostgreSQL is able to do something like that with a tablefunc. … There you could use crosstab() But having said: This is not complete dynamic and therefore not fitting your question 100%. …
frlan's user avatar
  • 495
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