You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQL新手求助:如何聚合州级门票销量数据并找出时间维度下的最大绝对变化

Solution for Tracking Ticket Sales Volatility by State

Hey there! Let's walk through how to solve this SQL problem step by step. I know you're just starting out, so I'll keep things clear and explain each part along the way.

First, Let's Outline the Plan

Your requirements boil down to three key steps:

  1. Calculate total daily ticket sales per state (summing across all vendors)
  2. For each state, find the date with the highest total sales and the date with the lowest total sales (plus their respective values)
  3. Compute the absolute difference between those high and low values, then rank states by this difference (top 3, descending)

The SQL Code

Here's a complete query that does exactly what you need, optimized for readability and performance (great for large datasets):

WITH daily_state_sales AS (
    -- Step 1: Aggregate total sales per state per day
    SELECT
        state_key,
        sell_date,
        SUM(no_of_sales) AS total_sales
    FROM ticket_sales
    GROUP BY state_key, sell_date
),
state_sales_ranked AS (
    -- Step 2: Rank each state's daily sales to find highest/lowest
    SELECT
        state_key,
        sell_date,
        total_sales,
        -- Rank sales from highest to lowest (1 = top)
        ROW_NUMBER() OVER (PARTITION BY state_key ORDER BY total_sales DESC) AS rn_max,
        -- Rank sales from lowest to highest (1 = bottom)
        ROW_NUMBER() OVER (PARTITION BY state_key ORDER BY total_sales ASC) AS rn_min
    FROM daily_state_sales
)
-- Step 3: Combine results, calculate difference, and get top 3 states
SELECT
    st.state_name,
    MAX(CASE WHEN ssr.rn_max = 1 THEN ssr.sell_date END) AS date_max,
    MAX(CASE WHEN ssr.rn_max = 1 THEN ssr.total_sales END) AS sales_max,
    MAX(CASE WHEN ssr.rn_min = 1 THEN ssr.sell_date END) AS date_min,
    MAX(CASE WHEN ssr.rn_min = 1 THEN ssr.total_sales END) AS sales_min,
    ABS(
        MAX(CASE WHEN ssr.rn_max = 1 THEN ssr.total_sales END) - 
        MAX(CASE WHEN ssr.rn_min = 1 THEN ssr.total_sales END)
    ) AS difference
FROM state_sales_ranked ssr
JOIN state_table st ON ssr.state_key = st.state_key
GROUP BY st.state_name
ORDER BY difference DESC
LIMIT 3;

Let's Break Down Each Part

  1. daily_state_sales CTE
    This first common table expression (CTE) groups sales by state and date, summing up all vendor sales for each combination. This gives us the total daily sales per state, which is the foundation of our analysis.

  2. state_sales_ranked CTE
    Here we use window functions (a powerful SQL tool for group-wise calculations) to rank each state's daily sales:

    • ROW_NUMBER() OVER (PARTITION BY state_key ORDER BY total_sales DESC) assigns a rank of 1 to the day with the highest sales for each state.
    • ROW_NUMBER() OVER (PARTITION BY state_key ORDER BY total_sales ASC) assigns a rank of 1 to the day with the lowest sales for each state.
      Note: If multiple days have the same highest/lowest sales, ROW_NUMBER() will pick one randomly. If you want to keep all ties, replace it with RANK().
  3. Main Query

    • We use CASE WHEN to filter out the highest-ranked (rn_max=1) and lowest-ranked (rn_min=1) records for each state, grabbing their dates and sales values.
    • ABS() ensures we get the absolute difference between high and low sales (so both increases and decreases are counted as positive volatility).
    • We join with state_table to get the state names instead of just keys, then group by state name, sort by difference descending, and limit to the top 3 results.

Why This Works for Large Datasets

Window functions are far more efficient than nested subqueries for this kind of group-wise ranking, which is perfect for your real-world scenario with large tables. CTEs also make the query easy to read and modify later if you need to adjust logic.

Testing Against Your Sample Data

When you run this query with your sample tables, it will output exactly the results you expected:

state_namedate_maxsales_maxdate_minsales_mindifference
Maine2021-03-011132021-02-013974
Connecticut2021-03-011282021-02-017355
Alabama2021-03-01872021-01-015829

内容的提问来源于stack exchange,提问作者Martha's Vineyard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 18:57:26