SELECT IIf(IsNumeric([dbarc].[acct_num]),IIf(Val([dbarc].[acct_num])>=680000,"70-","44-"),"44-") & [dbarc].[acct_num] AS cl_id, format(dbarc.ret_date-datepart("d",ret_date),'mm/dd/yyyy') AS post_date, 'M' AS post_type, IIf(IsNull([dbsr].[gim2000_id_2]),NZ(Switch([dbarc.rcat_cd]="TOTA","TOTAL",
[dbarc.rcat_cd]="TOTM","TOTM",
[dbarc.rcat_cd]="EQTY","EQTYALL",
[dbarc.rcat_cd]="FIXED","FIXEDALL"),[dbarc.rcat_cd]),[dbsr.gim2000_id_2]) AS perf_class, 'F' AS post_curr_code, 'N/A' AS sloc_cur, 'N' AS perf_cash, 0 AS mkt_val, 0 AS accr_inc, 0 AS accr_int, 0 AS accr_div, 0 AS d_net_tot_ror, 0 AS d_grs_tot_ror, 0 AS e_net_tot_ror, 0 AS e_grs_tot_ror, IIf(([dbsr].[gim2000_ID_3] & "")="N", [dbarc].[ret_pct],Null) AS p_net_tot_ror, IIf(IsNull([dbsr].[gim2000_ID_3]), [dbarc].[ret_pct],Null) AS p_grs_tot_ror, 0 AS d_net_prn_ror, 0 AS d_grs_prn_ror, 0 AS e_net_prn_ror, 0 AS e_grs_prn_ror, 0 AS p_net_prn_ror, 0 AS p_grs_prn_ror, 0 AS d_net_inc_ror, 0 AS d_grs_inc_ror, 0 AS e_net_inc_ror, 0 AS e_grs_inc_ror, 0 AS p_net_inc_ror, 0 AS p_grs_inc_ror, 0 AS flow, 0 AS contribution, 0 AS net_fees, 0 AS grs_fees, 0 AS bond_int, 0 AS txn_costs, 0 AS withhold, 0 AS income, 0 AS additions, 0 AS withdrawals, 0 AS interest, 0 AS dividends, 0 AS bond_int1, 0 AS grs_fees1, 0 AS net_fees1, 0 AS cost, 0 AS grs_wtd_flow, 0 AS net_wtd_flow, 0 AS txn_costs1, 0 AS withhold1, 0 AS wtd_bond_int, 0 AS wtd_txn_costs, 0 AS wtd_withhold
INTO tbl_output
FROM dbo_acct AS dba, dbo_acct_ret_category AS dbarc, dbo_spec_rpt_1 AS dbsr
WHERE dbarc.acct_num = dba.acct_num and
dbsr.acct_num = dba.acct_num and
dbarc.acct_num = dbsr.acct_num and
dbarc.rcat_cd = dbsr.rcat_cd and
dbsr.gim2000_id_1 is null and
dbsr.availability = 'X'
ORDER BY dbarc.ret_date, dbarc.acct_num;