I want to use case statement for a column like below
SELECT sr_no, TYPE, stage, party_name, amount, remarks, exp_type, exp_id,
voucher_no, cheque_no, cheque_dt, chq_favr_name, attachment,
CASE NVL (amount, 0) = 0 THEN checkVal = 0
ELSE
checkVal = 1
FROM xxcus.xxacl_pn_expense_info
WHERE mkey = '354'
AND ((NVL (amount, 0) <> 0) OR (party_name IS NOT NULL))
ORDER BY sr_no
But I am getting error as
ORA-00923: FROM keyword not found where expected
SELECT sr_no, TYPE, stage, party_name, amount, remarks, exp_type, exp_id,
voucher_no, cheque_no, cheque_dt, chq_favr_name, attachment,
CASE WHEN NVL(amount, 0) = 0 THEN 0 ELSE 1 END AS checkVal
FROM xxcus.xxacl_pn_expense_info
WHERE mkey = '354'
AND ((NVL (amount, 0) <> 0) OR (party_name IS NOT NULL))
ORDER BY sr_no
you're missing an END
SELECT sr_no, TYPE, stage, party_name, amount, remarks, exp_type, exp_id,
voucher_no, cheque_no, cheque_dt, chq_favr_name, attachment,
CASE WHEN NVL (amount, 0) = 0 THEN checkVal = 0
ELSE
checkVal = 1
END
FROM xxcus.xxacl_pn_expense_info
WHERE mkey = '354'
AND ((NVL (amount, 0) <> 0) OR (party_name IS NOT NULL))
ORDER BY sr_no
Related
I'm trying to create an SQL that added two more conditions when the specific column(value) is matched.
Example:
SELECT
COLUMN_NAME
FROM
TABLE
WHERE
NAME_COLUMN = 'NAME'
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOLD_COLUMN
END = 1
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOLD_COLUMN_1
END = 1
It is working when AGE = 18 it will return value that ADULTHOOLD_COLUMN = 1 and ADULTHOOLD_COLUMN_1 = 1
but when age is not equal to 18 it will not return any rows.
I was expecting that all of the data(with 1 or 0) will return.
ADULTHOOLD_COLUMN(Value in db is 1 or 0)
ADULTHOOLD_COLUMN_1(Value in db is 1 or 0)
Sounds a bit strange, but ok, looks like you need else:
SELECT
COLUMN_NAME
FROM
TABLE
WHERE
NAME_COLUMN = 'NAME'
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOD_COLUMN
ELSE 1
END = 1
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOD_COLUMN_1
ELSE 1
END = 1
Or maybe just to make it more readable:
SELECT
COLUMN_NAME
FROM
TABLE
WHERE
NAME_COLUMN = 'NAME'
AND (AGE is null OR AGE!='18' OR AGE = '18' AND ADULTHOOD_COLUMN = 1)
AND (AGE is null OR AGE!='18' OR AGE = '18' AND ADULTHOOD_COLUMN_1 = 1)
PS. Are you sure that AGE is not number? If that's a number datatype, you need to replace '18' to 18
IF inputVariable = '0'
THEN
DELETE existingTemptable
WHERE status != 999;
ELSE
IF inputVariable = '1'
THEN
DELETE existingTemptable
WHERE status = 999;
ELSE
DELETE existingTemptable
WHERE status != 999
AND date < utils.dateadd('MONTH', -6, SYSTIMESTAMP);
END IF;
END IF;
This If logic is present on a temp table and I have to remove the temp table and make it only select query , so approached with WITH CTE but stuck in the below
what should be in the where clause
with existingTemptable as
(
//got the temp table here
), myTable as
(
Select * from existingTemptable
**where
status = CASE WHEN inputVariable = '0' THEN 999
WHEN inputVariable != '0' AND inputVariable != '1' THEN 999
ELSE status
END**
)Select * from myTable
What to put in WHERE clause so that it mimics the If logic above
You have to be careful if inputVariable or status (or both) may be null. The rest is a direct application of logical rules one learns (or should learn) in middle school.
select *
from existingTemptable
where inputVariable = '0' and (status = 999 or status is null)
or
inputVariable = '1' and (status != 999 or status is null)
or
(inputVariable is null or inputVariable not in ('0', '1'))
and (status = 999 or status is null) and date >= [whatever]
Note - even in the absence of null handling, you can't write the where clause with a case expression; that's because in one branch you require "not equal", and you can't do it with a simple case expression.
For one of my column in query I want a CASE statement.
The scenario is
If my column name ltt.f_complete task_completed value is 0 then the value shd be NO and if 1 then YES.
I tried like below but getting as
ORA-00905: missing keyword
here is query
SELECT flv.property_name project_name, flv.building building_name,
flv.flat_no unit_no,
ldet.customer_fname
|| ' '
|| ldet.customer_mname
|| ' '
|| ldet.customer_lname customer_name,
la.booking_date holding_date, ltt.start_date task_date,
tid.task_desc task_type, fol.next_follow_up_date,
ltt.remarks task_comment, ltt.f_complete task_completed,
CASE TASK_VALUE WHEN '0' THEN 'NO'
WHEN '1' THEN 'YES',
act.act_desc activity_description, flv.follow_type followup_type,
fol.remarks followup_comment,
fol.next_follow_up_date next_followup_date,
actt.act_desc next_todo_activity
FROM xxcus.xxacl_pn_leases_all la,
(SELECT *
FROM xxcus.xxacl_pn_lease_det
WHERE sr_no = 1) ldet,
xxcus.xxacl_pn_lease_task_trl ltt,
xxcus.xxacl_pn_customer_followup fol,
xxacl_pn_flat_det_v flv,
xxcus.xxacl_pn_hold_task_v tid,
xxcus.xxacl_pn_hold_act_v act,
xxcus.xxacl_pn_followup_type_v flv,
xxcus.xxacl_pn_hold_act_v actt
WHERE la.booking_no = ltt.draft_form_no(+)
AND ltt.draft_form_no = fol.booking_no(+)
AND ltt.task_id = fol.task_id(+)
AND ltt.task_sr_no = fol.task_sr_no(+)
AND la.delete_flag = 'N'
AND la.booking_no = ldet.booking_no
AND fol.followup_date = TO_DATE (SYSDATE - 1)
AND la.flat_id = flv.flat_id
AND ltt.task_id = tid.task_id
AND fol.activity_id = act.act_id
AND fol.followup_type_id = flv.follow_type_id
AND fol.next_activity_id = actt.act_id
kindly suggest what is wrong
UPDATE
SELECT flv.property_name project_name, flv.building building_name,
flv.flat_no unit_no,
ldet.customer_fname
|| ' '
|| ldet.customer_mname
|| ' '
|| ldet.customer_lname customer_name,
la.booking_date holding_date, ltt.start_date task_date,
tid.task_desc task_type, fol.next_follow_up_date,
ltt.remarks task_comment, ltt.f_complete task_completed,
act.act_desc activity_description, flv.follow_type followup_type,
fol.remarks followup_comment,
fol.next_follow_up_date next_followup_date,
actt.act_desc next_todo_activity
FROM xxcus.xxacl_pn_leases_all la,
(SELECT *
FROM xxcus.xxacl_pn_lease_det
WHERE sr_no = 1) ldet,
xxcus.xxacl_pn_lease_task_trl ltt,
xxcus.xxacl_pn_customer_followup fol,
xxacl_pn_flat_det_v flv,
xxcus.xxacl_pn_hold_task_v tid,
xxcus.xxacl_pn_hold_act_v act,
xxcus.xxacl_pn_followup_type_v flv,
xxcus.xxacl_pn_hold_act_v actt
WHERE la.booking_no = ltt.draft_form_no(+)
AND ltt.draft_form_no = fol.booking_no(+)
AND ltt.task_id = fol.task_id(+)
AND ltt.task_sr_no = fol.task_sr_no(+)
AND la.delete_flag = 'N'
AND la.booking_no = ldet.booking_no
AND fol.followup_date = TO_DATE (SYSDATE - 1)
AND la.flat_id = flv.flat_id
AND ltt.task_id = tid.task_id
AND fol.activity_id = act.act_id
AND fol.followup_type_id = flv.follow_type_id
AND fol.next_activity_id = actt.act_id
The keyword you are missing is END. It should read like this:
CASE TASK_VALUE WHEN '0' THEN 'NO'
WHEN '1' THEN 'YES' END
Hi I want to make a procedure like following:
CREATE OR REPLACE PROCEDURE SOL.INSERT_LD_NEXTPROCESS (vgroupid NUMBER)
IS
VPERIODID VARCHAR2 (10);
vPROCSESSID NUMBER;
CURSOR c
IS
SELECT COMPANYID,
GROUPID,
PERIODID,
FN_PPROCESSCURRENT
FROM LIQUIDATIONSDETAILS
WHERE PROCESSID = FN_PPROCESSPREVIOUS
AND (UNCOLLECTED > 0 OR INVOICE = 0)
I want to add an extra filter it depends from parameter:
CASE WHEN vgroupid > -1 then
AND GROUPID = vgroupid
ELSE
NULL
END
...
so the where cause like
WHERE PROCESSID = FN_PPROCESSPREVIOUS
AND (UNCOLLECTED > 0 OR INVOICE = 0) AND GROUPID = vgroupid
when vgroupid = -1 then I need all records an when vgroupid > -1 then I need only the records in vgroupid
any idea?
CURSOR c
IS
SELECT COMPANYID,
GROUPID,
PERIODID,
FN_PPROCESSCURRENT
FROM LIQUIDATIONSDETAILS
WHERE PROCESSID = FN_PPROCESSPREVIOUS
AND (UNCOLLECTED > 0 OR INVOICE = 0)
AND (((GROUPID = vgroupid) AND (vgroupid > -1)) OR (vgroupid = -1))
for example:
if vgroupid = -1, then last condition will be (((GROUPID = -1) AND (-1 > -1)) OR (-1 = -1)) or ((forever_false AND forever_false) OR (forever_true)) or (-1 = -1) - all records
instead, if vgroupid = 123 last condition will be (((GROUPID = 123) AND (123 > -1)) OR (123 = -1)) or (((GROUPID = 123) and forever_true) OR (forever_false)) or (GROUPID = 123) - only 123 GROUPID
I am creating a view and the same view converted into materialized view in the same system. But doing the same thing in another system I got error ORA-01722: invalid number when creating the materialized view. Why?
create materialized view MV_EMP_VALI
refresh complete with rowid start with SYSDATE+1/24 AS
(select * from V_CHA1);
View:-
CREATE OR REPLACE VIEW V_CHA1 AS(SELECT EMPNO,
MONTHYEAR,
to_number(SUM(CPFEMO)) AS EMOLUMENTS,
to_number(SUM(CPEPF)) AS EMPPFSTATUARY,
to_number(SUM(AEMO)) AS AEMO,
to_number(SUM(APEPF)) AS APEPF,
MAX(recsts) AS recsts
FROM ((SELECT RECDATE,
(CASE WHEN (REPFEMOFLAG='N') THEN
round(NVL(trim(EMO), 0))
ELSE
round(NVL(REVISEMO, 0)) END ) as CPFEMO,
round(NVL(trim(EPF), 0)) AS CPEPF,
0 as AEMO,
0 as APEPF,
'' as recsts,
EMPNO
FROM EMP_VALI
WHERE EFLAG = 'Y' AND SFLAG = 'N' AND EMPNO IS NOT NULL and
RECDATE >'01-Apr-2011')
union all
(SELECT NDT.RECDATE AS RECDATE,
sum(round(NVL(trim(NDT.EMO), 0))) as CPFEMO,
sum(round(NVL(trim(NDT.EPF), 0))) as CPEPF,
0 as AEMO,
0 AS APEPF,
NDT.EMPNO
FROM EMP_VALI VAL, EMP_SUPP NDT
WHERE VAL.EMPNO = NDT.EMPNO AND VAL.EFLAG = NDT.EFLAG AND
VAL.EFLAG = 'Y' AND VAL.SFLAG = 'Y' AND
NDT.SLIFLAG='N' and
VAL.EMPNO is not null and
NDT.RECDATE = VAL.RECDATE
GROUP BY NDT.RECDATE, NDT.EMPNO) UNION ALL
(SELECT DT.RECPAIDDATE AS RECDATE,
0 as CPFEMO,
0 as CPEPF,
sum(round(NVL(trim(DT.EMO), 0))) as AEMO,
sum(round(NVL(trim(DT.EPF), 0))) AS APEPF,
max('') as recsts,
DT.EMPNO
FROM EMP_VALI VAL, EMP_SUPP DT
WHERE VAL.EMPNO = DT.EMPNO AND VAL.EFLAG = DT.EFLAG AND
VAL.EFLAG = 'Y' AND VAL.SFLAG = 'Y' AND
VAL.EMPNO IS NOT NULL and dt.RECDATE=val.RECDATE AND DT.SFLAG IS NOT NULL AND DT.SFLAG not in ('N','F')
GROUP BY DT.RECPAIDDATE, DT.EMPNO)UNION ALL
(SELECT DT.RECPAIDDATE AS RECDATE,
SUM((CASE
WHEN (DT.ECR4FLAG = 'C') then
round(NVL(trim(DT.EMO), 0))
else
0
end)) as CPFEMO,
sum((CASE
WHEN DT.ECR4FLAG = 'C' then
round(NVL(trim(DT.EPF), 0))
else
0
end)) as CPEPF,
sum((CASE
WHEN DT.ECR4FLAG = 'A' then
round(NVL(trim(DT.EMO), 0))
else
0
end)) as AEMO,
sum((CASE
WHEN DT.ECR4FLAG = 'A' then
round(NVL(trim(DT.EPF), 0))
else
0
end)) as APEPF,
max(EMPRECOVERYSTS) as recsts,
DT.EMPNO
FROM EMP_VALI VAL, EMP_SUPP DT
WHERE VAL.EMPNO = DT.EMPNO AND VAL.EFLAG = DT.EFLAG AND
VAL.EFLAG = 'Y' AND VAL.SFLAG = 'Y' AND
VAL.EMPRECSTS = 'DEP' AND VAL.EMPNO IS NOT NULL and
dt.RECDATE = val.RECDATE AND DT.SFLAG IS NOT NULL AND
DT.SFLAG in ('F')
GROUP BY DT.RECPAIDDATE, DT.EMPNO))
GROUP BY RECDATE, EMPNO)
/
It's hard to tell from the statement, but if I had to guess, I put my money on the expression:
RECDATE >'01-Apr-2011'
assuming the column RECDATE is actually of type DATE. Therefor Oracle tries to convert the character value '01-Apr-2011' to a DATE as well. As you did not specify an format mask for this, the default NLS settings are used. If they define a number for the month then the above value would fail conversion.
You should never rely on implicit data type conversion. Especially not with dates. Use an ANSI literal instead:
RECDATE > DATE '2011-04-01'
or use the to_date() function with a format mask:
RECDATE > to_date('01-Apr-2011', 'dd-mon-yyyy')
Note that this could still fail for certain settings of NLS_LANG. In French you would need to specify 'Avr' instead of 'Apr'. So unless you are absolutely certain you can control all NLS_XXX settings all the time I'd strongly suggest to use month numbers instead. If you are more comfortable using to_date() than ANSI literals, you can use:
RECDATE > to_date('01-04-2011', 'dd-mm-yyyy')
Edit
if it's not the date column you need to check any other column for implicit data conversions.
These expression:
sum(round(NVL(trim(DT.EMO), 0)))
round(NVL(trim(EMOLUMENTS), 0))
round(NVL(trim(DT.EMO), 0))
round(NVL(trim(DT.EPF), 0))
look suspicious. If the columns in there are real numbers, then trim() is invalid and useless. If those are not numbers they could cause that error depending on the content of the column.
This expression to_number(SUM(CPFEMO)) is also useless as sum() will already return a number there is no reason to call to_number on a number(). Although I doubt it could raise your error you should still avoid it as it does not make any sense.