当前位置: 代码迷 >> Sql Server >> 一个应用有关问题,请帮忙看看
  详细解决方案

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

热度:88   发布时间:2016-04-25 01:03:21.0
一个应用问题,请帮忙看看
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
绿色的地方去掉,红色的地方加上
------其他解决方案--------------------
“绿色的地方去掉,红色的地方加上 ”。

谢谢!
------其他解决方案--------------------
派生表需要有一个名称,否则会报错
  相关解决方案