How to do a case of 2 parameters in a sql query - sql

In a query I do :
SUM(CASE kbpres.soort WHEN 'K' THEN kbpres.prijs END)
AS FACKOSTEN
but I would like to use something like WHEN 'K' or WHEN 'V' THEN
but how do I do that ??

CASE WHEN kbpres.soort = 'K' OR kbpres.soort = 'V' THEN kbpres.prijs ELSE NULL END
Or
CASE WHEN kbpres.soort IN ('K', 'V') THEN kbpres.prijs ELSE NULL END
Or
CASE kbpres.soort WHEN 'K' THEN kbpres.prijs
WHEN 'V' THEN kbpres.prijs
ELSE NULL
END
Or
CASE WHEN kbpres.soort = 'K' THEN kbpres.prijs
WHEN kbpres.soort = 'V' THEN kbpres.prijs
ELSE NULL
END

SUM(CASE kbpres.soort WHEN 'K' THEN kbpres.prijs
WHEN 'V' THEN kbpres.prijs
END)
AS FACKOSTEN
OR
SUM(CASE WHEN kbpres.soort IN('K', 'V') THEN kbpres.prijs END)
AS FACKOSTEN

Try this:
SUM(CASE WHEN kbpres.soort IN ('K', 'V') THEN kbpres.prijs END)
AS FACKOSTEN

We don't know wich database engine you use, but in MySQL you can write this :
SUM(CASE WHEN kbpres.soort = 'K' OR kbpres.soort = 'V' THEN kbpres.prijs END)
AS FACKOSTEN

Related

Separate concatenated Case When statements

I want to know if there is a way to separate this statement and create a sentence like One, Two - or Two, Three. The separator must not work unless there is something following.
case when d1.code = 'X' then 'One' else '' end +
case when d2.Code = 'Y' then 'Two' else '' end +
case when d3.code = 'Z' then 'Three' else '' end as PlzHelp
If you're on a recent version of SQL Server, use CONCAT_WS:
SELECT CONCAT_WS(',', CASE WHEN d1.code = 'X' THEN 'One' END,
CASE WHEN d2.Code = 'Y' THEN 'Two' END,
CASE WHEN d3.code = 'Z' THEN 'Three' END) AS PlzHelp
...
If you are on an older version, you could go for something like this
SELECT RTRIM(SUBSTRING(CASE WHEN d1.code = 'X' THEN ', One' ELSE '' END
+CASE WHEN d2.Code = 'Y' THEN ', Two' ELSE '' END
+CASE WHEN d3.code = 'Z' THEN ', Three' ELSE '' END
+' ' ,3 ,17)) AS PlzHelp

How to get a Unique instance(column) based on 4 other columns from a table

I want to add a new column called Order_No from a table not currently in the query below : ordloc_discount
The four columns are
deal_id
item
location
last_calc_date
SELECT idh.vendor supplier
,idh.deal_id
,mff_report.mff_merch_sql.get_sup_name(idh.vendor) sup_name
,idh.deal_type
,scdhh.start_invoice_date
,TO_CHAR(scdhh.start_invoice_date,'MM/DD/YYYY') start_invoice_date_char
,idh.doc_date
,TO_CHAR(idh.doc_date,'MM/DD/YYYY') doc_date_char
,idh.ext_doc_id
,NULL deal_desc
,idh.custom_doc_ref_2 ext_ref_no
,idh.custom_doc_ref_3 deal_comp_type_desc
,idh.custom_doc_ref_4 comments
,idh.total_qty
,idh.total_cost
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN icdd.loc_type
WHEN 'P' THEN NULL
WHEN 'PL' THEN icdd.loc_type
ELSE icdd.loc_type
END loc_type
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN icdd.location
WHEN 'P' THEN NULL
WHEN 'PL' THEN icdd.location
ELSE icdd.location
END location
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN vl.loc_name
WHEN 'P' THEN NULL
WHEN 'PL' THEN vl.loc_name
ELSE vl.loc_name
END loc_name
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(im_parent.item,im.item)
WHEN 'PL' THEN NVL(im_parent.item,im.item)
ELSE im.item
END item
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
WHEN 'PL' THEN NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
ELSE NVL(mff_report.mff_merch_sql.get_brand_name(im.item),' ')
END brand_name
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(im_parent.item_desc,im.item_desc)
WHEN 'PL' THEN NVL(im_parent.item_desc,im.item_desc)
ELSE im.item_desc
END item_desc
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
WHEN 'VL' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
WHEN 'P' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
WHEN 'PL' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
ELSE mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
END vpn
,SUM(icdd.actual_turnover_units) turnover_units
,SUM(icdd.actual_turnover_revenue) turnover_revenue
,SUM(icdd.income_deal_curr) income
FROM im_doc_head idh
,mff_report.stage_complex_deal_head_hist scdhh
,im_complex_deal_detail icdd
,mff_report.v_loc vl
,item_master im
,item_master im_parent
WHERE ( ( idh.type IN ('DEBMEC','CRDMEC') --Debit and Credit Memos in APPROVED or POSTED
AND idh.status IN ('APPRVE','POSTED'))
OR ( idh.type = 'CRDNRC' --Credit Note Requests in APPROVED or MATCHED
AND idh.status IN ('APPRVE','MTCH')))
AND idh.deal_type = 'C'
AND NVL(:PM_supplier,idh.vendor) = idh.vendor
AND idh.doc_date BETWEEN NVL(:PM_doc_date_from,idh.doc_date) AND NVL(:PM_doc_date_to,idh.doc_date)
AND TRUNC(idh.approval_date) BETWEEN NVL(:PM_approval_date_from,TRUNC(idh.approval_date)) AND NVL(:PM_approval_date_to,TRUNC(idh.approval_date))
AND ( ( :PM_edi_812_filter = 'Y'
AND NOT EXISTS (SELECT 1
FROM sups s
,sup_traits_matrix stm
WHERE TO_NUMBER(idh.vendor) = s.supplier_parent
AND s.supplier = stm.supplier
AND stm.sup_trait = 525))
OR :PM_edi_812_filter = 'N'
OR :PM_supplier IS NOT NULL) -- ignore EDI 812 filter if a supplier is selected
AND ( idh.ext_doc_id MEMBER OF mff_report.parse_strings(:PM_ext_doc_id)
OR :PM_ext_doc_id IS NULL)
AND ( :PM_batch_mode = 'N'
OR ( :PM_batch_mode = 'Y' -- batch mode, pick up un-downloaded docs, and exclude matched CNRs which will not be downloaded
AND idh.edi_download_ind = 'N'
AND idh.status != 'MTCH'))
AND idh.deal_id = scdhh.deal_id (+)
AND SUBSTR(idh.ext_doc_id,(INSTR(idh.ext_doc_id,'-',1) + 1),INSTR(idh.ext_doc_id,'-',1,2) - (INSTR(idh.ext_doc_id,'-',1) + 1)) = scdhh.deal_detail_id (+)
AND idh.doc_date = scdhh.end_invoice_date (+)
AND idh.doc_id = icdd.doc_id
AND icdd.location = vl.loc
AND icdd.item = im.item
AND im.item_parent = im_parent.item (+)
AND ( :PM_supplier IS NOT NULL
OR :PM_doc_date_from IS NOT NULL
OR :PM_doc_date_to IS NOT NULL
OR :PM_approval_date_from IS NOT NULL
OR :PM_approval_date_to IS NOT NULL
OR :PM_ext_doc_id IS NOT NULL
OR :PM_batch_mode = 'Y')
GROUP BY idh.vendor
,idh.deal_id
,mff_report.mff_merch_sql.get_sup_name(idh.vendor)
,idh.deal_type
,scdhh.start_invoice_date
,TO_CHAR(scdhh.start_invoice_date,'MM/DD/YYYY')
,idh.doc_date`enter code here`
,TO_CHAR(idh.doc_date,'MM/DD/YYYY')
,idh.ext_doc_id
,idh.custom_doc_ref_2
,idh.custom_doc_ref_3
,idh.custom_doc_ref_4
,idh.total_qty
,idh.total_cost
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN icdd.loc_type
WHEN 'P' THEN NULL
WHEN 'PL' THEN icdd.loc_type
ELSE icdd.loc_type
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN icdd.location
WHEN 'P' THEN NULL
WHEN 'PL' THEN icdd.location
ELSE icdd.location
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN vl.loc_name
WHEN 'P' THEN NULL
WHEN 'PL' THEN vl.loc_name
ELSE vl.loc_name
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(im_parent.item,im.item)
WHEN 'PL' THEN NVL(im_parent.item,im.item)
ELSE im.item
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
WHEN 'PL' THEN NVL(mff_report.mff_merch_sql.get_brand_name(NVL(im_parent.item,im.item)),' ')
ELSE NVL(mff_report.mff_merch_sql.get_brand_name(im.item),' ')
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN NULL
WHEN 'VL' THEN NULL
WHEN 'P' THEN NVL(im_parent.item_desc,im.item_desc)
WHEN 'PL' THEN NVL(im_parent.item_desc,im.item_desc)
ELSE im.item_desc
END
,CASE mff_report.mff_fin_sql.get_dealadv_supplier_attr(TO_NUMBER(idh.vendor))
WHEN 'V' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
WHEN 'VL' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
WHEN 'P' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
WHEN 'PL' THEN mff_report.mff_merch_sql.get_vpn_for_item_vendor(NVL(im_parent.item,im.item),'S',TO_NUMBER(idh.vendor))
ELSE mff_report.mff_merch_sql.get_vpn_for_item_vendor(icdd.item,'S',TO_NUMBER(idh.vendor))
END
Get the new column order_no from a table not currently in the query

Error Message: Concat function requires 2 arguments

I have the following code which works fine when run as a Query but returns error message when run as view. I have attached the error message. I think I am missing a brace somewhere. If anyone can help it is much appreciated.
SELECT CASE
WHEN a.Sex = 1
THEN 'M'
WHEN a.Sex = 2
THEN 'F'
END AS Derived_Sex,
CASE
WHEN concat(b.Complete, a.[CALCULATED_AGE]) LIKE 'A%'
THEN 'Less than 1'
WHEN concat(b.Complete, a.[CALCULATED_AGE]) LIKE 'B%'
THEN 'Less than 1'
WHEN concat(b.Complete, a.[CALCULATED_AGE]) LIKE 'C%'
THEN 'Less than 1'
ELSE '1 year and above'
END AS Derived_InfantCat,
YEAR(a.DATE_OF_DEATH) AS [Derived_Year of Death],
FORMAT(a.DATE_OF_DEATH, 'MM') AS [Derived_Month of Death],
YEAR(a.DATE_OF_REGISTRATION) AS [Derived_Year of Registration],
FORMAT(a.DATE_OF_REGISTRATION, 'MM') AS [Derived_Month of Registration],
CONCAT(LEFT(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 4),
SUBSTRING(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 6, 2)) AS [Derived_YearMonth of Death],
CONCAT(LEFT(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 4),
SUBSTRING(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 6, 2)) AS [Derived_YearMonth of Registration],
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE
(CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END AS [Derived_Cause of Death Code],
c.[4 character DESCRIPTION],
c.[3 character Sub-Chapter Description],
d.[District_Name] AS [LA NAME],
e.[WD14NM] AS [WARD NAME],
(
CASE
WHEN a.[NHS_ESTABLISHMENT_IND] = '1'
THEN 'NHS'
WHEN a.[NHS_ESTABLISHMENT_IND] = '2'
THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(
CASE
WHEN a.[NHS_ESTABLISHMENT_IND] = '1'
THEN 'NHS'
WHEN a.[NHS_ESTABLISHMENT_IND] = '2'
THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(
CASE
WHEN a.[NHS_ESTABLISHMENT_IND] = '1'
THEN 'NHS'
WHEN a.[NHS_ESTABLISHMENT_IND] = '2'
THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN 'HOME'
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN 'ELSEWHERE'
ELSE 'OTHER'
END
) AS [PLACE OF DEATH CODE],
concat(a.[NHS_ESTABLISHMENT_IND],'-',a.PLACE_OF_DEATH_CODE) as code
FROM RAW.dbo.ONS_Death_DOD201516FYQ1 AS a
LEFT OUTER JOIN REFERENCE.dbo.[ONS-PCMD_AGE] AS b
ON a.CALCULATED_AGE_UNIT = b.[Age Unit]
LEFT OUTER JOIN REFERENCE.dbo.[OTH_ICD-10] AS c
ON
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT
(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE (
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END = c.[4 CharacterDiagnosis Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_LANames_1991 AS d
ON a.[ULA_OF_RESIDENCE_CODE] = d.[ONS_District_Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_WardNames_2014 AS e
ON CONCAT(a.[ULA_OF_RESIDENCE_CODE],a.[WARD_OF_RESIDENCE_CODE]) =e.WD14CDO
There is a chance of null value in the concatenated fields. I have applied ISNULL function to the columns from the outer joined table.
Just try with the below formatted code.
SELECT CASE
WHEN a.Sex = 1
THEN 'M'
WHEN a.Sex = 2
THEN 'F'
END AS Derived_Sex,
CASE
WHEN LTRIM(RTRIM(CONCAT(ISNULL(b.Complete,''), a.[CALCULATED_AGE]))) LIKE 'A%'
THEN 'Less than 1'
WHEN LTRIM(RTRIM(concat(ISNULL(b.Complete,''), a.[CALCULATED_AGE]))) LIKE 'B%'
THEN 'Less than 1'
WHEN LTRIM(RTRIM(concat(ISNULL(b.Complete,''), a.[CALCULATED_AGE]))) LIKE 'C%'
THEN 'Less than 1'
ELSE '1 year and above'
END AS Derived_InfantCat,
YEAR(a.DATE_OF_DEATH) AS [Derived_Year of Death],
FORMAT(a.DATE_OF_DEATH, 'MM') AS [Derived_Month of Death],
YEAR(a.DATE_OF_REGISTRATION) AS [Derived_Year of Registration],
FORMAT(a.DATE_OF_REGISTRATION, 'MM') AS [Derived_Month of Registration],
CONCAT(LEFT(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 4),
SUBSTRING(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 6, 2)) AS [Derived_YearMonth of Death],
CONCAT(LEFT(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 4),
SUBSTRING(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 6, 2)) AS [Derived_YearMonth of Registration],
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE
(CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END AS [Derived_Cause of Death Code],
c.[4 character DESCRIPTION],
c.[3 character Sub-Chapter Description],
d.[District_Name] AS [LA NAME],
e.[WD14NM] AS [WARD NAME],
(
CASE
WHEN a.[NHS_ESTABLISHMENT_IND] = '1'
THEN 'NHS'
WHEN a.[NHS_ESTABLISHMENT_IND] = '2'
THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(
CASE
WHEN a.[NHS_ESTABLISHMENT_IND] = '1'
THEN 'NHS'
WHEN a.[NHS_ESTABLISHMENT_IND] = '2'
THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(
CASE
WHEN a.[NHS_ESTABLISHMENT_IND] = '1'
THEN 'NHS'
WHEN a.[NHS_ESTABLISHMENT_IND] = '2'
THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN 'HOME'
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN 'ELSEWHERE'
ELSE 'OTHER'
END
) AS [PLACE OF DEATH CODE],
concat(a.[NHS_ESTABLISHMENT_IND],'-',a.PLACE_OF_DEATH_CODE) as code
FROM RAW.dbo.ONS_Death_DOD201516FYQ1 AS a
LEFT OUTER JOIN REFERENCE.dbo.[ONS-PCMD_AGE] AS b
ON a.CALCULATED_AGE_UNIT = b.[Age Unit]
LEFT OUTER JOIN REFERENCE.dbo.[OTH_ICD-10] AS c
ON
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT
(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE (
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END = c.[4 CharacterDiagnosis Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_LANames_1991 AS d
ON a.[ULA_OF_RESIDENCE_CODE] = d.[ONS_District_Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_WardNames_2014 AS e
ON CONCAT(ISNULL(a.[ULA_OF_RESIDENCE_CODE],''),ISNULL(a.[WARD_OF_RESIDENCE_CODE],'')) =e.WD14CDO
The "CONCAT" function must have at least two parameters.
However, in many places of your statement you don't pass two parameters/columns to CONCATENATE.
If you want to show Fullname from Firstname and LastName, you have to write:
CONCAT(Firstname, Lastname)
On MS SQL I got a similar "CONCAT needs 2 arguments" when I was trying to modify an existing view.
When I created a new view with the identical query, no problems there.
So some kind of cache issue can also be present.

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

Convert case statement to logical operator in the where clause

Can you please help me convert this piece of code to logical operator and use 'like' not '=' cause i know it's not efficient to put case statement on the where clause. Tia! Here's the code.
Where
(#reftype = '0' or
(#refnum = case #reftype when 'BN' then refBN when 'C' then refC when 'O' then refO else 'n/a' end)
)
Replace
or #refnum = case #reftype when 'BN' then refBN when 'C' then refC when 'O' then refO else 'n/a' end)
with
or (#reftype = 'BN' AND #refnum = refBN)
or (#reftype = 'C' AND #refnum = refC)
or (#reftype = 'O' AND #refnum = refO)
or #refnum = 'n/a'
You can translate it to boolean logic in this way:
WHERE #reftype = '0 'OR #refnum = 'n/a'
OR (#reftype = 'BN' AND #refnum = refbn)
OR (#reftype = 'C' AND #refnum = refc)
OR (#reftype = 'O' AND #refnum = refo)
But note that CASE in the WHERE-clause is not inefficient per se.