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

SQL分组与嵌套拼接:如何实现目标聚合输出?

问题描述

原始样本数据:

IDContinentCountryCity
1AfricaEgyptCairo
2AfricaEgyptAlexandria
3AfricaEgyptLuxor
4AfricaMoroccoRabat
5AfricaMoroccoCasablanca
6AsiaChinaBeijing
7AsiaChinaShanghai

期望输出:

IDContinentCountryAndCities
1AfricaEgypt(Cairo,Alexandria,Luxor) - Morocco(Rabat,Casablanca)
2AsiaChina(Beijing,Shanghai)

已尝试代码:

SELECT DISTINCT
    Country,
    ISNULL(STUFF((SELECT CONCAT('- ', City)
                  FROM Continental s2
                  WHERE s2.Country = s.Country
                  GROUP BY City
                  FOR XML PATH ('')), 1, 1, ''), '') AS Cities
FROM
    Continental s
GROUP BY
    Country

当前输出:

CountryCities
EgyptCairo- Alexandria- Luxor
MoroccoCasablanca- Rabat
ChinaBeijing- Shanghai

需要修改代码以得到期望的目标输出。

修改后的SQL代码
WITH CountryCities AS (
    -- 按国家聚合城市,生成「国家(城市1,城市2...)」格式字符串
    SELECT 
        Continent,
        Country,
        CONCAT(Country, '(', 
               STUFF((SELECT CONCAT(',', City)
                      FROM Continental s2
                      WHERE s2.Country = s.Country
                      ORDER BY City
                      FOR XML PATH('')), 1, 1, ''), 
               ')') AS CountryCityStr
    FROM Continental s
    GROUP BY Continent, Country
),
ContinentGroups AS (
    -- 按大洲聚合国家城市字符串,生成最终组合字段
    SELECT 
        Continent,
        STUFF((SELECT CONCAT(' - ', CountryCityStr)
               FROM CountryCities cc2
               WHERE cc2.Continent = cc1.Continent
               ORDER BY Country
               FOR XML PATH('')), 1, 3, '') AS CountryAndCities
    FROM CountryCities cc1
    GROUP BY Continent
)
-- 生成带自增ID的最终结果
SELECT 
    ROW_NUMBER() OVER (ORDER BY Continent) AS ID,
    Continent,
    CountryAndCities
FROM ContinentGroups
代码说明
  1. CountryCities 公共表表达式:

    • 按大洲和国家分组,对每个国家下的城市进行逗号拼接,再用国家名和括号包裹,生成类似Egypt(Cairo,Alexandria,Luxor)的字符串。
    • 用STUFF移除拼接后开头多余的逗号。
  2. ContinentGroups 公共表表达式:

    • 按大洲分组,将同一大洲的国家城市字符串用-拼接,生成目标格式的CountryAndCities字段。
    • 用STUFF移除开头多余的-(长度为3,包含空格和破折号)。
  3. 最终查询:

    • 用ROW_NUMBER()生成每个大洲对应的ID,按大洲排序输出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 19:52:41