兩個包含group by 語句的select 語句的連接查詢

格式:

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

發佈了41 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章