两个包含group by 语句的select 语句的连接查询

来源:互联网 发布:在线英语培训软件 编辑:程序博客网 时间:2024/06/09 23:13

格式:

select sum(t1.字段1), 计算2,t1.字段2

from 表1 t1

inner join 

(select sum(t2字段2) as 计算2

from 表2 

group by t2.关键字1) t2

on t1.id = t2.id

group by t1.字段2,计算2


关键是第一个select的group by 语句要放在后面


例:
select  t1.id,trydate0 
,sum(ProcessNeedMinutes) as TotalMinutes ,DoneMinutes
from tb_partprocess  t1
inner join tb_order  od on od.id = t1.id


left join 
(select sum(ProcessNeedMinutes) as DoneMinutes
from tb_partprocess pp where startdate is not null and enddate is not null
group by pp.id) as t2
on  t1.id = t2.id 

where 
 TryDate0>='2012-5-1 0:00:00' and  TryDate0<='2012-6-1 0:00:00' 
group by t1.id,t1.partno,TryDate0,DoneMinutes

原创粉丝点击