select count(1) from (select distinct unique_id_station from d_wx_expansion_oarea2023 dweo where 1 = 1 and unique_id_station is not null) as a union all select count(1) from (select distinct unique_id_station from d_wx_expansion_oarea2023 where 1 = 1 and is_pdcp_busy_week =1 and unique_id_station is not null) as aaa 将两条sql合成一条不使用UNION ALL
时间: 2024-03-21 19:38:44 浏览: 57
可以使用子查询和 CASE WHEN 语句将两条 SQL 合并成一条,如下所示:
```
SELECT
COUNT(DISTINCT CASE WHEN unique_id_station IS NOT NULL THEN unique_id_station END) as total_count,
COUNT(DISTINCT CASE WHEN is_pdcp_busy_week = 1 AND unique_id_station IS NOT NULL THEN unique_id_station END) as pdcp_busy_count
FROM d_wx_expansion_oarea2023
```
这条 SQL 语句会返回两个列,分别为总数量和满足 `is_pdcp_busy_week = 1` 条件的数量。
相关问题
select count(1) from (select distinct unique_id_station from d_wx_expansion_oarea2023 dweo where 1 = 1 and unique_id_station is not null) as a union all select count(1) from (select distinct unique_id_station from d_wx_expansion_oarea2023 where 1 = 1 and is_pdcp_busy_week =1 and unique_id_station is not null) as aaa 将两条sql合成一条
可以使用 UNION ALL 将两条 SQL 合并成一条,如下所示:
```
SELECT COUNT(1) FROM (
SELECT DISTINCT unique_id_station
FROM d_wx_expansion_oarea2023
WHERE unique_id_station IS NOT NULL
) AS a
UNION ALL
SELECT COUNT(1) FROM (
SELECT DISTINCT unique_id_station
FROM d_wx_expansion_oarea2023
WHERE is_pdcp_busy_week = 1 AND unique_id_station IS NOT NULL
) AS aaa
```
select count(1)from (select distinct unique_id_station from d_wx_expansion_oarea2023 dweo where 1 = 1 and unique_id_station is not null and province_name ='河北省'and month_id ='202212' ) as a union all select count(1) from d_wx_expansion_oarea2023 where 1 = 1 and province_name ='河北省'and city_name ='邯郸市'and month_id ='202212' union all select count(1) from d_wx_expansion_oarea2023 where 1 = 1 and province_name ='河北省'and city_name ='邯郸市'and month_id ='202212' and is_pdcp_busy_week =1 union all select count(1)from(select distinct unique_id_station from d_wx_expansion_oarea2023 where 1 = 1 and province_name ='河北省' and city_name ='邯郸市'and month_id ='202212' and is_pdcp_busy_week =1 and unique_id_station is not null) as aaa将四个sql语句合成一个
SELECT
(SELECT COUNT(DISTINCT unique_id_station) FROM d_wx_expansion_oarea2023 WHERE province_name = '河北省' AND month_id = '202212' AND unique_id_station IS NOT NULL) AS cnt1,
(SELECT COUNT(*) FROM d_wx_expansion_oarea2023 WHERE province_name = '河北省' AND city_name = '邯郸市' AND month_id = '202212') AS cnt2,
(SELECT COUNT(*) FROM d_wx_expansion_oarea2023 WHERE province_name = '河北省' AND city_name = '邯郸市' AND month_id = '202212' AND is_pdcp_busy_week = 1) AS cnt3,
(SELECT COUNT(DISTINCT unique_id_station) FROM d_wx_expansion_oarea2023 WHERE province_name = '河北省' AND city_name = '邯郸市' AND month_id = '202212' AND is_pdcp_busy_week = 1 AND unique_id_station IS NOT NULL) AS cnt4;
阅读全文