SQL [message #635845] |
Thu, 09 April 2015 20:20 |
jen804
Messages: 22 Registered: March 2009 Location: USA
|
Junior Member |
|
|
Hi,
I want to get annual sales and annual gp from the second query to add to the first query but not sure how to do it in a subquery or the best way to do so. Could someone help? Thank you!!
SELECT ch.bus_unit,
ch.sys_pltfrm,
ch.bus_pltfrm,
ch.CURRENT_RECORD_FLG,
CH.DIM_PC_ACCT_MGR_MG_ID,
Ch.dim_cust_hist_mg_id,
am.ACCT_TERR_NUM,
am.acct_mgr_empl_id,
am.acct_mgr_name,
ch.cust_num,
ch.cust_name,
ch.effective_dt,
ch.expiration_dt,
LAG(ch.effective_dt,1) OVER (PARTITION BY ch.cust_num ORDER BY ch.effective_dt)AS PREV_date,
LAG(am.acct_mgr_empl_id,1) OVER (PARTITION BY ch.cust_num ORDER BY ch.effective_dt)AS PREV_AM_ID,
LAG(am.acct_mgr_name,1) OVER (PARTITION BY ch.cust_num ORDER BY ch.effective_dt)AS PREV_AM
FROM mmsedw.v_dim_customer_hist_mg ch,
mmsedw.v_dim_pc_acct_mgr_mg am
WHERE ch.dim_pc_acct_mgr_mg_id=am.dim_pc_acct_mgr_mg_id
and ch.effective_dt between '1-mar-2015' and '31-mar-2015'
left join
select
c.cust_num,
c.bus_unit,
c.sys_pltfrm,
sum(f.ext_sls_pss + f.ext_sls_mms) as annual_Sales,
sum(f.ext_sell_mrgn_pss + f.ext_mrgn_cogs_rep_mms) as annual_gp
from mmsedw.v_fact_sales_mg f,
mmsedw.v_dim_customer_mg c,
mmsedw.v_dim_item_mg i,
mmsedw.v_dim_rebate_type r,
mmsedw.v_dim_transaction_type_mg t
where f.dim_cust_mg_id = c.dim_cust_mg_id
and f.dim_item_mg_id = i.dim_item_mg_id
and f.dim_rbt_type_id = r.dim_rbt_type_id
and f.dim_gl_dt_id between 20130401 and 20140331
and r.rbt_type <> 'N'
and i.fin_prod_sub_grp_dsc not in ('Flu','Lab Consulting','HIT')
and f.bus_pltfrm = 'PC'
and f.dim_transaction_type_mg_id = t.dim_transaction_type_mg_id
and t.sls_rpt_flg = 'Y'
group by
c.cust_num,
c.bus_unit,
c.sys_pltfrm
|
|
|
|
|