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合成一条
时间: 2024-03-21 17:38:39 浏览: 63
可以使用 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;
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 关键字,表示去重后的记录数。
阅读全文