Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ask for pivot feature #50477

Open
l1t1 opened this issue Jun 2, 2023 Discussed in #50473 · 7 comments
Open

ask for pivot feature #50477

l1t1 opened this issue Jun 2, 2023 Discussed in #50473 · 7 comments
Labels

Comments

@l1t1
Copy link

l1t1 commented Jun 2, 2023

please add the pivot/unpivot statement in clickhouse.

Discussed in #50473

@l1t1
Copy link
Author

l1t1 commented Jun 5, 2023

duckdb design
duckdb/duckdb#6961 (comment)

@Pipboyguy
Copy link

Snowflake has this as well https://docs.snowflake.com/en/sql-reference/constructs/pivot

@danthegoodman1
Copy link

Bumping as this is super useful and painful without

@alanpaulkwan
Copy link

+1 +1 +1

@anjmao
Copy link

anjmao commented Jan 17, 2024

Aggregate combinators seems to solve pivot https://clickhouse.com/blog/aggregate-functions-combinators-in-clickhouse-for-arrays-maps-and-states

SELECT
    countIf((status = 'confirmed') AND (confirm_time > (create_time + toIntervalMinute(1)))) AS num_confirmed_checked,
    sumIf(total_amount, (status = 'confirmed') AND (confirm_time > (create_time + toIntervalMinute(1)))) AS confirmed_checked_amount,
    countIf(status = 'declined') AS num_declined,
    sumIf(total_amount, status = 'declined') AS dec_amount,
    avgIf(total_amount, status = 'declined') AS dec_average
FROM payments

┌─num_confirmed_checked─┬─confirmed_checked_amount─┬─num_declined─┬────────dec_amount─┬───────dec_average─┐
│                    3911195.989913940435010780.18000793457215.6036001586914 │
└───────────────────────┴──────────────────────────┴──────────────┴───────────────────┴───────────────────┘

@alanpaulkwan
Copy link

@anjmao doesn't this require manually enumerating values? What if I had 50 distinct values

@rnesytov
Copy link

rnesytov commented Apr 18, 2024

I've tried solution with argMaxIf (get last values by created_at for specific column value) with 200+ values/columns. Got an issue with memory usage, every new value/column requires more and more memory.
So, one could you this solution with reasonable amount of columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants