SELECT COUNT(DISTINCT another_field), SUM(CASE WHEN your_field = your_condition_1 THEN 1 ELSE 0 END) AS count_1, SUM(CASE WHEN your_field = your_condition_2 THEN 1 ELSE 0 END) AS count_2, SUM(CASE WHEN your_field = your_condition_3 THEN 1 ELSE 0 END) AS count_3FROM your_table;这条语句里怎么让your_field 不加group by根据another_field去重
时间: 2024-03-12 17:45:32 浏览: 75
在 Oracle 中,如果您不想使用 `GROUP BY` 子句,可以使用子查询来实现按 `another_field` 字段去重的效果:
```
SELECT
COUNT(DISTINCT another_field),
SUM(CASE WHEN your_field = your_condition_1 THEN 1 ELSE 0 END) AS count_1,
SUM(CASE WHEN your_field = your_condition_2 THEN 1 ELSE 0 END) AS count_2,
SUM(CASE WHEN your_field = your_condition_3 THEN 1 ELSE 0 END) AS count_3
FROM (
SELECT DISTINCT another_field, your_field
FROM your_table
) subquery;
```
这条 SQL 语句将会先对 `your_table` 表进行去重,使用 `DISTINCT` 关键字按 `another_field` 和 `your_field` 字段分别去重,然后再在子查询的结果上进行统计操作。这样,查询结果将会根据 `another_field` 字段去重,并对每个不同的 `another_field` 值分别统计 `your_field` 字段的数量。
相关问题
SELECT COUNT(DISTINCT another_field), SUM(CASE WHEN your_field = your_condition_1 THEN 1 ELSE 0 END) AS count_1, SUM(CASE WHEN your_field = your_condition_2 THEN 1 ELSE 0 END) AS count_2, SUM(CASE WHEN your_field = your_condition_3 THEN 1 ELSE 0 END) AS count_3FROM your_table;这条语句里怎么让your_field根据another_field去重
如果您需要让 `your_field` 字段根据 `another_field` 字段去重,可以在 `COUNT` 函数和 `GROUP BY` 子句中分别使用 `DISTINCT another_field`:
```
SELECT
COUNT(DISTINCT another_field),
SUM(CASE WHEN your_field = your_condition_1 THEN 1 ELSE 0 END) AS count_1,
SUM(CASE WHEN your_field = your_condition_2 THEN 1 ELSE 0 END) AS count_2,
SUM(CASE WHEN your_field = your_condition_3 THEN 1 ELSE 0 END) AS count_3
FROM your_table
GROUP BY DISTINCT another_field;
```
这样,查询结果将会根据 `another_field` 字段去重,并对每个不同的 `another_field` 值分别统计 `your_field` 字段的数量。
select count(*) as count2, sum(count1) as coun3 from (select 1 , case when is_pdcp_busy_week =1 then 1 else 0 end as count1 from d_wx_expansion_oarea2023 )t union all SELECT COUNT(DISTINCT CASE WHEN unique_id_station IS NOT NULL THEN unique_id_station END) as count1, COUNT(DISTINCT CASE WHEN is_pdcp_busy_week = 1 AND unique_id_station IS NOT NULL THEN unique_id_station END) as count4 FROM d_wx_expansion_oarea2023将结果分别展示
对于第一个 SQL 查询,结果展示如下:
| count2 | count3 |
|--------|--------|
| 1 | count1的总和 |
其中,count2 的值为 1,表示只有一条结果。count3 的值为 count1 的总和,表示 is_pdcp_busy_week 为 1 的记录数。
对于第二个 SQL 查询,结果展示如下:
| count1 | count4 |
|--------|--------|
| 不为空的 unique_id_station 记录数 | is_pdcp_busy_week 为 1 且 unique_id_station 不为空的记录数 |
其中,count1 的值为不为空的 unique_id_station 记录数,count4 的值为 is_pdcp_busy_week 为 1 且 unique_id_station 不为空的记录数。注意,这里使用了 DISTINCT 关键字,表示去重后的记录数。
阅读全文