2010-05-07 98 views
-2

我有以下2个查询及其输出。我需要将2个查询合并为一个查询1个区域以查询2个描述并查询1个empl查询2个employee_id

查询1

select 
types,area,avg(time2)as time,empl,whse,des 
from(select l.control_number_2 as types, l.control_number as area, 
sum(round(cast(DATEDIFF(ss, l.start_tran_time,l.end_tran_time) /60.0 as float),2))over (partition by l.control_number) time2, l.employee_id as empl, l.wh_id as whse , 
(select distinct lu.description from t_lookup lu where lu.description = l.control_number) as des 
from t_cp_work_area_log l 
where start_tran_date >= '2010-05-04 00:00:00.000' 
) t 
group by whse, empl,types, area,des 

查询2

select t.client_code,t.tran_type, t.employee_id,count(t.hu_id)as plts, lu.description,cs.prod_id, ps.fixed_std, ps.dynamic_std,(fixed_std/sum(fixed_std) OVER (PARTITION BY lu.description)) "fs_summed",fixed_std/60 as stand_per_min 
from t_tran_log t, t_lookup lu, t_cp_client_prod_stds cs, t_cp_prod_stds ps 
where lu.text = t.tran_type and lu.source in ('t_cp_prod_stds','t_cp_work_area') 
and t.client_code = cs.client_code and cs.prod_id = ps.prod_id and ps.work_type = lu.text and 
t.start_tran_date >= '2010-05-04 00:00:00.000' 
group by t.tran_type, t.client_code,t.employee_id, cs.prod_id,lu.description,ps.fixed_std, ps.dynamic_std 

输出查询1

types area time empl whse des 
Inventory ADJ 7 TA C1 ADJ 
LOG-ON LOG-ON 55.4 TA C1 LOG-ON 
Outbound LDG 62.7 TA C1 NULL 
Outbound PCKG 11.45 TA C1 NULL 
Receiving RCVG 8.73 TA C1 RCVG 

输出查询2

client_code tran_type emplyee_id plts description prod_id fixed_std dynamic_std fs_summed stand_per_min 
826 853 TA 2 ADJ 13 50 50 1 0.833333 
810 114 TA 1 RCVG 4 50 50 0.555555556 0.833333 
826 114 TA 1 RCVG 11 40 40 0.444444444 0.666666 
+2

使用格式选项使您的查询可读。否则大多数人不会理会这个问题。 – 2010-05-07 13:36:03

+0

你的问题不是很清楚,所以提供帮助很难。你有没有考虑过使用“联盟”?如果是,为什么没有帮助?另外,尝试使用编辑器中的“代码”工具来格式化示例。 – 2010-05-07 13:37:47

+0

什么是数据库系统?什么版本? – 2010-05-07 13:39:47

回答

1

虽然我很难理解你的问题,但我想你只是问如何做一个加入。

SELECT * -- better to explicitly list the columns you need here 
    FROM 
    (<text of query1>) q1 
    JOIN 
    (<text of query2>) q2 
    ON q1.area = q2.description AND q1.empl = q2.employee_id 
+0

非常感谢!这很快,正是我所需要的。很显然,我对sql很陌生,并认为这将是一个简单的答案。我不愿意寻求帮助,但在尝试多次失败后,我很高兴我做到了。再次感谢 – Tina 2010-05-07 13:57:40