日期:2014-05-16  浏览次数:20962 次

求select 查询 和select cum(case when ...) 组合用法
我就是先写了 一个 查询

select t70.d0010 as jiagongUUID ,t70.d0040 as jiagongdate,t50.d0030 as dongtiNUM,t50.d0040 as dongtizhongliang,t70.d0030 as jiagongshunxu from T0070 t70 left join T0050 t50 on t50.d0010=t70.d0020 

查出来是两条记录的

然后 
select sum(case when t71.d0020 =t70.d0010 then 1 else 0 end) as jiagongkuaishu,sum(case when t71.d0020=t70.d0010 then t71.d0050 else 0 end ) from T0071 t71,T0070 t70 group by t70.d0010
其中一个表和另一个表关联 算出来 另一个表的其中字段的和
是一条记录 70表对应71表的只有一条记录

最后我想把这两条记录全部一起查出来
select T.jiagongUUID,sum(case when t71.d0020 =T.jiagongUUID then 1 else 0 end) as jiagongkuaishu ,sum(case when t71.d0020=T.jiagongUUID then t71.d0050 else 0 end ) as jiagongzhongliang from(select t70.d0010 as jiagongUUID ,t70.d0040 as jiagongdate,t50.d0030 as dongtiNUM,t50.d0040 as dongtizhongliang,t70.d0030 as jiagongshunxu from T0070 t70 left join T0050 t50 on t50.d0010=t70.d0020 ) T,T0071 t71 group by T.jiagongUUID
这样是对了 是我想要的 可是再加上一个T里面的字段就报错:不是group by 语句!!
真的会是怎么解决了

------解决方案--------------------
没有发现错误。。。。
------解决方案--------------------
把你想加入的字段改為 max(字段名) 就可以了
------解决方案--------------------
T,T0071这两张表为什么没有关联起来?
你这样查询肯定会有问题
------解决方案--------------------
#2,#4正解
------解决方案--------------------
用分析函数