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

请求编写SQL查询:按国家统计客户数量(GB与UK视为同一国家)

SQL Query to Count Customers by Country (Grouping GB & UK as One)

First, let's recap your table data for clarity:

NameCountry
AUK
BCN
CIN
DGB
ECN
FGB
HUK
IIN

To count customers per country while treating GB and UK as the same region, you can use a CASE statement to standardize those country codes into a single label, then group and count. This approach works across most SQL databases (PostgreSQL, MySQL, SQL Server, etc.):

SELECT
    CASE
        WHEN Country IN ('GB', 'UK') THEN 'United Kingdom'
        ELSE Country
    END AS standardized_country,
    COUNT(*) AS customer_count
FROM Customers
GROUP BY standardized_country;

How this works:

  • The CASE expression checks if the country is either GB or UK, replacing both with "United Kingdom" (feel free to swap this label for something like "UK/GB" if that fits your needs better).
  • We group by the standardized country name, then use COUNT(*) to calculate the total number of customers in each group.

Expected Output:

customer_count
4
2
2

If you're using MySQL and prefer a more concise syntax, you can use the IF function instead of CASE:

SELECT
    IF(Country IN ('GB', 'UK'), 'United Kingdom', Country) AS standardized_country,
    COUNT(*) AS customer_count
FROM Customers
GROUP BY standardized_country;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:46:00