2016-03-07 48 views
0

我有一个查询,我要么与另一个相同的信息加入只有1的标准是不同的,或者我想用1查询拉1列中每个值的前10个结果。你可以看到我有1行注释掉,如果你取消注释并注释掉下面这行是我需要的其他选择语句。我明白我不是最好的解释,但如果你明白并愿意帮助,我将不胜感激。加入2查询有相同的信息只有不同的搜索条件

SELECT coalesce(C.CUSTOMER_GROUP, C.CLIENT_ID) "Customer Group", 
max(c.name) "Name", 
round(sum(CHARGES),2) "Freight Charges", 
round(sum(XCHARGES),2) "Accessorials", 
round(sum(TOTAL_CHARGES),2) "Total Charges", 
max(c.user7) as OR_Data, 
max(cd.data) as test 


FROM TLORDER T, client C, custom_data as cd 
where (src_table_key = t.customer 
and custdef_id = '5') 
and t.bill_to_code = c.CLIENT_ID 
    and t.pick_up_by between '2015-1-1' and current date 
    and T.SITE_ID = 'SITE5' 
    AND (t.INTERFACE_STATUS_F IS NULL OR t.INTERFACE_STATUS_F>-1) 

--and (select cu.data from custom_data as cu where custdef_id = '5' and src_table_key = t.customer) in ('DIRECT', 'BROKER') 
and (select cu.data from custom_data as cu where custdef_id = '5' and src_table_key = t.customer) in ('AGENT', 'INTERLINE') 

and current_Status not in ('CANCL','QUOTE') 


group by coalesce(C.CUSTOMER_GROUP, C.CLIENT_ID) 
order by 5 desc 
fetch first 10 rows only 
with ur 
+0

Ummmm ... [联盟](http://www.w3schools.com/sql/sql_union.asp)? –

回答

0

有可能是一个更好的办法,不过......

SELECT coalesce(C.CUSTOMER_GROUP, C.CLIENT_ID) "Customer Group", 
max(c.name) "Name", 
round(sum(CHARGES),2) "Freight Charges", 
round(sum(XCHARGES),2) "Accessorials", 
round(sum(TOTAL_CHARGES),2) "Total Charges", 
max(c.user7) as OR_Data, 
max(cd.data) as test 


FROM TLORDER T, client C, custom_data as cd 
where (src_table_key = t.customer 
and custdef_id = '5') 
and t.bill_to_code = c.CLIENT_ID 
    and t.pick_up_by between '2015-1-1' and current date 
    and T.SITE_ID = 'SITE5' 
    AND (t.INTERFACE_STATUS_F IS NULL OR t.INTERFACE_STATUS_F>-1) 

and (select cu.data from custom_data as cu where custdef_id = '5' and src_table_key = t.customer) in ('DIRECT', 'BROKER') 
--and (select cu.data from custom_data as cu where custdef_id = '5' and src_table_key = t.customer) in ('AGENT', 'INTERLINE') 

and current_Status not in ('CANCL','QUOTE') 


group by coalesce(C.CUSTOMER_GROUP, C.CLIENT_ID) 
order by 5 desc 
fetch first 10 rows only 
with ur 

-- Using "UNION" will essentially append the 2nd query to the end of the first 
UNION 

SELECT coalesce(C.CUSTOMER_GROUP, C.CLIENT_ID) "Customer Group", 
max(c.name) "Name", 
round(sum(CHARGES),2) "Freight Charges", 
round(sum(XCHARGES),2) "Accessorials", 
round(sum(TOTAL_CHARGES),2) "Total Charges", 
max(c.user7) as OR_Data, 
max(cd.data) as test 


FROM TLORDER T, client C, custom_data as cd 
where (src_table_key = t.customer 
and custdef_id = '5') 
and t.bill_to_code = c.CLIENT_ID 
    and t.pick_up_by between '2015-1-1' and current date 
    and T.SITE_ID = 'SITE5' 
    AND (t.INTERFACE_STATUS_F IS NULL OR t.INTERFACE_STATUS_F>-1) 

--and (select cu.data from custom_data as cu where custdef_id = '5' and src_table_key = t.customer) in ('DIRECT', 'BROKER') 
and (select cu.data from custom_data as cu where custdef_id = '5' and src_table_key = t.customer) in ('AGENT', 'INTERLINE') 

and current_Status not in ('CANCL','QUOTE') 


group by coalesce(C.CUSTOMER_GROUP, C.CLIENT_ID) 
order by 5 desc 
fetch first 10 rows only 
with ur 
+0

我能从这里得到它,谢谢。 – Landers

相关问题