sql - 避免 SELECT 重复行上的 CASE
问题描述
新手在这里,我正在努力修复一个查询,其中选择案例由于聚合而在结果中创建重复行。
select distinct A, B, C, sum(case when X = 1 then N end) as D, sum(case when X = 2 then N end) as E
from table1
where X in (1,2)
group by A, B, C
结果
A B C D E
205 NICE Y 752
205 NICE Y 4356
预期的
A B C D E
205 NICE Y 752 4356
解决方案
这应该做你想要的:
select A, B, C,
sum(case when X = 1 then N end) as D,
sum(case when X = 2 then N end) as E
from table1
where X in (1, 2)
group by A, B, C;
select distinct
不需要。
如果您获得多行,则 、 和 的值A
不B
完全相同C
。这可能是由于值中的空格、隐藏字符、在不同字符集中看起来相同的字符。
我建议您独立调查每一列:
select distinct A
from table1;
找出差异在哪里。
推荐阅读
- javascript - vue为什么会丢失v-for中嵌套组件的数据
- javascript - 如何在 React JS 中 5 秒后消失警报?
- mongodb - 如何使用多个 IP 地址设置 mongod.conf bindIp(在 mongodb 4.4 中)
- asp.net - Crystal Report 的 ReportDocument 报错
- python - 在 DevOps 管道中下载并安装 selenium、webdriver 和 chromedriver?
- python-3.x - 记录双极模拟交流电流信号
- git - 如何在我的移动浏览器上运行 Flutter Web 应用
- javascript - 如何修复我的代码,以便它根据 MongoDB 中提交的 id 重定向页面?
- google-apps-script - 将图形资产添加到谷歌网络应用脚本的最佳途径是什么?
- java - order_inserts=true 的外键违规和混合子类实体的批次