Sql 联接2查询';s具有相同的信息,但搜索条件不同

Sql 联接2查询';s具有相同的信息,但搜索条件不同,sql,db2,Sql,Db2,我有一个查询,我想加入另一个具有相同信息的查询,但条件中只有一个不同,或者我想使用1个查询为1列中的4个值中的每一个提取前10个结果。你可以看到我已经注释掉了1行,如果你取消注释,并注释掉下面的那行,这是我需要的另一个Select语句。我明白我不是在最好地解释这一点,但如果你理解并愿意帮助我,我将不胜感激 SELECT coalesce(C.CUSTOMER_GROUP, C.CLIENT_ID) "Customer Group", max(c.name) "Name", round(sum(C

我有一个查询,我想加入另一个具有相同信息的查询,但条件中只有一个不同,或者我想使用1个查询为1列中的4个值中的每一个提取前10个结果。你可以看到我已经注释掉了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
嗯?