ORA-01843: not a valid month - Reporting - sql

I have a query that I am using in a PL-SQL view. The view contains a function called Jde_date that takes a date parameter in Julian format and converts it to Gregorian, it works perfectly.
The query is as follows:
select Cedula
, Nombre
, f_nacimiento
, fec_previa
, round(dia) Dia_faltante
, sexo
, Edad
, Fecha_Ingreso
, Nombre_Empresa
, NombreCargo
, Nom_Oficio
, UniddeNegocio
, DesUniNeg
, NombFte
, TipoContrato
, TipoSueldo
, SalHora
, Salario
, cesantias
, prima
, vacaciones
, Total_Prestaciones
, estado
, grupo_cotizante
, tipo_cotizante
, fondo_pensiones
from
(
SELECT distinct total.Cedula
, total.NOMBRE
, total.f_nacimiento
, total.fec_previa
, to_date(total.fec_previa) - sysdate as dia
, total.sexo
, total.Edad
, total.Fecha_Ingreso
, total.Nombre_Empresa
, total.NombreCargo
, total.Nom_Oficio
, total.UniddeNegocio
, Total.DesUniNeg
, total.NombFte
, total.TipoContrato
, total.TipoSueldo
, total.SalHora
, total.Salario
, total.cesantias
, total.prima
, total.vacaciones
, total.Total_Prestaciones
, total.estado
, total.grupo_cotizante
, total.tipo_cotizante
, total.fondo_pensiones
from
(
SELECT * FROM (
select
"F060116"."YAAN8" as AN8,
TRIM("F060116"."YAALPH") as NOMBRE,
TRIM("F060116"."YASSN") as Cedula,
TIPODOC.NNQ70BRTXT as tipo_documento,
"F0010"."CCNAME" as Nombre_Empresa,
"OFI"."DRDL01" as Nom_Oficio,
to_char(integral.JDE_DATE(YADSI), 'dd/mm/yyyy') as Fecha_Ingreso,
"F0005"."DRDL01" as estado,
TRIM(tsal.drdl01) as TipoSueldo,
"Tcon1".drdl01 as TipoContrato
,round(("F060116".yasal/100)/12) as Salario
,round(("F060116".yasal/100)/12/240) as SalHora
,round((("F060116".yasal/100)/12) * 0.0833) as CESANTIAS
,round((("F060116".yasal/100)/12) * 0.0833) as PRIMA
,round((("F060116".yasal/100)/12) * 0.0416) as VACACIONES
,round(((("F060116".yasal/100)/12) * 0.0833) + round((("F060116".yasal/100)/12) * 0.0833) + round((("F060116".yasal/100)/12) * 0.0416)) AS TOTAL_PRESTACIONES
,yasex as sexo
,to_char(integral.JDE_DATE(yadob), 'dd/mm/yyyy') as f_nacimiento
,case
when yasex = 'F' then to_char(add_months(add_months(to_char(integral.JDE_DATE(yadob), 'dd/mm/yyyy'),12*54),-4),'dd/mm/yyyy')
when yasex = 'M' THEN to_char(add_months(add_months(to_char(integral.JDE_DATE(yadob), 'dd/mm/yyyy'),12*59),-4),'dd/mm/yyyy')
END as fec_previa
,TRIM("Fpen"."DRDL01") as fondo_pensiones
,TRIM("gcoti"."DRDL01") as grupo_cotizante
,TRIM("tcoti"."DRDL01") as tipo_cotizante
,to_char(round((sysdate-integral.JDE_DATE(yadob))/365,2)) Edad
,"fte"."DRDL01" as NombFte
,to_char( integral.JDE_DATE(YADT) , 'dd/mm/yyyy') as FechaRetiro
,"F060116"."YAMCU" as UniddeNegocio
,"F0006"."MCDL01" as DesUniNeg
,"F5"."DRDL01" as NombreCargo
from "PRODDTA"."F060116" "F060116"
left join PRODCTL.F0005 TSAL on trim(TSAL.DRKY) = trim(YAP011) --tipo sueldo
and TSAL.DRSY = '06'
and TSAL.DRRT = '11'
left join "PRODCTL"."F0005" "fte" on rtrim(ltrim("fte"."DRKY")) = rtrim("F060116"."YAP005") --Frente trabajo
and "fte"."DRSY" = '06'
AND "fte"."DRRT" = '05'
left join "PRODDTA"."F0006" "F0006" on "F060116"."YAMCU"="F0006"."MCMCU"
left join "PRODCTL"."F0005" "F0005" on rtrim("F060116"."YAPAST")= ltrim("F0005"."DRKY") --Estado Contrato
and "F0005"."DRSY" = '06'
AND "F0005"."DRRT" = 'PS'
left join "PRODDTA"."F0010" "F0010" on "F060116"."YAHMCO" = "F0010"."CCCO"
left join "PRODCTL"."F0005" "F5" on rtrim("F060116"."YAJBCD") = ltrim("F5"."DRKY") --cargo
and "F5"."DRSY" = '06'
AND "F5"."DRRT" = 'G'
left join "PRODCTL"."F0005" "GPO" on rtrim(ltrim("GPO"."DRKY")) = rtrim("F060116"."YAP001") --Grupo de trabajo
and "GPO"."DRSY" = '06'
AND "GPO"."DRRT" = '01'
left join "PRODCTL"."F0005" "OFI" on rtrim(ltrim("OFI"."DRKY")) = rtrim("F060116"."YAP002") --Oficio
and "OFI"."DRSY" = '06'
AND "OFI"."DRRT" = '02'
left join "PRODCTL"."F0005" "fte" on rtrim(ltrim("fte"."DRKY")) = rtrim("F060116"."YAP005") --Ubicacion
and "fte"."DRSY" = '06'
AND "fte"."DRRT" = '05'
left join "PRODCTL"."F0005" "Tcon" on rtrim(ltrim("Tcon"."DRKY")) = rtrim("F060116"."YAP004") --Tipo jornada trabajo
and "Tcon"."DRSY" = '06'
AND "Tcon"."DRRT" = '04'
left join "PRODCTL"."F0005" "Tcon1" on rtrim(ltrim("Tcon1"."DRKY")) = rtrim("F060116"."YAP013") --Tipo de contrato
and "Tcon1"."DRSY" = '06'
AND "Tcon1"."DRRT" = '13'
left join "PRODCTL"."F0005" "Fpen" on rtrim(ltrim("Fpen"."DRKY")) = rtrim("F060116"."YAP017") --Fondo pensiones
and "Fpen"."DRSY" = '06'
AND "Fpen"."DRRT" = '17'
left join "PRODCTL"."F0005" "gcoti" on rtrim(ltrim("gcoti"."DRKY")) = rtrim("F060116"."YAP019") --Grupo cotizante
and "gcoti"."DRSY" = '06'
AND "gcoti"."DRRT" = '19'
left join "PRODCTL"."F0005" "tcoti" on rtrim(ltrim("tcoti"."DRKY")) = rtrim("F060116"."YAP020") --Tipo cotizante
and "tcoti"."DRSY" = '06'
AND "tcoti"."DRRT" = '21'
left join integral.REP_RENOVACION_CONTRATOS rct on trim(rct.CEDULA) = trim("F060116".YASSN)
left join PRODDTA.f01151 correoe on trim(correoe.eaan8) = trim(F060116.yaan8)
and trim(EAIDLN) = '0'
LEFT JOIN proddta.FQ70C014 TIPODOC ON trim(TIPODOC.NNAN8) = trim(F060116.yaan8)
AND NNPN = 0
) me
left join (
select ALADD1 DIRECCION, alan8 from proddta.F0116
where (ALUPMJ,alan8) in
(select max(ALUPMJ),alan8
from proddta.F0116
group by alan8)
group by ALADD1 , alan8 ) direcci
on trim(ME.an8) = trim(direcci.alan8)
left join (
select alan8,drdl01 departamento, ALCTY1 ciudad from proddta.F0116
LEFT JOIN PRODCTL.F0005
ON TRIM(DRKY) = trim(ALADDS)
and DRSY = '00'
AND DRRT = 'S'
where (ALEFTB,alan8) in
(select max(ALEFTB),alan8
from proddta.F0116
group by alan8)
group by alan8 , drdl01,ALCTY1
) ciu
on trim(ciu.alan8) = trim(ME.an8)
left join (
select tel.wpph1 telefono, tel.wpan8
from proddta.F0115 tel
where (tel.WPRCK7,tel.wpan8) in
(select max(tel.WPRCK7),tel.wpan8
from proddta.F0115 tel
where trim(tel.WPPHTP) = 'TEL'
group by tel.wpan8)
group by tel.wpph1 , tel.wpan8
) telt
on trim(ME.an8) = trim(telt.wpan8)
left join (
select celu.wpph1 celular, celu.wpan8
from proddta.F0115 celu
where (celu.WPRCK7,celu.wpan8) in
(select max(celu.WPRCK7),celu.wpan8
from proddta.F0115 celu
where trim(celu.WPPHTP) = 'MOV'
group by celu.wpan8)
group by celu.wpph1 , celu.wpan8
) celut
on trim(ME.an8) = trim(celut.wpan8) ) total
where
((total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='N/A' and total.sexo ='M'
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Depend. pens por vejez activo' and total.sexo ='M')
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Cotizante con requisitos cumpl' and total.sexo ='M' )
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Cotizante a quién reconoció In' and total.sexo ='M' )
and total.sexo = 'M') AND total.sexo = 'M' and total.edad >=57 and total.edad <=59)
OR
((total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='N/A' and total.sexo ='F'
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Depend. pens por vejez activo' and total.sexo ='F')
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Cotizante con requisitos cumpl' and total.sexo ='F' )
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Cotizante a quién reconoció In' and total.sexo ='F' )
and total.sexo = 'F') and total.sexo = 'F' and total.edad >=52 and total.edad <=54)
ORDER BY 7 desc)
In the sqldeveolper it works correctly, it returns the information without problems, in the preview of reporting services the same.
But when I publish the report I get the error ORA-01843: not a valid month.
How can I solve that?

to_date(total.fec_previa) - sysdate as dia
You have not specified the format model you are using in the conversion and are relying on the NLS_DATE_FORMAT session parameter to be correct; instead, you should be explicit about the format model you are using:
to_date(total.fec_previa, 'dd/mm/yyyy') - sysdate as dia

Related

Getting Error Ora-1489: Concatenated string is too long when I use LISTAGG

Here is the Sql. I am trying to use XMLAGG.
SELECT distinct LEARN_OBS_LCODE "KEY",
'Results' "TEMPLATE",
'en-US' "LOCALE",
'html' OUTPUT_FORMAT,
'TTX Checklist Activity Notification' "OUTPUT_NAME",
'EMAIL' DEL_CHANNEL,
LEARN_OBS_EMAIL "PARAMETER1",
-- "PARAMETER2",
'ejjc.fa.sender#workflow.mail.us6.oraclecloud.com' "PARAMETER3",
'(' || LEARN_OBS_LNAME || ') ' || 'Observation Checklist Pending'
|| ' - '
|| To_char(sysdate, 'MM/DD/YYYY') "PARAMETER4",
'true' "PARAMETER6"
FROM (select DISTINCT rtrim(LOCT.internal_location_code) LEARN_OBS_LCODE,
rtrim(LOC.location_name) LEARN_OBS_LNAME,
listagg((PEA.EMAIL_ADDRESS || ','|| LOBS.EMAIL1), ',') within group (order by LOCT.internal_location_code) LEARN_OBS_EMAIL
--XMLAGG(XMLAGG(XMLELEMENT(E,PEA.EMAIL_ADDRESS|| ','|| LOBS.EMAIL1 || ',')).extract('//text()') order by LOCT.internal_location_code).getClobaVal() as LEARN_OBS_EMAIL
FROM fusion.per_all_people_f PAPF
INNER JOIN fusion.per_person_names_f PER
ON PAPF.person_id = PER.person_id
AND Trunc(sysdate) BETWEEN PER.effective_start_date AND
PER.effective_end_date
AND PER.name_type = 'GLOBAL'
LEFT JOIN FUSION.PER_USERS PLU
ON PLU.PERSON_ID = PAPF.PERSON_ID
AND Trunc(sysdate) BETWEEN PLU.START_DATE AND NVL(PLU.END_DATE,Trunc(sysdate))
AND PLU.ACTIVE_FLAG = 'Y'
--AND PLU.USERNAME = FND_GLOBAL.USER_NAME
LEFT JOIN FUSION.PER_USER_ROLES PUR
ON PUR.USER_ID = PLU.USER_ID
AND Trunc(sysdate) BETWEEN PUR.START_DATE AND NVL(PUR.END_DATE, Trunc(sysdate))
LEFT JOIN FUSION.PER_ROLES_DN PR_BASE
ON PR_BASE.ROLE_ID = PUR.ROLE_ID
LEFT JOIN FUSION.PER_ROLES_DN_TL PLR
ON PLR.ROLE_ID = PR_BASE.ROLE_ID
AND PLR.LANGUAGE = USERENV('LANG')
AND PLR.ROLE_NAME IS NOT NULL
INNER JOIN fusion.per_all_assignments_f ASG
ON ASG.person_id = PAPF.person_id
AND Trunc(sysdate) BETWEEN ASG.effective_start_date AND
ASG.effective_end_date
AND ASG.primary_flag = 'Y'
AND ASG.assignment_type IN ( 'E', 'C','N' )
AND ASG.effective_latest_change = 'Y'
AND ASG.assignment_status_type <> 'INACTIVE'
LEFT JOIN per_location_details_f_vl LOC
ON ASG.location_id = LOC.location_id
AND Trunc(sysdate) BETWEEN LOC.effective_start_date AND
LOC.effective_end_date
LEFT JOIN per_locations LOCT
ON LOC.location_id = LOCT.location_id
LEFT JOIN fusion.per_email_addresses PEA
ON PEA.person_id = PAPF.person_id
AND Trunc(sysdate) BETWEEN PEA.date_from AND
Nvl(PEA.date_to, Trunc(
sysdate))
AND PEA.email_type = 'W1'
inner JOIN LEARN_OBSERVER LOBS
ON ASG.LOCATION_ID = LOBS.LOCATION_ID
WHERE Trunc(sysdate) BETWEEN PAPF.effective_start_date AND
PAPF.effective_end_date
--and papf.person_number = '38148'
AND UPPER(PR_BASE.ROLE_COMMON_NAME) = 'TTX_LEARNING_OBSERVER_BY_LOCATION_DATA'
--AND UPPER(PLR.ROLE_NAME) = 'TTX LEARNING OBSERVER BY LOCATION'
GROUP BY LOCT.internal_location_code , LOC.location_name

Why isn't this CTE recognized?

I'm trying to pull in student majors to this sql by using a CTE, but when I try to add the CTE fields, or join the CTE with an implicit join, which works fine in other queries, oracle throws the error 'invalid identifier'. Any thoughts?
This is only the first part of many unions in this sql but I've seen examples where a CTE works fine with unions so I don't think thats it, and besides that when I run this code without the unions I get the same errors, 'invalid identifier'.
with major (pidm, major) as
(
select
a.sgbstdn_pidm,
a.sgbstdn_majr_code_1
from
sgbstdn a
where a.sgbstdn_term_code_eff = (select
max(b.sgbstdn_term_code_eff)
from
sgbstdn b
where
a.sgbstdn_pidm = b.sgbstdn_pidm
and b.sgbstdn_term_code_eff <= '202004'
and b.sgbstdn_term_code_eff > '202001')
)
select --authorized aid
spriden_id id
,spriden_last_name ||', '||spriden_first_name || ' '|| spriden_mi "Name"
,major.major "Major"
,rpratrm_fund_code "Fund"
,rpratrm_period "Period"
,rpratrm_offer_amt "Offer"
,rpratrm_accept_amt "Accept"
,null "Loan Net Orig Amt"
,RPRATRM_AUTHORIZE_AMT "Authorized"
,rpratrm_paid_amt "Paid"
,c.hr "Census Hours"
,r.hr "Enrolled Hours"
,c.con "Consortium"
,b.pbcode "P Budget Code"
,b.pbcode_locked "P Budget Code Locked?"
,b.b_locked "Budget Locked"
--,astd.astd "Academic Standing"
,s.sap "SAP Code"
,s.term "SAP Term"
,decode(h.pidm, null, 'No', 'Yes') "Holds"
,admit.admit "Admitted?"
from
spriden
,rpratrm
--,(select SGVSTDN_pidm pidm, sgvstdn_astd_desc astd from SGVSTDN where SGVSTDN_term_code = '202003') astd
--admitted?
,(select
sgbstdn_pidm pidm
,case
when sgbstdn_levl_code like 'N%' then 'No'
when sgbstdn_levl_code is null then 'No Student Record found this term'
else 'Yes'
end admit
from
sgbstdn
where
sgbstdn_term_code_eff = '202003') admit
--HOLDS
,(select
rorhold_pidm pidm
from
rorhold
where
to_char(sysdate, 'YYYYMMDD') <= to_char(RORHOLD_TO_DATE, 'YYYYMMDD')
and to_char(sysdate, 'YYYYMMDD') >= to_char(RORHOLD_FROM_DATE, 'YYYYMMDD')
) h
--SAP
,(select
a.rorsapr_pidm pidm
,a.rorsapr_term_code term
,a.rorsapr_sapr_code sap
from
rorsapr a
where
a.rorsapr_term_code = (select max(b.rorsapr_term_code) from rorsapr b where a.rorsapr_pidm = b.rorsapr_pidm and b.rorsapr_term_code <= '202003')) s
--Period Budget Code Lock/FREEZE
,(select
rbrapbg_pidm pidm
,RBRAPBG_PBGP_CODE pbcode
,decode(RBRAPBG_PBGP_CODE_LOCK_IND, 'Y', 'Yes', 'No') pbcode_locked
,decode(RBRAPBG_BUDGET_FREEZE_IND, 'Y', 'Yes', 'No') b_locked
from
rbrapbg
where
RBRAPBG_RUN_NAME = 'ACTUAL'
and RBRAPBG_PERIOD = '202003'
and RBRAPBG_AIDY_CODE = '2021') b
,(select
rorenrl_pidm pidm
,rorenrl_term_code term
,RORENRL_FINAID_ADJ_HR hr
,RORENRL_CONSORTIUM_IND con
from
rorenrl
where
rorenrl_enrr_code = 'STANDARD'
and rorenrl_term_code like '202003') c
,(select
sfrstcr_pidm pidm
,sfrstcr_term_code term
,sum(sfrstcr_credit_hr) hr
from
sfrstcr
where
sfrstcr_term_code like '202003'
and sfrstcr_rsts_code in (select stvrsts_code from stvrsts where STVRSTS_INCL_SECT_ENRL = 'Y')
group by sfrstcr_pidm, sfrstcr_term_code) r
where
spriden_change_ind is null
and spriden_pidm = rpratrm_pidm
and rpratrm_aidy_code = '2021'
and RPRATRM_AUTHORIZE_AMT is not null
and rpratrm_pidm = c.pidm(+)
and rpratrm_period = c.term(+)
and rpratrm_pidm = r.pidm(+)
and rpratrm_period = r.term(+)
and rpratrm_period = '202003'
and rpratrm_pidm = b.pidm(+)
and rpratrm_pidm = s.pidm(+)
and rpratrm_pidm = h.pidm(+)
and rpratrm_pidm = admit.pidm(+)
and rpratrm_pidm = major.pidm
--and rpratrm_pidm = astd.pidm(+)
and not exists (select 'asdf' from SGVSTDN a where a.sgvstdn_pidm = rpratrm_period and a.SGVSTDN_term_code = (select max(b.sgvstdn_term_code) from sgvstdn b where b.sgvstdn_term_code <= '202003' and b.sgvstdn_astd_desc is not null and a.sgvstdn_pidm = b.sgvstdn_pidm) and a.sgvstdn_astd_code = 'SU')
and (r.hr is not null or c.hr is not null)
and not exists (select 'afd' from rorstat where rorstat_pidm = rpratrm_pidm and rorstat_aidy_code = rpratrm_aidy_code and RORSTAT_DISB_REQ_COMP_DATE is null)
and not exists (select 'afd' from rrrareq where rrrareq_pidm = rpratrm_pidm and rrrareq_aidy_code = rpratrm_aidy_code and rrrareq_fund_code = rpratrm_fund_code and RRRAREQ_SAT_IND = 'N')
--this will exclude those without loans should be a loan only query. if chur is auth but mpn not done, student won't show up
and exists (select 'asdf' from rlrdlor, rlrdldb where RLRDLOR_PIDM = rlrdldb_pidm and RLRDLOR_PIDM = rpratrm_pidm and RLRDLOR_LOAN_NO = rlrdldb_loan_no and RLRDLOR_AIDY_CODE = rlrdldb_aidy_code
and rlrdlor_aidy_code = rpratrm_aidy_code and RLRDLOR_FUND_CODE = rlrdldb_fund_code and rlrdlor_fund_code = rpratrm_fund_code and RLRDLOR_MPN_LINKED_IND = 'Y')
Adding the CTE to the from clause made it work. Forgot about that little detail.

How do I combine SQL Server coalesce, sum and subquery to get a single returned row

These two SQL selects work independently, but I need help combining them into a single select statement to produce one single row with all the fields from both queries.
Select #1:
select
'F' row_type,
sum(movement.override_pay_amt_n + tmp.b) as total_amount
from
movement,
(select coalesce(sum(driver_extra_pay.amount_n),0.0) b
from driver_extra_pay,deduct_code,movement
where driver_extra_pay.company_id = 'tms'
and driver_extra_pay.movement_id = 1
and driver_extra_pay.movement_id = movement.id
and driver_extra_pay.payee_id = movement.override_payee_id
and movement.company_id = 'tms'
and code_type = 'E'
and deduct_code.id = driver_extra_pay.deduct_code_id
and deduct_code.company_id = 'tms') tmp
where
movement.id = 1
and movement.company_id = 'tms'
Select #2:
select
'BBS' row_type,
movement.override_pay_amt,
override_pay_amt as total_amount,
movement.override_pay_amt_n,
movement.override_pay_amt_d,
movement.override_pay_amt_c,
movement.override_pay_amt_r
from
movement
where
movement.id = 1
and movement.company_id = 'tms' ;
My combined select:
select
'BBSALL' row_type,
movement.override_pay_amt,
override_pay_amt as total_amount,
movement.override_pay_amt_n,
movement.override_pay_amt_d,
movement.override_pay_amt_c,
movement.override_pay_amt_r,
sum(movement.override_pay_amt_n + tmp.b) as total_amount
from
movement,
(select coalesce(sum(driver_extra_pay.amount_n),0.0) b
from driver_extra_pay,deduct_code,movement
where driver_extra_pay.company_id = 'tms'
and driver_extra_pay.movement_id = 1
and driver_extra_pay.movement_id = movement.id
and driver_extra_pay.payee_id = movement.override_payee_id
and movement.id = 1
and movement.company_id = 'tms'
and code_type = 'E'
and deduct_code.id = driver_extra_pay.deduct_code_id
and deduct_code.company_id = 'tms') tmp
But I get this error....
Msg 8120, Level 16, State 1, Line 48
Column 'movement.override_pay_amt' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Just add the grouping, assuming you want the results summarised.
SELECT 'BBSALL' row_type
, movement.override_pay_amt
, override_pay_amt AS total_amount
, movement.override_pay_amt_n
, movement.override_pay_amt_d
, movement.override_pay_amt_c
, movement.override_pay_amt_r
, SUM(movement.override_pay_amt_n + tmp.b) AS total_amount
FROM movement
, (SELECT COALESCE(SUM(driver_extra_pay.amount_n), 0.0) b
FROM driver_extra_pay
, deduct_code
, movement
WHERE driver_extra_pay.company_id = 'tms'
AND driver_extra_pay.movement_id = 1
AND driver_extra_pay.movement_id = movement.id
AND driver_extra_pay.payee_id = movement.override_payee_id
AND movement.id = 1
AND movement.company_id = 'tms'
AND code_type = 'E'
AND deduct_code.id = driver_extra_pay.deduct_code_id
AND deduct_code.company_id = 'tms'
) tmp
GROUP BY row_type
, movement.override_pay_amt
, override_pay_amt
, movement.override_pay_amt_n
, movement.override_pay_amt_d
, movement.override_pay_amt_c
, movement.override_pay_amt_r

SQL Server 2008 Specified column was specified multiple times

The below code throws the following exception. How can I fix this?
Msg 8156, Level 16, State 1, Line 17
The column 'id' was specified multiple times for 'QISproduct'.
Query:
SELECT
g.artcode
, sum(g.aantal)
, i.class_01
, i.Isstockitem
FROM
gbkmut AS g
INNER JOIN
items AS i ON i.itemcode = g.artcode
INNER JOIN
(SELECT
QISP.id
, QISprocess.nml
, QISeventlog.id
, QISeventlog.dtsample
, QISproduct.nms
, QISbatchlog.nm
, QIStestlog.idvariable
, QIStestlog.no
, QISshortnote.ds
, gewicht = CASE QIStestlog.IDvariable
WHEN '139'
THEN QIStestlog.no
END
, aantal = CASE QIStestlog.IDvariable
WHEN '234'
THEN QIStestlog.no
END
, siloleeg = CASE QIStestlog.idvariable
WHEN '23'
THEN CASE QIStestlog.no
WHEN '10'
THEN 'Ja'
ELSE 'Nee'
END
END
, QISvariable.nml
, gl.nm
, QISprocess.id
FROM
QIC.Vobra_new2.dbo.production AS QISP
INNER JOIN
QIC.Vobra_new2.dbo.process AS QISprocess ON QISP.idprocess = QISProcess.id
INNER JOIN
QIC.Vobra_new2.dbo.product AS QISproduct ON QISP.idproduct = QISproduct.id
INNER JOIN
QIC.Vobra_new2.dbo.batchlog AS QISbatchlog ON QISP.idbatch = QISbatchlog.id
INNER JOIN
QIC.Vobra_new2.dbo.eventlog AS QISeventlog ON QISeventlog.idproduction = QISP.id
AND QISeventlog.idbatch = QISbatchlog.id
INNER JOIN
QIC.Vobra_new2.dbo.testlog AS QIStestlog ON QIStestlog.idevent = QISeventlog.id
LEFT OUTER JOIN
QIC.Vobra_new2.dbo.shortnote AS QISshortnote ON QISshortnote.id = QIStestlog.no
AND QIStestlog.idvariable = '144'
INNER JOIN
QIC.Vobra_new2.dbo.variable AS QISvariable ON QISvariable.id = QIStestlog.idvariable
LEFT OUTER JOIN
QIC.Vobra_new2.dbo.vvarxproc AS vvp ON vvp.idvariable = QISvariable.id
AND vvp.idprocess = QISP.idprocess
LEFT OUTER JOIN
QIC.Vobra_new2.dbo.attribute AS QISattribute ON QISattribute.id = vvp.idattribute
LEFT OUTER JOIN
QIC.Vobra_new2.dbo.grade AS QISgrade ON QISgrade.id = QISattribute.idgrade
LEFT OUTER JOIN
QIC.Vobra_new2.dbo.gradelevel AS gl ON gl.idgrade = QISattribute.idgrade
AND gl.nlevel = QIStestlog.no
WHERE
QISbatchlog.nm NOT LIKE 'V%'
AND QISP.dtstart > '2017-01-01'
AND QISP.dtstart < '2017-01-19'
AND QISP.idprocess IN ('12', '13', '14', '15', '16', '17', '18', '41')
AND QIStestlog.idvariable IN ('234', '139', '128')
) QISproduct ON g.artcode = QISproduct.nms
WHERE
g.bkjrcode > '2015'
AND g.reknr IN (3000, 3010, 3020)
AND g.aantal > 0
AND g.warehouse IN ('1', '9')
AND g.datum >= '2017-01-01'
AND g.oorsprong = 'R'
AND g.kstplcode <> 'VPR'
GROUP BY
g.artcode, i.Class_01, i.IsStockItem
The computed query aliased to QISProduct contains id column from two tables i.e. QISEventLog and QISProcess. So rename those columns to different names. Updated query
SELECT g.artcode
,sum(g.aantal)
,i.class_01
,i.Isstockitem
FROM gbkmut AS g
INNER JOIN items AS i ON i.itemcode = g.artcode
INNER JOIN (
SELECT QISP.id
,QISprocess.nml AS Processnml
,QISeventlog.id AS EventLogId
,QISeventlog.dtsample
,QISproduct.nms
,QISbatchlog.nm AS batchnm
,QIStestlog.idvariable
,QIStestlog.no
,QISshortnote.ds
,gewicht = CASE QIStestlog.IDvariable
WHEN '139'
THEN QIStestlog.no
END
,aantal = CASE QIStestlog.IDvariable
WHEN '234'
THEN QIStestlog.no
END
,siloleeg = CASE QIStestlog.idvariable
WHEN '23'
THEN CASE QIStestlog.no
WHEN '10'
THEN 'Ja'
ELSE 'Nee'
END
END
,QISvariable.nml variablenml
,gl.nm AS glnm
,QISprocess.id AS ProcessId
FROM QIC.Vobra_new2.dbo.production AS QISP
INNER JOIN QIC.Vobra_new2.dbo.process AS QISprocess ON QISP.idprocess = QISProcess.id
INNER JOIN QIC.Vobra_new2.dbo.product AS QISproduct ON QISP.idproduct = QISproduct.id
INNER JOIN QIC.Vobra_new2.dbo.batchlog AS QISbatchlog ON QISP.idbatch = QISbatchlog.id
INNER JOIN QIC.Vobra_new2.dbo.eventlog AS QISeventlog ON QISeventlog.idproduction = QISP.id
AND QISeventlog.idbatch = QISbatchlog.id
INNER JOIN QIC.Vobra_new2.dbo.testlog AS QIStestlog ON QIStestlog.idevent = QISeventlog.id
LEFT JOIN QIC.Vobra_new2.dbo.shortnote AS QISshortnote ON QISshortnote.id = QIStestlog.no
AND QIStestlog.idvariable = '144'
INNER JOIN QIC.Vobra_new2.dbo.variable AS QISvariable ON QISvariable.id = QIStestlog.idvariable
LEFT JOIN QIC.Vobra_new2.dbo.vvarxproc AS vvp ON vvp.idvariable = QISvariable.id
AND vvp.idprocess = QISP.idprocess
LEFT JOIN QIC.Vobra_new2.dbo.attribute AS QISattribute ON QISattribute.id = vvp.idattribute
LEFT JOIN QIC.Vobra_new2.dbo.grade AS QISgrade ON QISgrade.id = QISattribute.idgrade
LEFT JOIN QIC.Vobra_new2.dbo.gradelevel AS gl ON gl.idgrade = QISattribute.idgrade
AND gl.nlevel = QIStestlog.no
WHERE QISbatchlog.nm NOT LIKE 'V%'
AND QISP.dtstart > '2017-01-01'
AND QISP.dtstart < '2017-01-19'
AND QISP.idprocess IN (
'12'
,'13'
,'14'
,'15'
,'16'
,'17'
,'18'
,'41'
)
AND QIStestlog.idvariable IN (
'234'
,'139'
,'128'
)
) QISproduct ON g.artcode = QISproduct.nms
WHERE g.bkjrcode > '2015'
AND g.reknr IN (
3000
,3010
,3020
)
AND g.aantal > 0
AND g.warehouse IN (
'1'
,'9'
)
AND g.datum >= '2017-01-01'
AND g.oorsprong = 'R'
AND g.kstplcode <> 'VPR'
GROUP BY g.artcode
,i.Class_01
,i.IsStockItem
, QISbatchlog.nm
And
, gl.nm
Have same column name
You can add as to change colum name
, gl.nm as col1
You have multiple issues in your query. You are populating following columns with same column name in your inner query. Use Unique name using AS alias in inner query.
QISP.id
QISeventlog.id
QISprocess.id
gl.nm
QISbatchlog.nm
QISprocess.nml
QISvariable.nml
It is mandatory to have unique column name return by select list in sql.

SQL Server 2008 r2 CASE Expression not evaluating correctly

I've got the following SQL statement that is not determining when a field has a value of 'Y' when it should. Here is the SQL...
SELECT A.BUSINESS_UNIT
, A.WO_ID
, A.WO_TASK_ID
, A.ENS_TSK_CRT_VAL
, (CASE WHEN A.ENS_TSK_CRT_V01 = 'Y' THEN B.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V01
, (CASE WHEN A.ENS_TSK_CRT_V02 = 'Y' THEN C.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V02
, (CASE WHEN A.ENS_TSK_CRT_V03 = 'Y' THEN D.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V03
, (CASE WHEN A.ENS_TSK_CRT_V04 = 'Y' THEN E.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V04
, (CASE WHEN A.ENS_TSK_CRT_V05 = 'Y' THEN F.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V05
, (CASE WHEN A.ENS_TSK_CRT_V06 = 'Y' THEN G.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V06
, (CASE WHEN A.ENS_TSK_CRT_V07 = 'Y' THEN H.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V07
, (CASE WHEN A.ENS_TSK_CRT_V08 = 'Y' THEN I.DESCR ELSE ' ' END) AS ENS_TSK_CRT_V08
FROM PS_ENS_WM_TSKCR_VW A LEFT OUTER JOIN (
SELECT A.BUSINESS_UNIT
, A.WO_TASK_ID
, C.DESCR
, A.WO_ID
, C.ENS_TSK_CRT_CD
FROM PS_ENS_WM_TSKCR_VW A
, PS_ENS_WM_TSK_CRT C
WHERE A.ENS_TSK_CRT_V01 = 'Y'
AND C.SETID = 'SHARE'
AND C.ENS_TSK_CRT_CD = '01' ) B ON B.BUSINESS_UNIT = A.BUSINESS_UNIT
AND B.WO_ID = A.WO_ID
AND B.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN (
SELECT A.BUSINESS_UNIT
, A.WO_TASK_ID
, C.DESCR
, A.WO_ID
, C.ENS_TSK_CRT_CD
FROM PS_ENS_WM_TSKCR_VW A
, PS_ENS_WM_TSK_CRT C
WHERE A.ENS_TSK_CRT_V01 = 'Y'
AND C.SETID = 'SHARE'
AND C.ENS_TSK_CRT_CD = '02' ) C ON C.BUSINESS_UNIT = A.BUSINESS_UNIT
AND C.WO_ID = A.WO_ID
AND C.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN (
SELECT A.BUSINESS_UNIT
, A.WO_TASK_ID
, C.DESCR
, A.WO_ID
, C.ENS_TSK_CRT_CD
FROM PS_ENS_WM_TSKCR_VW A
, PS_ENS_WM_TSK_CRT C
WHERE A.ENS_TSK_CRT_V01 = 'Y'
AND C.SETID = 'SHARE'
AND C.ENS_TSK_CRT_CD = '03' ) D ON D.BUSINESS_UNIT = A.BUSINESS_UNIT
AND D.WO_ID = A.WO_ID
AND D.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN (
SELECT A.BUSINESS_UNIT
, A.WO_TASK_ID
, C.DESCR
, A.WO_ID
, C.ENS_TSK_CRT_CD
FROM PS_ENS_WM_TSKCR_VW A
, PS_ENS_WM_TSK_CRT C
WHERE A.ENS_TSK_CRT_V01 = 'Y'
AND C.SETID = 'SHARE'
AND C.ENS_TSK_CRT_CD = '04' ) E ON E.BUSINESS_UNIT = A.BUSINESS_UNIT
AND E.WO_ID = A.WO_ID
AND E.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN (
SELECT A.BUSINESS_UNIT
, A.WO_TASK_ID
, C.DESCR
, A.WO_ID
, C.ENS_TSK_CRT_CD
FROM PS_ENS_WM_TSKCR_VW A
, PS_ENS_WM_TSK_CRT C
WHERE A.ENS_TSK_CRT_V01 = 'Y'
AND C.SETID = 'SHARE'
AND C.ENS_TSK_CRT_CD = '05' ) F ON F.BUSINESS_UNIT = A.BUSINESS_UNIT
AND F.WO_ID = A.WO_ID
AND F.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN (
SELECT A.BUSINESS_UNIT
, A.WO_TASK_ID
, C.DESCR
, A.WO_ID
, C.ENS_TSK_CRT_CD
FROM PS_ENS_WM_TSKCR_VW A
, PS_ENS_WM_TSK_CRT C
WHERE A.ENS_TSK_CRT_V01 = 'Y'
AND C.SETID = 'SHARE'
AND C.ENS_TSK_CRT_CD = '06' ) G ON G.BUSINESS_UNIT = A.BUSINESS_UNIT
AND G.WO_ID = A.WO_ID
AND G.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN (
SELECT A.BUSINESS_UNIT
, A.WO_TASK_ID
, C.DESCR
, A.WO_ID
, C.ENS_TSK_CRT_CD
FROM PS_ENS_WM_TSKCR_VW A
, PS_ENS_WM_TSK_CRT C
WHERE A.ENS_TSK_CRT_V01 = 'Y'
AND C.SETID = 'SHARE'
AND C.ENS_TSK_CRT_CD = '07' ) H ON H.BUSINESS_UNIT = A.BUSINESS_UNIT
AND H.WO_ID = A.WO_ID
AND H.WO_TASK_ID = A.WO_TASK_ID LEFT OUTER JOIN (
SELECT A.BUSINESS_UNIT
, A.WO_TASK_ID
, C.DESCR
, A.WO_ID
, C.ENS_TSK_CRT_CD
FROM PS_ENS_WM_TSKCR_VW A
, PS_ENS_WM_TSK_CRT C
WHERE A.ENS_TSK_CRT_V01 = 'Y'
AND C.SETID = 'SHARE'
AND C.ENS_TSK_CRT_CD = '08' ) I ON I.BUSINESS_UNIT = A.BUSINESS_UNIT
AND I.WO_ID = A.WO_ID
AND I.WO_TASK_ID = A.WO_TASK_ID
WHERE ( A.ENS_TSK_CRT_VAL <> 0
AND A.WO_ID = '0000002151'
AND A.BUSINESS_UNIT = 'R3851' )
The case statements sometimes pick up the 'Y' and behave as expected, sometimes they don't. The fields being examined for 'Y' values are CHAR fields of one character in length. I've tried casting and converting to make certain that the value was what I was expecting in the case statement when compared to 'Y'. I've tried concatenating a '>' and '<' to either side to see if somehow there were whitespaces padding the value, and that was not the case at all. It just appears as though the case statement simply ceased working. Here is a small sample of what the data looked like, to give you an idea of what it was working with...sorry it's a little bit scrambled.
WO_TASK_ID WO_ID ENS_TSK_CRT_VAL ENS_TSK_CRT_V01 ENS_TSK_CRT_V02 ENS_TSK_CRT_V03
1 0000002151 2 Y N N
9 0000002151 12 N Y Y
12 0000002151 52 N Y N
I've since fixed the problem by taking a completely different route, but I just want to know how this is happening, and what to do to overcome the problem in the future when working with the CASE Expression again.
Any help is greatly appreciated...
Flynn
Possible this be helpful for you -
;WITH cte AS
(
SELECT A.BUSINESS_UNIT ,
A.WO_TASK_ID ,
C.DESCR ,
A.WO_ID ,
C.ENS_TSK_CRT_CD
FROM PS_ENS_WM_TSKCR_VW A ,
PS_ENS_WM_TSK_CRT C
WHERE A.ENS_TSK_CRT_V01 = 'Y'
AND C.SETID = 'SHARE'
AND C.ENS_TSK_CRT_CD IN (
'01', '02', '03', '04',
'05', '06', '07', '08'
)
)
SELECT A.BUSINESS_UNIT ,
A.WO_ID ,
A.WO_TASK_ID ,
A.ENS_TSK_CRT_VAL ,
ISNULL(B.DESCR, ' ') AS ENS_TSK_CRT_V01 ,
ISNULL(C.DESCR, ' ') AS ENS_TSK_CRT_V02 ,
ISNULL(D.DESCR, ' ') AS ENS_TSK_CRT_V03 ,
ISNULL(E.DESCR, ' ') AS ENS_TSK_CRT_V04 ,
ISNULL(F.DESCR, ' ') AS ENS_TSK_CRT_V05 ,
ISNULL(G.DESCR, ' ') AS ENS_TSK_CRT_V06 ,
ISNULL(H.DESCR, ' ') AS ENS_TSK_CRT_V07 ,
ISNULL(I.DESCR, ' ') AS ENS_TSK_CRT_V08
FROM PS_ENS_WM_TSKCR_VW A
LEFT JOIN cte B ON B.BUSINESS_UNIT = A.BUSINESS_UNIT
AND B.WO_ID = A.WO_ID
AND B.WO_TASK_ID = A.WO_TASK_ID
AND B.ENS_TSK_CRT_CD = '01'
AND A.ENS_TSK_CRT_V01 = 'Y'
LEFT JOIN cte C ON C.BUSINESS_UNIT = A.BUSINESS_UNIT
AND C.WO_ID = A.WO_ID
AND C.WO_TASK_ID = A.WO_TASK_ID
AND C.ENS_TSK_CRT_CD = '02'
AND A.ENS_TSK_CRT_V02 = 'Y'
LEFT JOIN cte D ON D.BUSINESS_UNIT = A.BUSINESS_UNIT
AND D.WO_ID = A.WO_ID
AND D.WO_TASK_ID = A.WO_TASK_ID
AND D.ENS_TSK_CRT_CD = '03'
AND A.ENS_TSK_CRT_V03 = 'Y'
LEFT JOIN cte E ON E.BUSINESS_UNIT = A.BUSINESS_UNIT
AND E.WO_ID = A.WO_ID
AND E.WO_TASK_ID = A.WO_TASK_ID
AND E.ENS_TSK_CRT_CD = '04'
AND A.ENS_TSK_CRT_V04 = 'Y'
LEFT JOIN cte F ON F.BUSINESS_UNIT = A.BUSINESS_UNIT
AND F.WO_ID = A.WO_ID
AND F.WO_TASK_ID = A.WO_TASK_ID
AND F.ENS_TSK_CRT_CD = '05'
AND A.ENS_TSK_CRT_V05 = 'Y'
LEFT JOIN cte G ON G.BUSINESS_UNIT = A.BUSINESS_UNIT
AND G.WO_ID = A.WO_ID
AND G.WO_TASK_ID = A.WO_TASK_ID
AND G.ENS_TSK_CRT_CD = '06'
AND A.ENS_TSK_CRT_V06 = 'Y'
LEFT JOIN cte H ON H.BUSINESS_UNIT = A.BUSINESS_UNIT
AND H.WO_ID = A.WO_ID
AND H.WO_TASK_ID = A.WO_TASK_ID
AND H.ENS_TSK_CRT_CD = '07'
AND A.ENS_TSK_CRT_V07 = 'Y'
LEFT JOIN cte I ON I.BUSINESS_UNIT = A.BUSINESS_UNIT
AND I.WO_ID = A.WO_ID
AND I.WO_TASK_ID = A.WO_TASK_ID
AND I.ENS_TSK_CRT_CD = '08'
AND A.ENS_TSK_CRT_V08 = 'Y'
WHERE A.ENS_TSK_CRT_VAL != 0
AND A.WO_ID = '0000002151'
AND A.BUSINESS_UNIT = 'R3851'
BTW, just in case anyone is curious, here was the workaround I used before posting my question about the CASE Expression to the original problem......
SELECT A.BUSINESS_UNIT , A.WO_ID , A.WO_TASK_ID , A.ENS_TSK_CRT_VAL
, (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,1) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 01 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL01
, (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,2) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 02 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL02
, (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,3) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 03 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL03
, (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,4) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 04 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL04
, (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,5) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 05 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL05
, (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,6) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 06 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL06
, (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,7) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 07 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL07
, (CASE WHEN (CASE WHEN CAST(A.ENS_TSK_CRT_VAL AS INT) & POWER(2,8) > 0 THEN 1 ELSE 0 END) = 1 THEN (SELECT DESCR FROM PS_ENS_WM_TSK_CRT WHERE ENS_TSK_CRT_CD = 08 AND SETID = 'SHARE') ELSE ' ' END) AS ENS_TSK_CRT_VAL08
FROM PS_ENS_WM_TSKCR_WO A
WHERE A.ENS_TSK_CRT_VAL <> 0
AND A.WO_ID = '0000002151' AND A.BUSINESS_UNIT = 'R3851'
I suspect it is the behavior of the case statement. I just discovered a difference in 2008 R2 vs 2005 and in my searches came upon your problem. Case is handled by sql server 2008 R2 differently than 2005. Note the statement in the remarks of the 2008 doc: "In some situations, an expression is evaluated before a CASE statement receives the results of the expression as its input. Errors in evaluating these expressions are possible."
So in 2008 all potential outputs may be evaluated even if not appropriate (the input doesn't evaluate to true), which is occuring in some stored procs of mine now after upgrading to 2008.
So perhaps in your case, regardless of whether B.DESCR, C.DESCR, D.DESCR, etc. equal 'Y',
B.DESCR, C.DESCR, D.DESCR, etc. get evaluated and may at that time be null or otherwise somehow unable to be compared to 'Y' thereby causing that part of the statement to fail (and this is silent it seems). Not sure but it smells the same as what I had for a problem. The only thing that makes me cautious about this "answer" is that my problem was within the where clause and yours is in the select part of the sql statement.
Regardless, maybe someone will find this helpful. It took some digging to figure this out.
you can try to remove trailing space from Y which can cause similar issues