加入2个查询与相同的信息只有不同的搜索条件

时间:2016-03-07 18:26:23

标签: sql db2

我有一个查询,我要么想要与另一个具有相同信息的连接,只有一个标准不同,或者我想使用1个查询为1中的4个值中的每一个提取前10个结果柱。您可以看到我有1行注释掉,如果您取消注释并注释掉下面的行,那就是我需要的另一个Select Statement。我理解我并不是最好的解释,但如果你理解并愿意帮助我会很感激。

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

1 个答案:

答案 0 :(得分: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