mysql - Combining two SQL queries with multiple tables and left joins -
mysql - Combining two SQL queries with multiple tables and left joins -
is there way can combine these 2 queries:
first query
select top 100 work.pzinskey, work.pyid, party.macid, party.otherpartyid, party.customeremail, account.accountnumber, account.accountname, account.advisercode, account.advisername, account.dealercode, account.dealername, account.primaryaccount, account.productcategory, account.productcode, account.productdescription, account.registeredstate, document.udocid worktable work, partytable party, accounttable account, documenttable document, notestable notes work.pzinskey = party.pxinsindexedkey , work.pzinskey = account.pxinsindexedkey , work.pyid = document.caseid
and sec query
select top 100 businessareatbl.businessarea, processtbl.process, subprocesstbl.subprocess worktable work left outer bring together (select distinct product_id businessarea_id, product businessarea casetypestable) businessareatbl on work.requestbusinessarea#1 = businessareatbl.businessarea_id left outer bring together (select distinct process_id, process, product_id businessarea_id casetypestable) processtbl on work.requestprocess#1 = processtbl.process_id , processtbl.businessarea_id = work.requestbusinessarea#1 left outer bring together (select distinct subprocess_id, subprocess, product_id businessarea_id, process_id casetypestable) subprocesstbl on work.requestsubprocess#1 = subprocesstbl.subprocess_id , subprocesstbl.businessarea_id = work.requestbusinessarea#1 , subprocesstbl.process_id = work.requestprocess#1
it's 2 queries produce separate results, each query includes info worktable. in 2nd query, worktable info derived casetypestable.
i want businessarea, process, , subprocess fields included results of first query.
thanks in advance help.
this should work:
(select top 100 w.pzinskey, w.pyid, p.macid, p.otherpartyid, p.customeremail, a.accountnumber, a.accountname, a.advisercode, a.advisername, a.dealercode, a.dealername, a.primaryaccount, a.productcategory, a.productcode, a.productdescription, a.registeredstate, d.udocid worktable w left bring together partytable p on w.pzinskey = p.pxinsindexedkey left bring together accounttable on w.pzinskey = a.pxinsindexedkey left bring together documenttable d on w.pyid = d.caseid) union (select top 100 ba.businessarea, pr.process, spr.subprocess worktable w left outer bring together (select distinct product_id businessarea_id, product businessarea casetypestable) businessareatbl ba on w.requestbusinessarea#1 = ba.businessarea_id left outer bring together (select distinct process_id, process, product_id businessarea_id casetypestable) processtbl pr on w.requestprocess#1 = pr.process_id , pr.businessarea_id = w.requestbusinessarea#1 left outer bring together (select distinct subprocess_id, subprocess, product_id businessarea_id, process_id casetypestable) subprocesstbl spr on w.requestsubprocess#1 = spr.subprocess_id , spr.businessarea_id = w.requestbusinessarea#1 , spr.process_id = w.requestprocess#1))
use keyword union
combine 2 or more seperate select
statements.
mysql sql sql-server
Comments
Post a Comment