2016-03-07 47 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

相關問題