|
anchoredwisdom
Starting Member
22 Posts |
Posted - 2011-11-07 : 07:27:58
|
| Hello We have two tables where we have order amount and con amount.In each row we have region,subregion,product and these two amounts.We are trying to create one sql where we get both amounts and corresponding regions,products.The idea is to retain either side amounts.I wrote the below sql but i am getting duplicate.The logic is this i created one sql where i bring sum of orderamount by region,product,fiscalweek etc. SQL 1another sql for bringing sum of conamount by region,product,fiscalweek. SQL 2I right joined the first sql with datemas and retain all fiscalweeks and then the resultant sql joined with the product table finally i did a full outer join with the sql which brings order amount I am getting duplicate values.Please share your thoughts and let me know if something is wrong in my approachselect* from (select distincts1.region_name,s1.sub_region_name,s3.tier1,s3.tier2,s3.tier3,s3.tier4,s1.orderamount,s2.fiscal_week_name,s4.camount from(selectregion_name,sub_region_name,Trunc(sum(net_amount)) as orderamount,fiscal_week_name,tier4from cx_sfdc_orders_fact ordersgroup by region_name,sub_region_name,fiscal_week_name,tier4) S1RIGHT JOIN(select distinct fiscal_week_name,fiscal_month_name,fiscal_quarter_namefrom date_mas ) S2ON (S1.fiscal_week_name = s2.fiscal_weeK_name)LEFT JOIN(SELECT DISTINCT tier1,tier2,tier3,PNL tier4 from CX_PROD_HIER_XREF) s3on s1.tier4 = s3.tier4FULL OUTER JOIN(select region_name,sub_region_name,pcng_ctgry_nm,fiscal_week,sum(oprtnty_ttl_pr_cnvrtd_val) camount,tier_4_DEfrom cx_sfdc_opportunity_detwhere PCNG_CTGRY_NM in ('BestCase','Closed')group by region_name,sub_region_name,pcng_ctgry_nm,fiscal_week,tier_4_dE ) s4on (S3.TIER4 = s4.tier_4_DE)ands4.fiscal_week=s2.fiscal_week_nameands4.region_name =s1.region_nameands4.sub_region_name =s1.sub_region_name)where camount is null |
|