sql - Join two select queries horizontally in Postgresql -
i have following 2 queries:
query #1:
(select pl.c_project_id, pl.c_projectphase_id, pl.c_projecttask_id, pl.m_product_id, pj.name projectname, ph.name phasename, pt.name taskname, pd.name prodname, round(pl.plannedqty, 2) planqty, round(pl.plannedprice, 2) planrate, round(pl.plannedamt, 2) planamt adempiere.c_projectline pl left join adempiere.c_project pj on pl.c_project_id = pj.c_project_id left join adempiere.c_projectphase ph on pl.c_projectphase_id = ph.c_projectphase_id left join adempiere.c_projecttask pt on pl.c_projecttask_id = pt.c_projecttask_id left join adempiere.m_product pd on pl.m_product_id = pd.m_product_id pl.c_project_id = 1000001 , pl.ad_client_id = 1000000 order ph.c_projectphase_id, pt.c_projecttask_id)
output is: 11 columns , 16 rows
query #2:
(select fa.c_project_id, fa.c_projectphase_id, fa.c_projecttask_id, fa.m_product_id, pj.name costprojectname, ph.name costphasename, pt.name costtaskname, pd.name costprodname, abs(fa.qty) costqty, round((fa.amtacctdr/fa.qty), 2) costrate, round(sum(fa.amtacctdr), 0) costamt adempiere.fact_acct fa left join adempiere.c_project pj on fa.c_project_id = pj.c_project_id left join adempiere.c_projectphase ph on fa.c_projectphase_id = ph.c_projectphase_id left join adempiere.c_projecttask pt on fa.c_projecttask_id = pt.c_projecttask_id left join adempiere.m_product pd on fa.m_product_id = pd.m_product_id fa.c_project_id = 1000001 , (fa.gl_category_id = 1000006 or fa.gl_category_id = 1000005) , fa.qty > 0 , fa.c_project_id not null group fa.m_product_id, fa.c_project_id, fa.c_projectphase_id, fa.c_projecttask_id, fa.qty, fa.amtacctdr, pj.name, ph.name, pt.name, pd.name)
output is: 11 columns , 6 rows
i want join these queries horizontally, display columns rows should not duplicate. when apply union join them result shows duplicate rows. how can cope issue?
you should able join queries this:
select * ( <your first query here> ) tbl1 join ( <your second query here> ) tbl2 on tbl1.c_project_id = tbl2.c_project_id , tbl1.c_projectphase_id = tbl2.c_projectphase_id -- might add or , tbl1.c_projecttask_id = tbl2.c_projecttask_id -- remove join criteria , tbl1.m_product_id = tbl2.m_product_id -- here
Comments
Post a Comment