How to correctly place brackets in a CASE statement? - sql

Case Statement:
(CASE WHEN v_cur.order_type != 'STK'
AND v_cur.lot_net_cost_flag = 'Y' THEN 0
ELSE ( (v_cur.unit_costs_amount * v_cur.line_quantity_ordered)
/ CASE WHEN v_cur.unit_of_measure_code = 'E' THEN 1
WHEN v_cur.unit_of_measure_code = 'C' THEN 100
WHEN v_cur.unit_of_measure_code = 'M' THEN 1000
ELSE 1 END ) v_dt_unit_of_measure_code_val )
END v_dt_cost_amount

case
when v_cur.order_type != 'STK' and v_cur.lot_net_cost_flag = 'Y' then 0
else v_cur.unit_costs_amount * v_cur.line_quantity_ordered /
case v_cur.unit_of_measure_code
when 'E' then 1
when 'C' then 100
when 'M' then 1000
else 1
end
end as v_dt_cost_amount
CASE syntax does not require any brackets. You only need them for mixed OR/AND logical conditions and for non-default arithmetic precedence, which you don't have in the above expression because (x * y) / z is the same thing as x * y / z.

delete v_dt_unit_of_measure_code_val
CASE WHEN v_cur.order_type != 'STK'
AND v_cur.lot_net_cost_flag = 'Y' THEN 0
ELSE ( (v_cur.unit_costs_amount * v_cur.line_quantity_ordered)
/ CASE WHEN v_cur.unit_of_measure_code = 'E' THEN 1
WHEN v_cur.unit_of_measure_code = 'C' THEN 100
WHEN v_cur.unit_of_measure_code = 'M' THEN 1000
ELSE 1 END )
END v_dt_cost_amount

Related

ERROR: CASE types numeric and character varying cannot be matched

Hi I am new in the amazon redshift. Don't have much experience on it. I am running below query in postgre sql but getting some error. Can anyone please help me to correct the query so that i can get the correct result-
The query was in oracle format which I converted into redshift format and tried to run it but getting error.
Query:
select * from
(
SELECT '01' AS record_type,
a.fi_doc_co_cd AS fi_doc_co_cd,
CASE When a.action_code = 'A' THEN 'A' ELSE 'C' END AS action_code,
a.fi_doc_no AS fi_doc_no,
a.fi_doc_fiscal_yr AS fi_doc_fiscal_yr,
a.bu_cd AS business_unit, 00 AS line_number,
00 AS split_line_number, a.row_updt_tms AS row_added_date,
a.suplr_id AS suplr_id, a.vndr_invc_no AS vndr_invc_no,
CASE when a.invc_flag =
'X' THEN a.invc_gross_amt
ELSE a.invc_gross_amt * -1
END AS amount,
CASE when a.invc_flag =
'X' THEN a.sls_tax_amt
ELSE a.sls_tax_amt * -1
END AS sales_tax_amount,
CASE when a.invc_flag =
'X' THEN a.frt_amt
ELSE a.frt_amt * -1
END AS freight_amount, 0 AS use_tax_amount,
CASE when a.invc_flag =
'X' THEN a.trans_invc_gross_amt
ELSE a.trans_invc_gross_amt * -1
END AS trans_amount,
a.trans_invc_doc_currcy AS trans_currcy_cd,
CASE when a.invc_flag=
'X' THEN a.trans_sls_tax_amt
ELSE a.trans_sls_tax_amt * -1
END AS trans_sales_tax_amt,
CASE when a.invc_flag =
'X' THEN a.trans_frt_amt
ELSE a.trans_frt_amt * -1
END AS trans_freight_amount,
NULL AS trans_use_tax_amount, a.exchange_rate AS exchange_rate,
a.po_suplr_id AS po_supplier, lpad(trim(a.po_no), 9, '0' ) AS po_no,
a.po_type AS po_type, a.fi_doc_post_dt AS invoice_post_dt,
a.invc_crt_dt AS invoice_create_date,
a.invc_doc_type_cd AS invoice_document_type,
a.invc_typ_cd AS invoice_type, a.invc_doc_currcy AS invoice_currency,
a.pymt_terms AS payment_terms, a.hdr_mismatch_cd AS mismatch_code,
NULL AS mfr_part_no, NULL AS li_descn_txt, NULL AS po_ln_no,
NULL AS quantity, NULL AS a1099_cd, NULL AS uom_no,
NULL AS taxg_authty_no, NULL AS tax_cd,
NULL AS ap_department_costcenter, NULL AS ap_account,
NULL AS ap_referencenumber, NULL AS ap_companycode,
NULL AS ap_internalordernumber, NULL AS ap_profitcenterid,
NULL AS lcl_fran_cd, NULL AS lcl_div_locn_cd, NULL AS lcl_deptm_cd,
NULL AS lcl_acct_cd, NULL AS lcl_sub_cd, NULL AS lcl_refnc_cd,
NULL AS lcl_intrn_co_cd, NULL AS lcl_plnt_cd,
NULL AS lcl_intrn_ord_no, NULL AS lcl_wbs_no, NULL AS lcl_netw_no,
NULL AS lcl_asset_no, NULL AS lcl_prft_ctr_no,
NULL AS lcl_prftblty_seg_no, NULL AS lcl_prod_cd,
NULL AS lcl_cust_no, NULL AS lcl_end_cust_no, a.sap_tcode AS tcode,
CASE when a.sap_tcode=
'MR8M' THEN a.cancel_ref_doc
ELSE NULL
END AS cancel_reference_doc,
a.clrng_doc_no, a.clrng_doc_fiscal_yr, a.clrng_doc_co_cd,
a.bpf_comment
FROM sapncdb_spectrum.ap_invc a
WHERE ( CURRENT_USER =
(SELECT CURRENT_USER
WHERE CURRENT_USER IN (SELECT a.svc_acct_nm
FROM sapncdb_spectrum.svc_acct_bu a
WHERE CURRENT_USER = a.svc_acct_nm AND a.bu_cd = '*'))
OR a.bu_cd IN (SELECT svc_acct_bu.bu_cd
FROM sapncdb_spectrum.svc_acct_bu
WHERE svc_acct_bu.svc_acct_nm = (SELECT CURRENT_USER
))
)
UNION
SELECT '02', a.fi_doc_co_cd, CASE a.action_code WHEN 'A' THEN 'A' ELSE 'C' END,
a.fi_doc_no, a.fi_doc_fiscal_yr, a.bu_cd,
CASE b.src_system_invc_ln_no
WHEN 0 THEN b.invc_ln_no
ELSE b.src_system_invc_ln_no
END,
00, a.row_updt_tms, a.suplr_id, a.vndr_invc_no,
CASE a.invc_flag WHEN 'X' THEN b.invc_ln_amt ELSE b.invc_ln_amt * -1 END,
CASE a.invc_flag WHEN 'X' THEN b.sls_tax_amt ELSE b.sls_tax_amt * -1 END,
CASE a.invc_flag WHEN 'X' THEN b.frt_amt ELSE b.frt_amt * -1 END,
CASE a.invc_flag WHEN 'X' THEN b.use_tax_amt ELSE b.use_tax_amt * -1 END,
CASE a.invc_flag
WHEN 'X' THEN b.trans_invc_ln_amt
ELSE b.trans_invc_ln_amt * -1
END,
b.trans_currcy_cd,
CASE a.invc_flag
WHEN 'X' THEN b.trans_sls_tax_amt
ELSE b.trans_sls_tax_amt * -1
END,
CASE a.invc_flag WHEN 'X' THEN b.trans_frt_amt ELSE b.trans_frt_amt * -1 END,
CASE a.invc_flag
WHEN 'X' THEN b.trans_use_tax_amt
ELSE b.trans_use_tax_amt * -1
END,
a.exchange_rate, a.po_suplr_id, lpad(trim(a.po_no), 9, '0' ),
a.po_type, a.fi_doc_post_dt, a.invc_crt_dt, a.invc_doc_type_cd,
a.invc_typ_cd, a.invc_doc_currcy,
a.pymt_terms, b.prc_mismatch || b.qty_mismatch, b.mfr_part_no,
b.li_descn_txt, b.po_ln_no, b.li_qty, b.a1099_cd, b.uom_no,
b.taxg_authty_no, b.tax_cd, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL,
a.clrng_doc_no, a.clrng_doc_fiscal_yr, a.clrng_doc_co_cd,
a.bpf_comment
FROM sapncdb_spectrum.ap_invc a, sapncdb_spectrum.ap_invc_ln b
WHERE a.fi_doc_no = b.fi_doc_no
AND a.fi_doc_fiscal_yr = b.fi_doc_fiscal_yr
AND a.fi_doc_co_cd = b.fi_doc_co_cd
AND a.reqst_id = b.reqst_id
AND ( CURRENT_USER =
(SELECT CURRENT_USER
WHERE CURRENT_USER IN (SELECT a.svc_acct_nm
FROM sapncdb_spectrum.svc_acct_bu a
WHERE CURRENT_USER = a.svc_acct_nm AND a.bu_cd = '*'))
OR a.bu_cd IN (SELECT svc_acct_bu.bu_cd
FROM sapncdb_spectrum.svc_acct_bu
WHERE svc_acct_bu.svc_acct_nm = (SELECT CURRENT_USER
))
)
UNION
SELECT '03', a.fi_doc_co_cd, CASE a.action_code WHEN 'A' THEN 'A' ELSE 'C' END, a.fi_doc_no,
a.fi_doc_fiscal_yr, a.bu_cd,
CASE b.src_system_invc_ln_no
WHEN 0 THEN b.invc_ln_no
ELSE b.src_system_invc_ln_no
END,
CASE c.src_system_invc_splt_ln_no
WHEN 0 THEN c.invc_splt_ln_no
ELSE c.src_system_invc_splt_ln_no
END,
a.row_updt_tms, a.suplr_id, a.vndr_invc_no,
CASE a.invc_flag
WHEN 'X' THEN c.invc_splt_ln_amt
ELSE c.invc_splt_ln_amt * -1
END,
CASE a.invc_flag WHEN 'X' THEN c.sls_tax_amt ELSE c.sls_tax_amt * -1 END,
CASE a.invc_flag WHEN 'X' THEN c.frt_amt ELSE c.frt_amt * -1 END,
CASE a.invc_flag WHEN 'X' THEN c.use_tax_amt ELSE c.use_tax_amt * -1 END,
CASE a.invc_flag
WHEN 'X' THEN c.trans_invc_splt_ln_amt
ELSE c.trans_invc_splt_ln_amt * -1
END,
b.trans_currcy_cd,
CASE a.invc_flag
WHEN 'X' THEN c.trans_sls_tax_amt
ELSE c.trans_sls_tax_amt * -1
END,
CASE a.invc_flag WHEN 'X' THEN c.trans_frt_amt ELSE c.trans_frt_amt * -1 END,
CASE a.invc_flag
WHEN 'X' THEN c.trans_use_tax_amt
ELSE c.trans_use_tax_amt * -1
END,
a.exchange_rate, a.po_suplr_id, lpad(trim(a.po_no), 9, '0' ),
a.po_type, a.fi_doc_post_dt,
a.invc_crt_dt, a.invc_doc_type_cd, a.invc_typ_cd, a.invc_doc_currcy,
a.pymt_terms, b.prc_mismatch || b.qty_mismatch, b.mfr_part_no,
b.li_descn_txt, b.po_ln_no, c.invc_splt_ln_qty, b.a1099_cd, b.uom_no,
b.taxg_authty_no, b.tax_cd, c.ap_department_costcenter, c.ap_account,
c.ap_referencenumber, c.ap_companycode, c.ap_internalordernumber,
c.ap_profitcenterid, c.lcl_fran_cd, c.lcl_div_locn_cd,
c.lcl_deptm_cd, c.lcl_acct_cd, c.lcl_sub_cd, c.lcl_refnc_cd,
c.lcl_intrn_co_cd, c.lcl_plnt_cd, c.lcl_intrn_ord_no, c.lcl_wbs_no,
c.lcl_netw_no, c.lcl_asset_no, c.lcl_prft_ctr_no,
c.lcl_prftblty_seg_no, c.lcl_prod_cd, c.lcl_cust_no,
c.lcl_end_cust_no, NULL, NULL,
a.clrng_doc_no, a.clrng_doc_fiscal_yr, a.clrng_doc_co_cd,
a.bpf_comment
FROM sapncdb_spectrum.ap_invc_ln b,
sapncdb_spectrum.ap_invc a,
sapncdb_spectrum.ap_invc_ln_acct_distn c
WHERE a.fi_doc_no = b.fi_doc_no
AND a.fi_doc_no = c.fi_doc_no
AND b.fi_doc_no = c.fi_doc_no
AND a.fi_doc_co_cd = b.fi_doc_co_cd
AND a.fi_doc_co_cd = c.fi_doc_co_cd
AND b.fi_doc_co_cd = c.fi_doc_co_cd
AND a.fi_doc_fiscal_yr = b.fi_doc_fiscal_yr
AND a.fi_doc_fiscal_yr = c.fi_doc_fiscal_yr
AND b.fi_doc_fiscal_yr = c.fi_doc_fiscal_yr
AND a.reqst_id = b.reqst_id
AND a.reqst_id = c.reqst_id
AND b.reqst_id = c.reqst_id
AND b.invc_ln_no = c.invc_ln_no
AND ( CURRENT_USER =
(SELECT CURRENT_USER
WHERE CURRENT_USER IN (SELECT a.svc_acct_nm
FROM sapncdb_spectrum.svc_acct_bu a
WHERE CURRENT_USER = a.svc_acct_nm AND a.bu_cd = '*'))
OR a.bu_cd IN (SELECT svc_acct_bu.bu_cd
FROM sapncdb_spectrum.svc_acct_bu
WHERE svc_acct_bu.svc_acct_nm = (SELECT CURRENT_USER
))
)
ORDER BY 2,4,5,6,9,7,8,1
) s
where rownum < 100;
Error:
An error occurred when executing the SQL command:
select * from
(
SELECT '01' AS record_type,
a.fi_doc_co_cd AS fi_doc_co_cd,
CASE When a.action_code = 'A' THEN 'A' ELSE 'C' END A...
ERROR: CASE types numeric and character varying cannot be matched [SQL State=42804]
1 statement failed.
Execution time: 0.63s

If else ladder using Case in sql server

I am trying to write if elif ladder in SQL but I am finding it little difficult to do that. My ladder looks like below.
IF (MortgageProduct.FreeLegalFees != NULL) THEN
IF(MortgageProduct.FreeLegalFees==1) THEN
Return TRUE as FreeLegalFees
ELSE
Return FALSE as FreeLegalFees
ELSE
IF (ConveyancerFee!=NULL) THEN
IF ConveyancerFee.AMOUNT = 0.0 then
Return TRUE as FreeLegalFees
ELSE
Return FALSE as FreeLegalFees
END IF
ELSE
IF (LenderFee!=NULL) THEN
IF LenderFee.AMOUNT = 0.0 then
Return TRUE as FreeLegalFees
ELSE
Return FALSE as FreeLegalFees
END IF
ELSE
Return TRUE as FreeLegalFees
END IF
I know that I have to use Case for that in SQL but I am confuse in writing Case statement in sql. Can somebody please help me to frame this ladder in sql server.
This is far more succinct. I have also aliased what I assume are your table names:
CASE WHEN MP.FreeLegalFees IS NOT NULL THEN IIF(MP.FreeLegalFees = 1,1,0)
WHEN CF.AMOUNT IS NOT NULL THEN IIF(CF.AMOUNT = 0,1,0)
WHEN LF.Amount IS NOT NULL THEN IIF(LF.AMOUNT = 0,1,0)
ELSE 1 END
/*
MP MortgageProduct
CF ConveyancerFee
LF LenderFee
*/
I would write this as:
(case when mp.FreeLegalFees = 1 then 1
when mp.FreeLegalFees is not null then 0
when cf.amount = 0.0 then 1
when cf.amount is not null then 0
when lf.amount = 0.0 then 1
when lf.amount is not null then 0
else 1
end) as FreeLegalFees
CASE
WHEN MP.FREELEGALFEES = 1 THEN 1
WHEN MP.CONVEYANCERLEGALFEESET IS NOT NULL THEN
(
CASE
WHEN (
(SELECT AMOUNT FROM CONVEYANCERLEGALFEE WHERE LOCATION=#p_iLocation
and TYPEOFAPPLICATION=#p_iTypeOfApplication
and TYPEOFBUYER=#p_iTypeOfBuyer and MAXIMUMVALUE=100000
and CONVEYANCERLEGALFEESETSTRTDATE <= (SELECT GETDATE())
and CONVEYANCERLEGALFEESET = MP.CONVEYANCERLEGALFEESET ) = 0.0)
THEN 1
ELSE 0
END
)
WHEN ((Select COUNT(FEESET) FROM LEGALFEE WHERE TYPEOFAPPLICATION=#p_iTypeOfApplication
AND FEETYPE = (Select VALUEID from COMBOVALUE where VALUENAME='Redeeming')) >= 1 ) THEN
(
CASE
WHEN (
(SELECT AMOUNT FROM LEGALFEE
WHERE TYPEOFAPPLICATION = #p_iTypeOfApplication
AND FEETYPE= (SELECT VALUEID FROM COMBOVALUE WHERE VALUENAME='Redeeming')) = 0.0)
THEN 1
ELSE 0
END
)
ELSE 1
This is the solution which works perfect. Thank you all.
CASE
WHEN MortgageProduct.FreeLegalFees IS NOT NULL
THEN (
CASE
WHEN MortgageProduct.FreeLegalFees = 1
THEN 'TRUE'
ELSE 'FALSE'
END
)
WHEN ConveyancerFee IS NOT NULL
THEN (
CASE
WHEN ConveyancerFee.AMOUNT = 0.0
THEN 'TRUE'
ELSE 'FALSE'
END
)
WHEN LenderFee IS NOT NULL
THEN (
CASE
WHEN LenderFee.AMOUNT = 0.0
THEN 'TRUE'
ELSE 'FALSE'
END
)
ELSE 'TRUE'
END AS FreeLegalFees

SSMS 2008 Conversion failed when converting the varchar value '2.126' to data type int

I'm new to SQL and I'm using SQL Server 2008 Management Studio. The below code isn't working due to a decimal being converted to an INT. From similar issues I can see that I need to use CONVERT, but unsure how to use this in my code?
DROP TABLE [DBO].[STEP9_OUTPUT]
GO
SELECT
S8.*,
CAST(CASE
WHEN S8.ERROR_TRANSACTION = 'N'
THEN NOERROR_VALUE
WHEN S8.SOURCE_SYSTEM_IND = 'P'
THEN P_VALUE
ELSE A_VALUE
END AS FLOAT) AS CORRECTED_PRICE
INTO STEP9_OUTPUT
FROM
(SELECT
S8.*,
CASE
WHEN S8.SOURCE_SYSTEM_IND = 'P' AND DIVRATE = 'Y'
THEN P_VALUE_DIV
WHEN S8.SOURCE_SYSTEM_IND = 'P' AND DIVRATE = 'N'
THEN P_VALUE_MULT
ELSE NULL
END AS P_VALUE
FROM
(SELECT
S8.*,
CASE
WHEN S8.SOURCE_SYSTEM_IND = 'P' AND
S8.ERROR_TRANSACTION = 'Y' AND
DIVRATE = 'N' AND S8.FN > 0
THEN S8.FN
WHEN S8.UNITS_INFUND_FOR_TRADE > 0
THEN S8.FTP
WHEN S8.FJFG = 'N' AND S8.RATE IS NULL
THEN S8.FB / 100 * 1
WHEN S8.FJFG = 'N' AND S8.RATE IS NOT NULL
THEN S8.FB / 100 * S8.RATE
WHEN S8.FJFG <> 'N' AND S8.RATE IS NULL
THEN 1 * 1
WHEN S8.FJFG <> 'N' AND S8.RATE IS NOT NULL
THEN 1 * S8.RATE
ELSE NULL
END AS P_VALUE_MULT,
CASE
WHEN S8.SOURCE_SYSTEM_IND = 'P' AND
S8.ERROR_TRANSACTION = 'Y' AND
[DIVRATE] = 'Y' AND S8.FN > 0
THEN S8.FN
WHEN S8.UNITS_INFUND_FOR_TRADE > 0
THEN S8.FTP
WHEN S8.FJFG = 'N' AND S8.RATE IS NULL
THEN S8.FB / 100
WHEN S8.FJFG = 'N' AND S8.RATE IS NOT NULL
THEN S8.FB / (100 / S8.RATE)
ELSE NULL
END AS P_VALUE_DIV
FROM
(SELECT
S8.*,
CASE
WHEN S8.ERROR_TRANSACTION = 'N'
THEN FUND_UNIT_PRICE_FOR_TRADE
ELSE NULL
END AS NOERROR_VALUE,
CASE
WHEN S8.SOURCE_SYSTEM_IND = 'A' AND
S8.ERROR_TRANSACTION = 'Y' AND
S8.ASSET_CODE = 'XX'
THEN S8.FUND_BID_VALUE
ELSE NULL
END AS A_VALUE,
CASE
WHEN S8.FACURR = 'SEK' OR S8.FACURR = 'JPY'
THEN 'Y'
ELSE 'N'
END AS DIVRATE
FROM
STEP8_OUTPUT S8) S8
) S8
) S8
SELECT CAST(CAST('2.126' as decimal) as int)

How do I escape <> for this SQL in a XML file

I am trying to store the following sql in a bean within an XML file. I am trying to escape the greater than and less than signs, however I seem to still be getting an error in Eclipse. I must not understand how to properly escape these signs. I count six less than and greater than signs, which I have attempted to escape.
Here's my attempt:
<property name="SQL" value="<><><><><><>"
"SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') SQL_RUN_DT
,'STG_DISB_HOLD_TH' SRC_TBL
,A1.DISB_DT
,A1.CPC
,A1.AWARD_YR
,COUNT(1) ROW_CNT
,SUM(A1.APPL_NBR) APPL_NBR_SUM
,SUM(A1.TSYS_SCHL_ID) TSYS_SCHL_ID_SUM
,SUM(TO_NUMBER(A1.DISB_NBR)) DISB_NBR_SUM
,SUM(A1.DISB_SEQ_NBR) DISB_SEQ_NBR_SUM
,SUM(CASE WHEN A1.DISB_STAT_CD = 'R' AND A1.DISB_SEQ_NBR = 1 THEN 1
ELSE A1.INTNL_APPL_SEQ_NBR + 1
END) INTERNAL_DISB_SEQ_SUM
,MIN(A1.DISB_RCVD_DT) DISB_RCVD_DT_MIN
,MAX(A1.DISB_RCVD_DT) DISB_RCVD_DT_MAX
,SUM(CASE WHEN A1.DISB_STAT_CD = 'P' THEN 1 ELSE 0 END) DISB_STAT_CD_P
,SUM(CASE WHEN A1.DISB_STAT_CD = 'R' THEN 1 ELSE 0 END) DISB_STAT_CD_R
,MIN(A1.PYMT_START_DT) PYMT_START_DT_MIN
,MAX(A1.PYMT_START_DT) PYMT_START_DT_MAX
,COUNT(DISTINCT A1.SCHL_ENROLL_CD) SCHL_ENROLL_CD_CNT
,SUM(TO_NUMBER(A1.NET_TRANS_CD)) NET_TRANS_CD_SUM
,SUM(A1.NET_TRANS_AMT) NET_TRANS_AMT_SUM
,SUM(CASE WHEN A1.DISB_STAT_CD = 'R' AND A1.DISB_SEQ_NBR = 1 AND A1.NET_C_OR_D_IND = 'D' THEN A1.NET_TRANS_AMT + A1.NET_TRANS_AMT
WHEN A1.DISB_STAT_CD = 'R' AND A1.DISB_SEQ_NBR = 1 AND A1.NET_C_OR_D_IND <> 'D' THEN A1.NET_TRANS_AMT - A1.NET_TRANS_AMT
WHEN (A1.DISB_STAT_CD <> 'R' OR A1.DISB_SEQ_NBR <> 1) AND A1.NET_C_OR_D_IND = 'D' THEN A1.SUM_NET_AMT + A1.NET_TRANS_AMT
WHEN (A1.DISB_STAT_CD <> 'R' OR A1.DISB_SEQ_NBR <> 1) AND A1.NET_C_OR_D_IND <> 'D' THEN A1.SUM_NET_AMT - A1.NET_TRANS_AMT
ELSE 0
END) SUM_NET_AMT_SUM
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'F' THEN 1 ELSE 0 END) ENRL_STATUS_CD_F
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'Q' THEN 1 ELSE 0 END) ENRL_STATUS_CD_Q
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'H' THEN 1 ELSE 0 END) ENRL_STATUS_CD_H
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'L' THEN 1 ELSE 0 END) ENRL_STATUS_CD_L
,SUM(CASE WHEN A1.ENRL_STATUS_CD IS NULL THEN 1 ELSE 0 END) ENRL_STATUS_CD_NULL
,COUNT(DISTINCT A1.PGM_CIP_CD) PGM_CIP_CD_CNT
,COUNT(DISTINCT A1.LEGACY_USER_ID) LEGACY_USER_ID_CNT
,COUNT(DISTINCT A1.MAINT_APP) MAINT_APP_CNT
,MIN(A1.MAINT_DTM) MAINT_DTM_MIN
,MAX(A1.MAINT_DTM) MAINT_DTM_MAX
,COUNT(DISTINCT A1.MAINT_USERID) MAINT_USERID_CNT
FROM
(
SELECT LM.TEACH_MASTER_ID
,SAM.APPL_NBR
,TO_NUMBER(SAM.AWARD_YR) AWARD_YR
,SAM.TSYS_SCHL_ID
,SAM.AWARD_NBR
,SAM.DISB_NBR
,SAM.DISB_SEQ_NBR
,SAM.CPC
,SAM.DISB_DT
,SAM.DISB_RCVD_DT
,SAM.DISB_STAT_CD
,SAM.PYMT_START_DT
,SAM.SCHL_ENROLL_CD
,SAM.NET_TRANS_CD
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.NET_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.NET_TRANS_AMT
END NET_TRANS_AMT
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.GROSS_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.GROSS_TRANS_AMT
END GROSS_TRANS_AMT
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.FEE_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.FEE_TRANS_AMT
END FEE_TRANS_AMT
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.REBATE_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.REBATE_TRANS_AMT
END REBATE_TRANS_AMT
,SAM.MAINT_DTM
,SAM.MAINT_USERID
,SAM.ENRL_STATUS_CD
,SAM.PGM_CIP_CD
,'BATCH' AS LEGACY_USER_ID
,'BATCH' AS MAINT_APP
,SAM.NET_C_OR_D_IND
,ROW_NUMBER() OVER (PARTITION BY SAM.APPL_NBR, SAM.AWARD_YR, SAM.TSYS_SCHL_ID, SAM.AWARD_NBR, SAM.DISB_NBR
ORDER BY DTSEQ.INTNL_APPL_SEQ_NBR DESC) ROW_NUM_PART
,COALESCE(DTSEQ.INTNL_APPL_SEQ_NBR,0) INTNL_APPL_SEQ_NBR
,DTSEQ.CHG_GROSS_TRANS_AMT DT_CHG_GROSS_TRANS_AMT
,DTSEQ.CHG_NET_TRANS_AMT DT_CHG_NET_TRANS_AMT
,DTSEQ.CHG_FEE_TRANS_AMT DT_CHG_FEE_TRANS_AMT
,DTSEQ.CHG_REBATE_TRANS_AMT DT_CHG_REBATE_TRANS_AMT
,COALESCE(DTSEQ.SUM_GROSS_AMT,0) SUM_GROSS_AMT
,COALESCE(DTSEQ.SUM_NET_AMT,0) SUM_NET_AMT
,COALESCE(DTSEQ.SUM_FEE_AMT,0) SUM_FEE_AMT
,COALESCE(DTSEQ.SUM_REBATE_AMT,0) SUM_REBATE_AMT
FROM MYSCHEMA.TEACH_MASTER LM
JOIN MYSCHEMA.STG_DISB_HOLD SAM
ON LM.APP_NUM_LEGACY = SAM.APPL_NBR
AND LM.ATTEND_SCHL_MASTER_ID = SAM.TSYS_SCHL_ID
AND LM.AWARD_YR = TO_NUMBER(SAM.AWARD_YR)
LEFT JOIN MYSCHEMA.STG_DISB_TRANS DTSEQ
ON SAM.APPL_NBR = DTSEQ.APPL_NBR
AND SAM.AWARD_YR = DTSEQ.AWARD_YR
AND SAM.TSYS_SCHL_ID = DTSEQ.TSYS_SCHL_ID
AND SAM.AWARD_NBR = DTSEQ.AWARD_NBR
AND SAM.DISB_NBR = DTSEQ.DISB_NBR
WHERE SAM.DISB_STAT_CD IN ('R','P')
AND SAM.NET_TRANS_CD IN ('0102','0131','0161','0189','0200')
) A1
WHERE A1.ROW_NUM_PART = 1
GROUP BY TO_CHAR(SYSDATE,'YYYY-MM-DD')
,'STG_DISB_HOLD_TH'
,A1.DISB_DT
,A1.CPC
,A1.AWARD_YR
;"/>
You have to escape them ALL:
WHEN A1.DISB_STAT_CD [..snip..] A1.NET_C_OR_D_IND <> 'D' THEN A1.NET_TRANS_AMT - A1.NET_TRANS_AMT
^^--missed a bunch of these
Plus, this next bit makes no sense:
<property name="SQL" value="<><><><><><>"
^---start attribute end attribute --^
"SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') SQL_RUN_DT
^---attribute value with no name for it

Concatenating multiple CASE statements into one alias

After some previous help on how to approach a problem I am having with some legacy code, it seems like the best approach for my issue is to concatenate case statements to return a value I can parse out in PHP.
I am trying to do something like this, but it is returning many rows, and eventually getting this error:
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
SELECT org.org_id,
org.org_name_1,
Datename(YEAR, member.enroll_date) AS enroll_year,
Max(CASE
WHEN board.member_from IS NULL THEN 0
ELSE 1
END) AS board_member,
CASE
WHEN ( org.delete_reason = 'OUT'
AND org.org_delete_flag = 'Y'
AND org.org_status_flag = 'C' ) THEN 'out_of_business|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues > 0 ) THEN 'insurance_member|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues = 0
AND member.status_flag IN( 'C', 'P' ) ) THEN 'insurance_product|'
ELSE ''
END + CASE
WHEN ( member.enroll_date IS NOT NULL
AND member.status_flag NOT IN( 'C', 'P' ) ) THEN 'member_since|'
ELSE ''
END + CASE
WHEN ( org.org_relationship_parent = 'Y'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 'subsidiary_member|'
ELSE ''
END + CASE
WHEN ( org.org_misc_data_9 = 'PAC' ) THEN 'pac|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'PART' ) THEN 'partner_member|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'FREE'
AND org.org_status_flag = 'P' ) THEN 'associate_member|'
ELSE ''
END
--ELSE 'non_member'
--END
AS org_status,
60 AS expires_in,
CASE
WHEN stat.dues_type = 'M' THEN
CASE
WHEN ( stat.termination_date IS NULL ) THEN ( stat.flat_dues )
ELSE 0
END
ELSE
CASE
WHEN ( member.payments = 0 ) THEN member.dues_billed_annual
ELSE member.payments
END
END AS dues_level,
CASE
WHEN ( org.affiliate_code = 'PCCE'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 1
ELSE 0
END AS pcce_membr,
-- '$'+CONVERT(VARCHAR,#dues) AS dues_level,
Ltrim(#product_level) AS product_level,
Ltrim(#involve_level) AS involvement_level
FROM organiz AS org
LEFT JOIN affilbil AS member
ON member.status_id = org.org_id
AND member.dues_category = 'MBR'
LEFT JOIN individu AS ind
ON ind.org_id = org.org_id
LEFT JOIN commembr AS board
ON board.status_id = ind.ind_id
AND board.committee_code = '5'
AND board.member_to IS NULL
LEFT JOIN statinsmorn AS stat
ON stat.status_id = org.org_id
AND stat.carrier = 'BS'
AND stat.planz = 'PCI'
WHERE org.org_id = #org_id
GROUP BY org.org_id,
org.org_name_1,
member.enroll_date,
org.delete_reason,
org.org_status_flag,
org.org_delete_flag,
stat.status_id,
stat.flat_dues,
stat.dues_type,
stat.termination_date,
org.org_misc_data_9,
org_relationship_parent,
org.dues_category,
member.status_flag,
member.dues_billed_annual,
member.payments,
stat.carrier,
org.Affiliate_Code
Well, this is embarrassing.
When I was making my changes to the stored procedure, I had inadvertently placed a call to the same procedure at the bottom. So I was recursively calling the same procedure over and over again. DOH.