select count(distinct offer_name),month_id from (select count(distinct offer_name), month_id from dwd_sum_zhuliuxxsc where month_id =to_char(month_id+1,yyyymm) group by month_id)语法修正一下
时间: 2024-02-01 09:04:36 浏览: 26
你的 SQL 查询语句已经很接近正确的语法了,只需要将子查询的结果作为一个临时表,再在外层查询中进行聚合即可。可以使用以下语句:
```
SELECT COUNT(DISTINCT offer_name), month_id
FROM (
SELECT COUNT(DISTINCT offer_name), month_id
FROM dwd_sum_zhuliuxxsc
WHERE month_id = TO_CHAR(month_id+1, 'yyyymm')
GROUP BY month_id
) tmp
GROUP BY month_id;
```
这样就可以得到按月份统计的不同 offer_name 数量了。希望对你有所帮助。
相关问题
select t1.month_id, count(distinct offer_name), '今年往月新增有订购' as A from zone_crmchk.dwd_sum_zhuliuxxsc t1 left join zone_crmchk.dwd_sum_zhuliuxxsc t2 on t2.month < t1.month group by t1.month
这是一个 SQL 查询语句,用于统计每个月新增有订购的 offer 数量。其中,t1 和 t2 都是来自于 zone_crmchk 数据库中的 dwd_sum_zhuliuxxsc 表,并通过左连接来进行关联。具体来说,t1 表中的记录代表着当前月份的数据,而 t2 表中的记录则代表着之前月份的数据。通过在 t2 表上添加条件 t2.month < t1.month,可以保证只统计之前的月份。最后,根据 t1.month_id 进行分组,并使用 count(distinct offer_name) 统计不重复的 offer 数量,最终得到的结果会显示在第三列,显示的内容为“今年往月新增有订购”。
select distinct tablespace_name from all_tables group by tablespace_name
在 SQL 中,你的查询语句是有问题的。你不能在使用 `GROUP BY` 之前使用 `DISTINCT` 关键字。如果你想获取所有表的唯一的 `tablespace_name`,你可以使用以下查询语句:
```sql
SELECT DISTINCT tablespace_name
FROM all_tables;
```
这个查询语句将返回 `all_tables` 表中唯一的 `tablespace_name` 值。每个不同的 `tablespace_name` 将作为结果返回,重复的值将被过滤掉。