Skip to content

Sorting groups by numerical value #63041

Closed Answered by den-crane
njcstreet asked this question in Q&A
Discussion options

You must be logged in to vote

gpt4 https://fiddle.clickhouse.com/1b477f44-cf43-492d-8cb3-e84344ea3552

Prompt: try window functions and rank

ChatGPT:

Using window functions and ranking is a great way to handle this task efficiently in ClickHouse. We can compute rankings based on the maximum sales amount for each product and sub-product, and then use these rankings to order the results. Here’s how you can structure the query using window functions:

SELECT 
    product,
    sub_product,
    sales_amount
FROM 
    (
        SELECT 
            product,
            sub_product,
            sales_amount,
            -- Calculate the rank of products by their maximum sales amount
            rank() OVER (ORDER BY max_sales_p…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by njcstreet
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants