读书人

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

发布时间: 2012-12-16 12:02:32 作者: rapoo

一个应用问题,请帮忙看看
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
绿色的地方去掉,红色的地方加上
[其他解释]
“绿色的地方去掉,红色的地方加上 ”。

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

读书人网 >SQL Server

热点推荐