While executing the below code:
select distinct tam.sol_id||'|'||(select sol.sol_desc from sol where sol.sol_id=tam.sol_id)||'|'||count(*)||'|'||sum(org_tran_amt)
||'|'||count(case when ott.tran_date between '01-02-2021' and '24-02-2021' as 1 else 0 end) a
from ott,tam
where tam.acid=ott.acid
and tam.gl_sub_head_code in ('85300','85320','85330','85340','85350','85360','85365','85370','85380','85390','85395')
and tran_date <= '24-02-2021'
and tam.sol_id in (select sst.sol_id from sst where sst.set_id='ROFPZ')
and not exists (select * from tct where tct.tran_date=ott.tran_date and trim(tct.tran_id)=trim(ott.tran_id)
and nvl(ott.org_tran_amt-tct.AMT_OFFSET,0)='0' and tct.entity_cre_flg='Y' and tct.del_flg='N');
received below error message as output:
from ott,tam
*
ERROR at line 3:
ORA-00905: missing keyword
Try to modify the case part of the statement:
case when ott.tran_date between '01-02-2021' and '24-02-2021' then 1 else 0 end
You must write "then" instead of "as".
Related
Trying to make a select query on teradata, but i get this message error:
syntax error: invalid select expression list
I can't fix it. How can I formulate the query appropriately?
SELECT BILS01_GRADO, BILS01_CODICE_COM, BILS01_PROT, BILS01_PROG_OGG_IMP,BILS01_PROG_REC,
CASE WHEN BILS01_DATA_PRES_ISTSOSP<TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP=0 THEN BILS01_DATA_PRES_ISTSOSP
WHEN BILS01_DATA_PRES_ISTSOSP=TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP>0 THEN DATA_CONTROVERSIA2
WHEN BILS01_DATA_PRES_ISTSOSP=TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP=0 THEN TO_DATE('9999-12-31','YYYY-MM-DD')
WHEN BILS01_DATA_PRES_ISTSOSP<
(
CASE WHEN
(
CASE WHEN BILS01_DATA_SPED<to_date('9999-12-31','YYYY-MM-DD') THEN BILS01_DATA_SPED
WHEN BILS01_DATA_SPED=to_date('9999-12-31','YYYY-MM-DD') OR BILS01_DATA_RIC<to_date('9999-12-31','YYYY-MM-DD')THEN BILS01_DATA_RIC
WHEN BILS01_DATA_ACQ<to_date('9999-12-31','YYYY-MM-DD') THEN BILS01_DATA_ACQ ELSE BILS01_DATA_PROT END AS DATA_CONTROVERSIA
)
<TO_DATE('1972-01-01','YYYY-MM-DD') THEN TO_DATE('1972-01-01','YYYY-MM-DD') ELSE DATA_CONTROVERSIA END AS DATA_CONTROVERSIA2
)
THEN BILS01_DATA_PRES_ISTSOSP ELSE DATA_CONTROVERSIA2 END AS A) AS OUT_DATA_RICH_SOSP
FROM zucow.BILS01
--GROUP BY BILS01_GRADO, BILS01_CODICE_COM, BILS01_PROT, BILS01_PROG_OGG_IMP,
-- BILS01_PROG_REC, OUT_DATA_RICH_SOSP
How to run procedure without parameter in Teradata
I'm trying with : call db1.proc_dbOSA()
Error Msg:
Call failed 3707: PROC_DBOSA: Syntax error, expected something
like a name or a Unicode delimited identifier between ‘(‘ and ‘)’
New Procedure with error result.
When i run only code then everything works ok.
REPLACE PROCEDURE db1.proc_dbOSA()
BEGIN
DELETE FROM db1.LOG_dbOSA;
INSERT INTO
db1.LOG_dbOSA
(StoreNo, IDX, Flow, Status, MRP, OSA_IDX, OSA_AC)
WITH a AS (
SELECT
c.StoreCode,
CAST(SUBSTRING(c.ArticleCode FROM 13 FOR 6) AS INT) AS IDX,
RpType,
CASE
WHEN c.MinimumTargetStockWrpl >= l.MinimumTargetStockWrpl THEN CAST(l.MinimumTargetStockWrpl AS INT)
WHEN c.MinimumTargetStockWrpl < l.MinimumTargetStockWrpl THEN CAST(c.MinimumTargetStockWrpl AS INT)
End AS StoreMin,
c.ValUnrestrictedStock
FROM
db1.tab1 c
INNER JOIN
(
SELECT
StoreCode,
ArticleCode,
MinimumTargetStockWrpl
FROM
db1.tab1
WHERE
ProcessingDate = CURRENT_DATE - 14
) l ON c.StoreCode = l.StoreCode AND c.ArticleCode = l.ArticleCode
WHERE
c.ProcessingDate = CURRENT_DATE AND c.MinimumTargetStockWrpl IS NOT NULL AND l.MinimumTargetStockWrpl IS NOT NULL AND l.MinimumTargetStockWrpl > 0
)
, t AS
(
SELECT
CAST(SUBSTRING(ArticleCode FROM 13 FOR 6) AS INT) AS IDX,
RpType,
ArticlesPlanner
FROM
DWH_db_V.STK_B_ARTICLE_DAY_V
WHERE
ProcessingDate = CURRENT_DATE AND StoreCode = 'DR04'
)
SELECT
a.StoreCode,
a.IDX,
t.RpType,
t.ArticlesPlanner,
a.RpType,
CASE
WHEN a.ValUnrestrictedStock > 0 THEN 1
WHEN a.ValUnrestrictedStock <= 0 THEN 0
End AS OSA_IDX,
CASE
WHEN a.ValUnrestrictedStock >= StoreMin THEN 1
WHEN a.ValUnrestrictedStock < StoreMin THEN 0
End AS OSA_AC
FROM
a
LEFT JOIN
t ON t.IDX = a.IDX;
End;
BTEQ Error:
+---------+---------+---------+---------+---------+---------+---------+----
Call proc_dbOSA;
Call proc_dbOSA;
$
* Failure 3707 Syntax error, expected something like '(' between the word
'proc_dbOSA' and ';'.
Statement# 1, Info =18
* Total elapsed time was 1 second.
Call proc_dbOSA();
* Failure 3707 PROC_DBOSA:Syntax error, expected something like a name or
a Unicode delimited identifier between '(' and ')'.
* Total elapsed time was 1 second.
Stored procedures do not support the following:
EXPLAIN and USING request modifiers within a stored procedure
EXECUTE macro statement
WITH clause within a stored procedure.
Stored procedures, as well as macros, do not support the following query logging statements:
BEGIN QUERY LOGGING
END QUERY LOGGING
FLUSH QUERY LOGGING
REPLACE QUERY LOGGING
I've encountered an issue when trying to obtain the following output:
"If x_date >= now-365 then 1 else 0"
My select statement reads:
SELECT
id,
x_date,
CASE x_date
WHEN x_date >= addday(cast(now() as date),-365) then 1
else 0
end as output
I'm receiving an error message that reads:
"SQL Error [30100] [HY000]: CASE argument case((xdate,ge,[addday(trunc(cast('date', now(), 'DATE')) '-365')], utc_il8n), 'true', 'false') is not compatible with the rest of the values.
Has anyone else performed a similar operation with dates in a CASE statement? The Addday works fine and returns 2017-01-05.
Issue with the CASE syntax. I think reading this and other sources may have cause the confusion: https://www.techonthenet.com/sql_server/functions/case.php
Should read:
SELECT
id,
x_date,
CASE WHEN x_date >= addday(cast(now() as date),-365) then 1
else 0
end as output
I'm attempting to execute a simple "if/then" statement on an Oracle SQL database using RODBC in R. The SQL statement works fine in SQL Developer v4.0.2.15 but throws an error when performing the same statement in R
sqlQuery(channel, "
select
Variable1,
Variable2,
CASE WHEN Variable1 = 0 then 0 else 1 end as Var3
from schema.TABLE
where ROWNUM<100;
"
)
The error message (updated):
[1] "[1] "HY000 936 [Oracle][ODBC][Ora]ORA-00936: missing expression\n"
[2] "[2] ...
The statement works fine when removing the CASE WHEN line, so the error must be in the CASE WHEN syntax.
I've found a semicolon sometimes causes problems in cases like this.
> sqlQuery(con, "select case when dummy = 'X' then 1 else 0 end from dual")
CASEWHENDUMMY='X'THEN1ELSE0END
1 1
> sqlQuery(con, "select case when dummy = 'X' then 1 else 0 end from dual;")
[1] "HY000 911 [Oracle][ODBC][Ora]ORA-00911: invalid character\n"
[2] "[RODBC] ERROR: Could not SQLExecDirect 'select case when dummy = 'X' then 1 else 0 end from dual;'"
> close(con)
If you look at the SQL statement (which seems to appear in it's entirety in the error message), we see:
'\nselect\n UW_NET_ULTIMATE_LOSS_USD,\n UW_NET_WRITTEN_PREMIUM_USD,\n
IF UW_NET_WRITTEN_PREMIUM_USD = 0 THEN testvar=0 ELSE testvar=1 end\n
from bqiread.POLICY_METRICS\nwhere ROWNUM<100;\n
There's no CASE statement here....it apprears to be an IF/THEN/ELSE, which is not valid Oracle syntax.
Perhaps your tool is generating SQL for a different RDBMS than Oracle, and attempting to execute it in Oracle?
I am trying to execute case statement but i am getting an error. Here is my query
select LOSA_APP.app_ref_no AS "App.Ref.No.", CODE_BRANCH.branch_name AS "BRANCH"
from losa_app LOSA_APP
INNER JOIN
losa_app_z LOSA_APP_Z
ON
losa_app.app_ref_no = losa_app_z.app_ref_no
INNER JOIN
code_branch CODE_BRANCH
ON
LOSA_APP.attend_branch = CODE_BRANCH.branch_id
where
LOSA_APP.app_status='A' -- Application Status in {‘accepted’}
and
trunc(sysdate) between (nvl(LOSA_APP_Z.li_dt, LOSA_APP_Z.li_collect_dt)) AND ((trunc(sysdate))) -- falling under the reporting period
and
(trunc(sysdate) - nvl(losa_app_z.li_dt,losa_app_z.li_collect_dt)) > 90 -- select application cases at any step after entering Documentation flows.
and
losa_app.product_type = 'MG' -- Select records based on input parameter value passed in.
and
case :input1
when 'ABB' Then code_branch.branch_code1 like '%0232%'
when 'AiBB' Then code_branch.branch_code1 like '%0347%'
end
While executing it asks me for input. After that it gives me error that
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
Error at Line: 22 Column: 50
Line 22 is when 'ABB' Then code_branch.branch_code1 like '%0232%'
What keyword i am missing?
Thanks
Don't try to return a logical boolean from the CASE statement.
Instead, make the CASE statement part of the logical test. Such as...
code_branch.branch_code1
like
case :input1 when 'ABB' Then '%0232%'
when 'AiBB' Then '%0347%' end
try this one
SELECT ....
FROM...
WHERE ....
AND
(
((:input1 = 'ABB') AND code_branch.branch_code1 like '%0232%') OR
((:input1 = 'AiBB') AND code_branch.branch_code1 like '%0347%')
)