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

Join 2 Query's with The same information only different search criteria

我有一个查询,我想与另一个具有相同信息的查询连接,只有 1 个条件不同,或者我想使用 1 个查询为 4 个值中的每一个提取前 10 个结果在 1 列中。你可以看到我有 1 行被注释掉了,如果你取消注释并注释掉下面那行,那是我需要的另一个 Select 语句。我知道我不是最好地解释这一点,但如果你理解并愿意提供帮助,我将不胜感激。

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

可能有更好的方法,但是...

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