首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一个应用有关问题,请帮忙看看

2012-12-16 
一个应用问题,请帮忙看看select f.a,sum(xyCount)from(select f.a,count(distinct(ly.b)) xyCountfrom f i

一个应用问题,请帮忙看看
select f.a,sum(xyCount)
from
(select f.a,count(distinct(ly.b)) xyCount
from f inner join ly on ...
group by f.a
union
select f.a,count(distinct z.b) xyCount
from f inner join z on ...
group by f.a
)
group by a

两个使用了聚合函数的结果及并集后再group by,这样写有错误
有什么好办法吗?
[最优解释]

select a,sum(xyCount)
 from
 (select f.a,count(distinct(ly.b)) xyCount
 from f inner join ly on ...
 group by f.a
 union
 select f.a,count(distinct z.b) xyCount
 from f inner join z on ...
 group by f.a
 ) as TB
 group by a

[其他解释]
select f.a,sum(xyCount)
 from
 (select f.a,count(distinct(ly.b)) xyCount
 from f inner join ly on ...
 group by f.a
 union
 select f.a,count(distinct z.b) xyCount
 from f inner join z on ...
 group by f.a
 ) a
 group by a
加个红字
[其他解释]
select f.a,sum(xyCount) xyCount
 from
 (select f.a,count(distinct(ly.b)) xyCount
 from f inner join ly on ...
 group by f.a
 union
 select f.a,count(distinct z.b) xyCount
 from f inner join z on ...
 group by f.a
 ) as B 
 group by a
绿色的地方去掉,红色的地方加上
[其他解释]
“绿色的地方去掉,红色的地方加上 ”。

谢谢!
[其他解释]
派生表需要有一个名称,否则会报错

热点排行