sql 查询语句合并问题
我想请问各位
select pStreet,count(distinct pComname) comnameNum
from tbl_Product
where pStreet in (select distinct pStreet from tbl_Product)
group by pStreet 单位和行业数量
select distinct pComname ,count(cTime) cTimeNum from tbl_Condition where cTime is not null
group by pComname 巡检数量
select COUNT(distinct pComname) from tbl_Condition 巡检间数
如何把上面这3条查询语句合并成一条查询语句?很急啊~~高分送上 SQL select 合并
[解决办法]
try this,
select * from
(
select pStreet,count(distinct pComname) comnameNum
from tbl_Product
where pStreet in (select distinct pStreet from tbl_Product)
group by pStreet
union all
select distinct pComname,count(cTime) cTimeNum
from tbl_Condition
where cTime is not null
group by pComname
union all
select '',COUNT(distinct pComname)
from tbl_Condition
) t
[解决办法]
SELECT MAX(pStreet) pStreet ,
SUM(comnameNum) comnameNum ,
MAX(pComname) pComname ,
SUM(cTimeNum) cTimeNum ,
SUM(pComname) pComname
FROM ( SELECT pStreet ,
COUNT(DISTINCT pComname) comnameNum ,
NULL AS pComname ,
0 AS cTimeNum ,
0 AS pComname
FROM tbl_Product
WHERE pStreet IN ( SELECT DISTINCT
pStreet
FROM tbl_Product )
GROUP BY pStreet
UNION ALL
SELECT NULL ,
0 ,
pComname ,
COUNT(cTime) cTimeNum ,
0
FROM tbl_Condition
WHERE cTime IS NOT NULL
GROUP BY pComname
UNION ALL
SELECT NULL ,
0 ,
NULL ,
0,
COUNT(DISTINCT pComname) pComname
FROM tbl_Condition
) t
