ORA-00905: missing keyword in where case statement - sql

select m_seqe
,m_emai
,m_phon
,e_seqe
,m_id
,
( case
when (:M_STAT = 0 ) and (m_id is not null ) then 'a'
when (:M_STAT = 0 ) and (m_id is null ) then 'b'
when (:M_STAT = 200 ) then 'c'
Else ' - ' end ) as Stat
from m_users
where m_stat = :M_STAT
and m_id =
case when (:M_ID = 0 ) then m_id is null
when (:M_ID ='a') then m_id is not null
else 'do nothing' end

The way you put it & I understood what you're saying, that would be a combination of several OR conditions:
WHERE m_stat = :M_STAT
AND ( ( m_id IS NULL AND :m_id = 0)
OR ( m_id IS NOT NULL AND :m_id = 'a')
OR ( m_id = 'do nothing'
);

You can't use a case expression to determine the logic in a filter; so this doesn't make sense and is what the parser is complaining about:
and m_id =
case when (:M_ID = 0 ) then m_id is null
when (:M_ID ='a') then m_id is not null
else 'do nothing' end
You could potentially have a case expression that evaluates to a value which you then compare against your column value, ut it doesn't seem to make sense here either, not least because null handling is a little awkward.
You seem to want to replace that with simple Boolean logic:
where m_stat = :M_STAT
and ((:M_ID = 0 and m_id is null)
or (:M_ID ='a' and m_id is not null))
or to be consistent in treating the bind variable as a string:
where m_stat = :M_STAT
and ((:M_ID = '0' and m_id is null)
or (:M_ID ='a' and m_id is not null))
There is no 'else' to worry about; your 'do nothing' string seems to be a placeholder that isn't intended to actually match against anything I.e. it's not a real m_id value you look for). If neither combined condition is true for a given row then the overall condition is false, and there won't be a match for that row and it will be filtered out.

Your problem is ambiguous but can be implemented as follows
select m_seqe
,m_emai
,m_phon
,e_seqe
,m_id
,
( case
when (:M_STAT = 0 ) and (m_id is not null ) then 'a'
when (:M_STAT = 0 ) and (m_id is null ) then 'b'
when (:M_STAT = 200 ) then 'c'
Else ' - ' end ) as Stat
from m_users
where m_stat = :M_STAT and
(
case when when (m_id IS NOT NULL AND :m_id = 'a') then 'a'
when (m_id IS NULL AND :M_ID = 0 ) then 'b'
when (m_id = 'do nothing') then 'c'
when 'd' end
) in ('a','b','c')

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

ERROR ORA-00905: missing keyword (microsoft ole db provider for oracle)

can anyone see what keyword is missing from the pl/sql query below?
I am getting the error ORA-00905: missing keyword (microsoft ole db provider for oracle)
not sure what keyword is missing - this is an odbc query to oracle database
SELECT "STAT"."ORD"."SAMPLEID",
MAX(CASE WHEN "STAT"."ORDANAEL"."MC"='QAERS' THEN N"STAT"."ORDANAEL"."RES_TXT" ELSE
'' END) AS ERSID,
"STAT"."ORDMCSTP_V"."SEQNB", "STAT"."ORD"."ORDPATNAME",
"STAT"."ORD"."ORDPATBIRTHDT", "STAT"."ORD"."CORORDNB", "STAT"."ORD"."PROJNB",
Max(CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN 'AMMOL'
WHEN "STAT"."ORDANAEL"."MC"='AMMOLR' THEN 'AMMOLR' ELSE '' END) AS TEST,
Max(CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN N"STAT"."ORDRESMCEL"."MC_RES"
WHEN "STAT"."ORDANAEL"."MC"='AMMOLR' THEN N"STAT"."ORDRESMCEL"."MC_RES" ELSE ''
END) AS MC_RES,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMIMI' THEN N"STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS IMI,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMIMP' THEN N"STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS IMP,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMNDM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS NDM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMOX48' THEN
N "STAT"."ORDMCSTPEL"."RES" ELSE '' END) AS OXA48,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSPM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SPM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMVIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS VIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMDIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS DIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMKPC' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS KPC,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMGES' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS GES,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSME' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SME,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMFRI1' THEN
N "STAT"."ORDMCSTPEL"."RES" ELSE '' END) AS FRI1,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMGIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS GIM,
Max( CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN N "STAT"."ORDANAEL"."VALMED" ELSE
'' END) AS VALMED_AMMOL,
Max( CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' AND
"STAT"."ORDANAEL_D"."DFIELD"='RR' THEN N "STAT"."ORDANAEL_D"."DVALUE" ELSE '' END)
AS RPT_DT_AMMOL,
"STAT"."ORDANAEL"."RES_ST"
FROM (("STAT"."ORDMCSTPEL" INNER JOIN ("STAT"."ORDMCSTP_V" INNER JOIN
("STAT"."ORD" INNER JOIN "STAT"."ORDANAEL" ON ("STAT"."ORD"."LID" =
"STAT"."ORDANAEL"."LID") AND ("STAT"."ORD"."LPERIOD" =
"STAT"."ORDANAEL"."LPERIOD") AND ("STAT"."ORD"."ORDNB" =
"STAT"."ORDANAEL"."ORDNB")) ON ("STAT"."ORDMCSTP_V"."LID" = "STAT"."ORD"."LID")
AND ("STAT"."ORDMCSTP_V"."LPERIOD" = "STAT"."ORD"."LPERIOD") AND
("STAT"."ORDMCSTP_V"."ORDNB" = "STAT"."ORD"."ORDNB")) ON ("STAT"."ORDMCSTPEL"."BC"
= "STAT"."ORDMCSTP_V"."BC") AND ("STAT"."ORDMCSTPEL"."CURRENT_STEPNB" =
"STAT"."ORDMCSTP_V"."CURRENT_STEPNB")) INNER JOIN "STAT"."ORDANAEL_D" ON
("STAT"."ORDANAEL"."NBINT" = "STAT"."ORDANAEL_D"."DNBINT") AND
("STAT"."ORDANAEL"."SPSEQ" = "STAT"."ORDANAEL_D"."DSPSEQ") AND
("STAT"."ORDANAEL"."ORDNB" = "STAT"."ORDANAEL_D"."DORDNB") AND
("STAT"."ORDANAEL"."LPERIOD" = "STAT"."ORDANAEL_D"."DLPERIOD") AND
("STAT"."ORDANAEL"."LID" = "STAT"."ORDANAEL_D"."DLID")) INNER JOIN
"STAT"."ORDRESMCEL" ON ("STAT"."ORDMCSTP_V"."SEQNB" = "STAT"."ORDRESMCEL"."SEQNB")
AND ("STAT"."ORDANAEL"."ORDNB" = "STAT"."ORDRESMCEL"."ORDNB") AND
("STAT"."ORDANAEL"."LPERIOD" = "STAT"."ORDRESMCEL"."LPERIOD") AND
("STAT"."ORDANAEL"."LID" = "STAT"."ORDRESMCEL"."LID")
WHERE (("STAT"."ORD"."LID")='H1')
AND (("STAT"."ORD"."LPERIOD")>'5200')
AND (("STAT"."ORD"."PROJNB")='ACPOERS')
GROUP BY "STAT"."ORD"."SAMPLEID", "STAT"."ORDMCSTP_V"."SEQNB",
"STAT"."ORD"."ORDPATNAME", "STAT"."ORD"."ORDPATBIRTHDT", "STAT"."ORD"."CORORDNB",
"STAT"."ORD"."PROJNB", "STAT"."ORDANAEL"."RES_ST"
HAVING ((("STAT"."ORDMCSTP_V"."SEQNB") Is Not Null) AND (("STAT"."ORD"."PROJNB")
='ACPOERS') AND (("STAT"."ORDANAEL"."RES_ST")>'4'))
Just too big for comment. Just cleaned up the double quotes and the N and it comes clean in a SQL formatter.
SELECT STAT.ORD.SAMPLEID,
MAX ( CASE WHEN STAT.ORDANAEL.MC = 'QAERS' THEN STAT.ORDANAEL.RES_TXT ELSE '' END ) AS ersid,
STAT.ORDMCSTP_V.SEQNB,
STAT.ORD.ORDPATNAME,
STAT.ORD.ORDPATBIRTHDT,
STAT.ORD.CORORDNB,
STAT.ORD.PROJNB,
MAX (
CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN 'AMMOL'
WHEN STAT.ORDANAEL.MC = 'AMMOLR' THEN 'AMMOLR'
ELSE ''
END )
AS test,
MAX (
CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN STAT.ORDRESMCEL.MC_RES
WHEN STAT.ORDANAEL.MC = 'AMMOLR' THEN STAT.ORDRESMCEL.MC_RES
ELSE ''
END )
AS mc_res,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMIMI' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS imi,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMIMP' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS imp,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMNDM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS ndm,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMOX48' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS oxa48,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSPM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS spm,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMVIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS vim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMDIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS dim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMKPC' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS kpc,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMGES' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS ges,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSME' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS sme,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMFRI1' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS fri1,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS sim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMGIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS gim,
MAX ( CASE WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN STAT.ORDANAEL.VALMED ELSE '' END )
AS valmed_ammol,
MAX ( CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL'
AND STAT.ORDANAEL_D.DFIELD = 'RR' THEN
STAT.ORDANAEL_D.DVALUE
ELSE
''
END )
AS rpt_dt_ammol,
STAT.ORDANAEL.RES_ST
FROM ((STAT.ORDMCSTPEL
INNER JOIN (STAT.ORDMCSTP_V
INNER JOIN (STAT.ORD
INNER JOIN STAT.ORDANAEL
ON ( STAT.ORD.LID = STAT.ORDANAEL.LID )
AND ( STAT.ORD.LPERIOD = STAT.ORDANAEL.LPERIOD )
AND ( STAT.ORD.ORDNB = STAT.ORDANAEL.ORDNB ))
ON ( STAT.ORDMCSTP_V.LID = STAT.ORD.LID )
AND ( STAT.ORDMCSTP_V.LPERIOD = STAT.ORD.LPERIOD )
AND ( STAT.ORDMCSTP_V.ORDNB = STAT.ORD.ORDNB ))
ON ( STAT.ORDMCSTPEL.BC = STAT.ORDMCSTP_V.BC )
AND ( STAT.ORDMCSTPEL.CURRENT_STEPNB = STAT.ORDMCSTP_V.CURRENT_STEPNB ))
INNER JOIN STAT.ORDANAEL_D
ON ( STAT.ORDANAEL.NBINT = STAT.ORDANAEL_D.DNBINT )
AND ( STAT.ORDANAEL.SPSEQ = STAT.ORDANAEL_D.DSPSEQ )
AND ( STAT.ORDANAEL.ORDNB = STAT.ORDANAEL_D.DORDNB )
AND ( STAT.ORDANAEL.LPERIOD = STAT.ORDANAEL_D.DLPERIOD )
AND ( STAT.ORDANAEL.LID = STAT.ORDANAEL_D.DLID ))
INNER JOIN STAT.ORDRESMCEL
ON ( STAT.ORDMCSTP_V.SEQNB = STAT.ORDRESMCEL.SEQNB )
AND ( STAT.ORDANAEL.ORDNB = STAT.ORDRESMCEL.ORDNB )
AND ( STAT.ORDANAEL.LPERIOD = STAT.ORDRESMCEL.LPERIOD )
AND ( STAT.ORDANAEL.LID = STAT.ORDRESMCEL.LID )
WHERE ( ( STAT.ORD.LID ) = 'H1' )
AND ( ( STAT.ORD.LPERIOD ) > '5200' )
AND ( ( STAT.ORD.PROJNB ) = 'ACPOERS' )
GROUP BY STAT.ORD.SAMPLEID,
STAT.ORDMCSTP_V.SEQNB,
STAT.ORD.ORDPATNAME,
STAT.ORD.ORDPATBIRTHDT,
STAT.ORD.CORORDNB,
STAT.ORD.PROJNB,
STAT.ORDANAEL.RES_ST
HAVING ( ( ( STAT.ORDMCSTP_V.SEQNB ) IS NOT NULL )
AND ( ( STAT.ORD.PROJNB ) = 'ACPOERS' )
AND ( ( STAT.ORDANAEL.RES_ST ) > '4' ) );
You can use N in front of a string literal to mark it as an nvarchar2. Example:
select N'abc' from dual
But you can't use it in front of an identifier the way you are doing it. If you want to read a certain column value as an nvarchar2, you'll need to cast it as such. Example:
... THEN cast("STAT"."ORDANAEL"."RES_TXT" as nvarchar2(50)) ...

How to use bitwise operator in existing sql query?

Here is my sql query. I have column name "ExpenseBucketCoverage" in claim table in which I am storing bitwise operators store multiple values in one column like below
MED_COPAY = 1, MED_DED= 10, MED_COINS = 100, RX_COPAY = 1, RX_DED= 10, RX_COINS = 100
I want to replace hard coded value like MED_COPAY, MED_COINS, MED_DED, RX_DED, RX_COINS & RX_COPAY in query by using ExpenseBucketCoverage column value. Can some one please tell me how can I do that?
Someone has suggested me below soultion
retrieve data from claim and left joining the first matched record in eligibility. And then add custom code to loop through the datarows to split the rows by covered expense bucket, and set the service category code in-memory column based on the ExpenseBucketCoverage value for the claim.
SELECT
e.categoryid,
c.servicetype,
'II' AS RepordType,
e.TPAId AS TPA_Id,
e.EmployerCode,
e.SubscriberId,
e.MemberID,
c.ServiceFrom,
c.ServiceTo,
CASE
WHEN e.categoryid IN( 'MED_DED', 'RX_DED' ) THEN
deductible
WHEN e.categoryid IN( 'MED_COINS', 'RX_COINS' ) THEN
isnull(coins,0)
WHEN e.categoryid IN( 'MED_COPAY', 'RX_COPAY' ) THEN
copay
ELSE 0
END AS ClaimAmount,
'' AS AccountTypeCode,
'1' ClaimsCrossoverAutoPay,
e.CategoryId,
CASE c.ServiceType
WHEN 'H' THEN
CASE e.PayeeIndicator
WHEN 'N' THEN '0'
WHEN 'Y' THEN '1'
END
WHEN 'P' THEN '0'
END AS PayProvider,
CASE c.ServiceType
WHEN 'H' THEN
CASE PayeeIndicator
WHEN 'N' THEN '0'
WHEN 'Y' THEN '1'
END
WHEN 'P' THEN '0'
END AS ReimbursementMethod,
CASE c.ServiceType
WHEN 'H' THEN c.Provider
WHEN 'P' THEN ''
END AS ProviderId,
'1' EnforceAccountEffectiveDates,
c.Id,
c.ClaimNumber + e.CategoryId as 'ExternalClaimNumber',
c.ProviderName,
c.CarrierId + ';' + c.SourceClaimNumber AS Notes
FROM Claim c
INNER JOIN Eligibility e ON e.TPAId = c.TPAId AND e.EIN = c.EIN AND
c.Processed = 'Y' AND e.FilterType = 'Eligibility'
AND c.TPAId='PrimePay'
AND (c.ServiceFrom >= e.BenefitEffectiveDate
AND c.ServiceFrom <=e.BenefitTermDate)
AND ( ( c.PayorID = c.PatientSSN
AND e.SubscriberSSN = c.PatientSSN
AND (c.EmployeeFirstName = c.PatientFirstName
AND c.EmployeeLastName = c.PatientLastName)
AND(e.MemberSSN = '' OR e.MemberSSN = NULL)
AND(e.MemberFirstName = '' OR e.MemberFirstName = NULL)
AND(e.MemberLastName = '' OR e.MemberLastName = NULL))
OR((c.PayorID != c.PatientSSN AND e.MemberSSN = c.PatientSSN
AND e.MemberFirstName = c.PatientFirstName
AND e.MemberLastName = c.PatientLastName)
OR(c.PayorID != c.PatientSSN AND e.MemberFirstName = c.PatientFirstName
AND e.MemberLastName= c.PatientLastName)))
AND (( c.Servicetype ='P'
AND e.CategoryID IN('RX_COINS','RX_COPAY', 'RX_DED' ))
OR ( c.Servicetype = 'H'
AND e.CategoryID IN( 'MED_COINS','MED_COPAY', 'MED_DED' )))

ERROR! - Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause

I'm getting this error: Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.
I've looked around but I can not find anything to help me in my case.
I am using SQL Server 2012
SELECT DISTINCT
CommodityCode.CommodityCode AS ComCode,
Facility.FacilityAcctNum,
Vessel.VesselType,
Facility.FacilityNm,
Case
When Commodities.ShippedReceivedId = 'R' And Commodities.ForeignDomesticId = 'F' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End AS RecF,
Case
When Commodities.ShippedReceivedId = 'R' And Commodities.ForeignDomesticId = 'D' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End AS RecD,
Case
When Commodities.ShippedReceivedId = 'S' And Commodities.ForeignDomesticId = 'D' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End AS Shipped,
Case
When Commodities.ShippedReceivedId = 'S' And Commodities.ForeignDomesticId = 'F' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End AS ShippedF,
Commodities.Destination,
Commodities.Origin,
Commodities.Specific,
CommodityCode.Descr,
Facility.FacilityID,
TransactionCharge.ArrivalDt,
TransactionCharge.DepartDt,
TransactionCharge.DtDocked,
TransactionCharge.DtSailed
FROM Vessel
INNER JOIN Commodities
INNER JOIN CommodityCode ON Commodities.CommodityCode = CommodityCode.CommodityCode
INNER JOIN TransactionCharge ON Commodities.TransactionID = TransactionCharge.TransactionID ON Vessel.VesselID = TransactionCharge.VesselId
INNER JOIN Facility ON TransactionCharge.FacilityID = Facility.FacilityID
INNER JOIN Unit ON Commodities.Unit = Unit.Unit
INNER JOIN TransactionFee ON TransactionCharge.TransactionID = TransactionFee.TransactionID
GROUP BY
CommodityCode.CommodityCode,
Facility.FacilityAcctNum,
Vessel.VesselType,
Facility.FacilityNm,
Commodities.Destination,
Commodities.Origin,
Commodities.Specific,
CommodityCode.Descr,
Facility.FacilityID,
Case
When Commodities.ShippedReceivedId = 'R' And Commodities.ForeignDomesticId = 'F' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End,
Case
When Commodities.ShippedReceivedId = 'R' And Commodities.ForeignDomesticId = 'D' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End,
Case
When Commodities.ShippedReceivedId = 'S' And Commodities.ForeignDomesticId = 'D' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End,
Case
When Commodities.ShippedReceivedId = 'S' And Commodities.ForeignDomesticId = 'F' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End,
TransactionCharge.ArrivalDt,
TransactionCharge.DepartDt,
TransactionCharge.DtDocked,
TransactionCharge.DtSailed
HAVING (Vessel.VesselType='V' Or Vessel.VesselType='O')
You have subqueries in your case statement in your GROUP BY. For example, looking at the first part of the case statement:
When Commodities.ShippedReceivedId = 'R' And Commodities.ForeignDomesticId = 'F' Then
Case When Commodities.Amount Is Null Or Commodities.Unit Is Null Then Null
Else
Round (Commodities.Amount/Case When Commodities.Unit = 'B' Then (Select BushelFactor From CommodityCode
Where CommodityCode.CommodityCode = Commodities.CommodityCode)
Else (Select ShortTonFactor From Unit
Where Unit.Unit = Commodities.Unit) End,2
)
End
Else 0
End,
There are two there to start with, beginning with Select BushelFactor... and Select ShortTonFactor..., so you will have to remove them.
I had a quick go at reformatting your query and removing/ commenting some of the issues with it:
SELECT --DISTINCT - do you really want DISTINCT?
cc.CommodityCode AS ComCode,
f.FacilityAcctNum,
v.VesselType,
f.FacilityNm,
CASE
WHEN Commodities.ShippedReceivedId = 'R' AND Commodities.ForeignDomesticId = 'F' THEN
CASE
WHEN Commodities.Amount IS NULL OR Commodities.Unit IS NULL THEN NULL
ELSE ROUND(Commodities.Amount /
CASE
WHEN Commodities.Unit = 'B' THEN cc.BushelFactor
ELSE u.ShortTonFactor
END, 2)
END
ELSE 0
END AS RecF,
CASE
WHEN Commodities.ShippedReceivedId = 'R' AND Commodities.ForeignDomesticId = 'D' THEN
CASE
WHEN Commodities.Amount IS NULL OR Commodities.Unit IS NULL THEN NULL
ELSE
ROUND(Commodities.Amount /
CASE
WHEN Commodities.Unit = 'B' THEN cc.BushelFactor
ELSE u.ShortTonFactor END, 2)
END
ELSE 0
END AS RecD,
CASE
WHEN Commodities.ShippedReceivedId = 'S' AND Commodities.ForeignDomesticId = 'D' THEN
CASE
WHEN Commodities.Amount IS NULL OR Commodities.Unit IS NULL THEN NULL
ELSE
ROUND(Commodities.Amount /
CASE
WHEN Commodities.Unit = 'B' THEN cc.BushelFactor
ELSE u.ShortTonFactor
END, 2)
END
ELSE 0
END AS Shipped,
CASE
WHEN Commodities.ShippedReceivedId = 'S' AND Commodities.ForeignDomesticId = 'F' THEN
CASE
WHEN Commodities.Amount IS NULL OR Commodities.Unit IS NULL THEN NULL
ELSE
ROUND(Commodities.Amount/
CASE
WHEN Commodities.Unit = 'B' THEN cc.BushelFactor
ELSE u.ShortTonFactor
END, 2)
END
ELSE 0
END AS ShippedF,
c.Destination,
c.Origin,
c.Specific,
cc.Descr,
f.FacilityID,
tc.ArrivalDt,
tc.DepartDt,
tc.DtDocked,
tc.DtSailed
FROM
Vessel v
INNER JOIN Commodities c ON --some_condtion, OR is this a cross join?
INNER JOIN CommodityCode cc ON c.CommodityCode = cc.CommodityCode
INNER JOIN TransactionCharge tc ON c.TransactionID = tc.TransactionID AND v.VesselID = tc.VesselId
INNER JOIN Facility f ON tc.FacilityID = f.FacilityID
INNER JOIN Unit u ON c.Unit = u.Unit
INNER JOIN TransactionFee td ON tc.TransactionID = tf.TransactionID
WHERE
v.VesselType IN ('V', 'O');
I don't expect this to produce the answers you want, but it might make it easier to spot what is wrong with your original query now that the logic is somewhat simplified?

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.