sql - SQL 根据 CASE 语句选择行
问题描述
如果存在,我想获得每个 ID 的“好”值,如果不存在,则获得“坏”值。
如果一个 ID 的 index='good',想要返回该 ID 的行。
如果一个 ID 只有 index='bad',想要得到那个。
你会怎么做?
解决方案
你可以试试下面的 -
with cte as
(
select id, index, value, row_number() over(partition by id order by case when index='good' then 1 else 2 end) as rn
from tablename
)
select id,index, value
from cte where rn=1
推荐阅读
- machine-learning - 使用 Chi2 进行特征选择生成 nan
- javascript - 可交互/可点击的 HTML 图像图
- java - 属性文件似乎正在产生空指针异常?
- cpu-architecture - 带有流水线的 cpu 中的 CPI
- bash - 使用 bash 完成从 /dev 获取文件
- mysql - 创建存储过程以从表中选择并将行插入日志表
- javascript - 以特定格式显示数组
- sql - SQL 请求将值放入列中
- asp.net-mvc - 使用我自己的登录页面使用 MVC 向 Azure AD 进行身份验证
- android - 隐藏 Android SDK sdkmanager cli 命令的进度条