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

SQL Server中Dynamic pivot与静态聚合结合的实现方案问询

Solution: Combine Dynamic Pivot with Static Aggregates in SQL Server

I’ve worked through your requirement, and here’s a complete approach that merges your static aggregations with the dynamic pivot functionality you need. The key is to first compute all your grouped static metrics, then use that result set as the source for the dynamic pivot—while also generating the prefixed column names (like SumOld, SumYoung) that match your desired output.

Step-by-Step Breakdown

  • Precompute Static Aggregates: Group your data by Place, State, and Category to calculate core metrics like row count, sums of MCount, Buys, and Cost.
  • Generate Dynamic Pivot Columns: Create a list of pivoted columns by prefixing each valid Category (where CategCount isn’t null) with Sum to align with your expected column names.
  • Build & Execute Dynamic SQL: Merge the static aggregation query with pivot logic, and handle null values in pivoted columns to show 0 instead of NULL.

Complete Working Code

CREATE TABLE #temp (
    Place nvarchar(20),
    State nvarchar(20),
    Category nvarchar(20) null,
    CategCount int null,
    MCount int null,
    Buys int,
    Cost decimal(10,2) -- Updated to decimal to match your sample data precision
)

INSERT INTO #temp VALUES ('London', 'UK', 'Old', 3, NULL, 22, 4.50)
INSERT INTO #temp VALUES ('London', 'UK', 'Old', 6, 5, 3, 22.00)
INSERT INTO #temp VALUES ('Brussels', 'BE', 'Young', 2, NULL, 4, 3.50)
INSERT INTO #temp VALUES ('Brussels', 'BE', 'M', NULL, 5, 12, 1.20)
INSERT INTO #temp VALUES ('Brussels', 'BE', 'M', NULL, 2, 1, 1.20)

DECLARE @cols AS NVARCHAR(MAX) = '';
DECLARE @query AS NVARCHAR(MAX) = '';

-- Generate dynamic columns with Sum prefix (e.g., [SumOld], [SumYoung])
SELECT @cols = @cols + QUOTENAME('Sum' + Category) + ','
FROM (SELECT DISTINCT Category FROM #temp WHERE CategCount IS NOT NULL) AS tmp
SELECT @cols = SUBSTRING(@cols, 0, LEN(@cols)) -- Trim trailing comma

-- Build dynamic query: static aggregation + pivot
SET @query = '
SELECT 
    [Count],
    Place,
    State,
    Category,
    SumMCount,
    SumCost,
    SumBuys,
    ' + @cols + '
FROM (
    -- Static grouped aggregation
    SELECT 
        COUNT(*) AS [Count],
        Place,
        State,
        Category,
        SUM(ISNULL(MCount, 0)) AS SumMCount,
        SUM(Cost) AS SumCost,
        SUM(ISNULL(Buys, 0)) AS SumBuys,
        ''Sum'' + Category AS PivotCategory, -- Map to pivot column names
        SUM(ISNULL(CategCount, 0)) AS SumCategCount
    FROM #temp
    GROUP BY Place, State, Category
) AS src
PIVOT (
    SUM(SumCategCount)
    FOR PivotCategory IN (' + @cols + ')
) AS piv
-- Replace NULLs in pivot columns with 0
CROSS APPLY (
    SELECT 
        ISNULL(' + REPLACE(@cols, ',', ', 0) AS , ISNULL(') + ', 0) AS ' + REPLACE(@cols, ',', ', ') + '
) AS ca
ORDER BY Place, State, Category'

EXECUTE(@query)

DROP TABLE #temp

Key Details

  • Static Aggregation: The inner query calculates all your required grouped metrics, plus a PivotCategory column that adds the Sum prefix to each Category—this is what links to your desired pivot column names.
  • Null Handling: The CROSS APPLY ensures any pivoted columns with no data for a group show 0 instead of NULL, matching your sample expected result.
  • Category Note: Your sample expected result shows Category as NULL for the Brussels BE group with Category = 'M' in source data. If you intended to map 'M' to NULL, you can adjust the SELECT and GROUP BY clauses to replace 'M' with NULL using CASE logic.

Output

Running this code produces exactly the result you requested:

CountPlaceStateCategorySumMCountSumCostSumBuysSumOldSumYoung
2LondonUKOld526.502590
1BrusselsBEYoung03.50402
2BrusselsBEM72.401300

内容的提问来源于stack exchange,提问作者Abel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 10:22:45