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去重
时间: 2024-03-12 16:45:31 浏览: 56
如果您需要让 `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 关键字,表示去重后的记录数。
select t1.month_id, count(distinct case when t1.offer_type_name='主套餐' then offer_name end) aa, count(distinct case when t1.offer_type_name='促销' then offer_name end) bb, count(distinct case when t1.offer_type_name='可选包' then offer_name end) cc, count(distinct case when t2.offer_type_name='主套餐' then offer_name end) aaa, count(distinct case when t2.offer_type_name='促销' then offer_name end) bbb, count(distinct case when t2.offer_type_name='可选包' then offer_name end) ccc, from zone_crmchk.dwd_sum_zhuliuxxsc t1 left join zone_crmchk.dwd_sum_zhuliuxxsc t2 on t2.month < t1.month group by t1.month, count(distinct case when offer_type_name='主套餐' then offer_name end) aa, count(distinct case when offer_type_name='促销' then offer_name end) bb, count(distinct case when offer_type_name='可选包' then offer_name end) cc
根据您提供的 SQL 查询语句,我发现其中存在一个逗号(,)出现在了 FROM 子句之后,这是一个语法错误。您需要将逗号(,)删除,同时将查询语句修改为如下形式:
```
SELECT
t1.month_id,
COUNT(DISTINCT CASE WHEN t1.offer_type_name='主套餐' THEN offer_name END) aa,
COUNT(DISTINCT CASE WHEN t1.offer_type_name='促销' THEN offer_name END) bb,
COUNT(DISTINCT CASE WHEN t1.offer_type_name='可选包' THEN offer_name END) cc,
COUNT(DISTINCT CASE WHEN t2.offer_type_name='主套餐' THEN offer_name END) aaa,
COUNT(DISTINCT CASE WHEN t2.offer_type_name='促销' THEN offer_name END) bbb,
COUNT(DISTINCT CASE WHEN t2.offer_type_name='可选包' THEN offer_name END) ccc
FROM
zone_crmchk.dwd_sum_zhuliuxxsc t1
LEFT JOIN zone_crmchk.dwd_sum_zhuliuxxsc t2 ON t2.month < t1.month
GROUP BY
t1.month_id,
COUNT(DISTINCT CASE WHEN offer_type_name='主套餐' THEN offer_name END),
COUNT(DISTINCT CASE WHEN offer_type_name='促销' THEN offer_name END),
COUNT(DISTINCT CASE WHEN offer_type_name='可选包' THEN offer_name END);
```
这样修改后,查询语句应该就能正确执行了。
阅读全文