sql - 如何编写 SQL 代码来显示对数据值进行分类
问题描述
我有如下数据:
身份证国家 1 印度 2 香港 2 英国 3 智利 3 香港 4 新加坡
区域表是一个临时表,其数据如下:
国家地区 印度 亚太地区 香港 亚太地区 英国 EMEA 智利 拉丁美洲 香港 亚太地区 新加坡 亚太地区
如果 Country 属于同一地区,则显示地区名称 else 显示多个地区,如果 ID 具有不同的地区
输出 :
标识区域 1 亚太地区 2 多区域 3 多区域 4 亚太地区
解决方案
尝试Union
或尝试row_number()
作为其他答案之一,这很容易直接,因为row_number()
不会减少行数,但有时group by
取决于数据库和版本。row_number()
Select id, "MULTIPLE
REGIONS"
from table t1 join temp t2
on t1.country=t2.country
Group by id having
count(distinct
region) >=1
Union
(Select id, region
from table t1 join temp t2
on t1.country=t2.country
Group by id having
count(distinct
region) =1)
推荐阅读
- javascript - 在 JS 中获取用户定义的 CSS 值
- azure-active-directory - dataLocationCode 字段在多地理位置的图形 API 中不可用
- keycloak - 根据有效角色获取 keycloak 用户
- javascript - javascript/typescript/angular 中的逗号分隔符
- spring-boot - CCAvenue 与 Angular 7 + spring boot + mongo 集成
- javascript - 更改材质ui文本字段的边框底部颜色
- html - 我正在制作一个网站登录页面,但菜单 div 未能出现在我的背景前。如何强制 div 出现在前面?
- python-3.x - GCS 对象更改通知,发布/订阅无法接收任何内容
- field - 引用域函数中的另一个字段 (Odoo12)
- swift - 如何在 Swift 中将 Int 转换为 4 字节的字节数组?