sum() 函数里加case when 统计一个字段 并用另一个字段去重 怎么实现
时间: 2024-03-12 21:45:42 浏览: 35
您可以使用以下 SQL 语句来实现:
```
SELECT SUM(CASE WHEN col1='xxx' THEN col2 ELSE 0 END) AS sum_value
FROM (
SELECT DISTINCT col3, col1, col2
FROM your_table
) t
```
其中,`col1` 是您要统计的字段,`col2` 是您要求和的字段,`col3` 是您要用来去重的字段。`xxx` 是您要筛选的条件。在这个查询中,首先使用了一个子查询来过滤重复的 `col3`,然后在主查询中使用 `CASE WHEN` 语句统计符合条件的 `col2` 的和。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![unitypackage](https://img-home.csdnimg.cn/images/20210720083646.png)