error on statement execution when creating a view - sql

Could someone please help in explaining the error I get when executing following statement:
Error starting at line 1 in command:
`
CREATE OR REPLACE FORCE VIEW "PRODILMOWNER"."ZV_LPEUR_I" ("MANDT","BALANCETYPE", "BRANCH", "CURRENCY", "AMOUNT", "COUNTERPARTY", "COUNTERPARTY_PARENT", "AMOUNT_EUR","DESCRIPTION")
AS
SELECT a.balancetype,
a.branch,
a.currency,
cast a.amount as number (25,2),
a.counterparty,
a.counterparty_parent,
a.mandt,
a.description,
(a.amount*
(SELECT c.midspot
FROM ZV_EXCHANGERATES c
WHERE c.currency =a.currency
AND c.valuedate =TO_CHAR(sysdate,'YYYY/MM/DD')
AND a.valuedate = TO_CHAR(sysdate,'DD-MMM-YY')
) ) AS cast amount_eur as number (25,2),
FROM ZT_LP a
Error at Command Line:1 Column:0
Error report:
SQL Error: No more data to read from socket

You may have other errors, but the correct way to express a cast is:
cast(a.amount as number(25, 2)) as amount,
Note the parentheses and the column alias.

If you try to create view as following :
CREATE OR REPLACE FORCE VIEW "PRODILMOWNER"."ZV_LPEUR_I" ("MANDT","BALANCETYPE", "BRANCH", "CURRENCY", "AMOUNT", "COUNTERPARTY", "COUNTERPARTY_PARENT", "AMOUNT_EUR","DESCRIPTION")
AS
SELECT a.balancetype,
a.branch,
a.currency,
cast(a.amount as number(25, 2)) as amount, --cast a.amount as number (25,2),
a.counterparty,
a.counterparty_parent,
a.mandt,
a.description,
a.amount*
(SELECT cast(c.midspot as number(25, 2))
FROM ZV_EXCHANGERATES c
WHERE c.currency =a.currency
AND c.valuedate =TO_CHAR(sysdate,'YYYY/MM/DD')
AND a.valuedate = TO_CHAR(sysdate,'DD-MMM-YY')
) AS amount_eur -- cast amount_eur as number (25,2) --,
FROM ZT_LP a;
by removing parts right next to dashes(--), then no problem remains.
take casts in paranthesis
take second cast inside subselect
remove the comma , just before FROM clause

Related

The replacement for double colon to set the value in postgresql

with recursive tree_Gy_Department as(
select PreGD.*, 1::integer recursion_level
from GY_DEPARTMENT PreGD
where PreGD.dept_id = :deptId
union all
select NextGD.*, recursion_level +1
from GY_DEPARTMENT NextGD
join tree_Gy_Department treeGD on treeGD.parent_id = NextGD.dept_id)
select recursion_level, a.dept_name,
case
when recursion_level = 1 then REGEXP_replace(initcap(a.DEPT_NAME), '\\s', '')
else REGEXP_replace(initcap(a.DEPT_NAME), '[[:lower:]]|\\s', '', 'g') END
AS Result
from tree_Gy_Department a;
I'm trying to run this query and it works in the console query of PostgreSQL but when I put it in the repository it got an error: ERROR: syntax error at or near ":". I think the error occurred when I set the value for recursion_level "1::level recursion_level", maybe a conflict with hibernate. Does anyone have a replacement for this double colon?
Use the standard cast() syntax instead. Hibernated gets confused by the ::
cast(1 as integer) as recursion_level
But I don't think you need the cast at all. A simple 1 as recursion_level will work just as well.

How to fix SQL error SAP HANA while using aggregate function

If I run this sql in SAP HANA, It generates the following error.
(SQL Editor) Could not execute 'WITH l1 AS( SELECT REMOTE_SOURCE_NAME,OWNER,NAME,MAX(START_TIME) START_TIME,RESULT_STATE FROM ...'
Error: (dberror) 266 - inconsistent datatype: only numeric type is available for SUM/AVG/STDDEV/VAR function: line 43 col 7 (at pos 654)
If I exclude this part of sql select sum(LOADING_TIME) AS TOTAL_LOADING_TIME, then it works.
How can I solve the issue to get total_loading_time?
WITH l1 AS(
SELECT REMOTE_SOURCE_NAME,OWNER,NAME,MAX(START_TIME) START_TIME,RESULT_STATE
FROM "MEAG_EIM_SHARED"."meag.eim.shared::replication.Log"
WHERE TYPE = 'INITIAL' AND RESULT_STATE = 'COMPLETED'
AND REMOTE_SOURCE_NAME='RS_SDI_IMMO'
group by REMOTE_SOURCE_NAME,OWNER,NAME,RESULT_STATE
order by NAME ASC),
l2 AS
(SELECT REMOTE_SOURCE_NAME,OWNER,NAME,START_TIME,END_TIME,RESULT_STATE
FROM "MEAG_EIM_SHARED"."meag.eim.shared::replication.Log"
WHERE TYPE = 'INITIAL' AND RESULT_STATE = 'COMPLETED'
AND REMOTE_SOURCE_NAME='RS_SDI_IMMO'
group by REMOTE_SOURCE_NAME,OWNER,NAME,RESULT_STATE,START_TIME,END_TIME
order by NAME ASC)
select sum(LOADING_TIME) AS TOTAL_LOADING_TIME
from(
select layer1.REMOTE_SOURCE_NAME,layer1.OWNER,layer1.name,layer1.RESULT_STATE,layer1.start_time start_time,layer2.end_time end_time,
SECONDS_BETWEEN(layer1.START_TIME,layer2.END_TIME) || 's' LOADING_TIME
from l1 layer1 inner join l2 AS layer2
on layer1.start_time=layer2.start_time
order by name);
LOADING_TIME has the character ā€žsā€œ concatenated to it - that turns the numeric value into a string.
And for strings there is no SUM() function.
If having the ā€žsā€œ is important then adding it after the summation will fix the issue.
Otherwise, adding the unit denomination (I guess the s stands for seconds) to the column name would allow client tools to still work with the numeric value (I.e. when sorting)

What is the syntax problem here using this subquery inside where clause

SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.

Syntax error in query expression using sub query

SELECT
(
SELECT Sum(tbl_allTransactions.transAmount) AS SumOftransAmount
FROM tbl_allTransactions
WHERE (((tbl_allTransactions.[transType])='Expense')) OR (((tbl_allTransactions.[transType])='Budget') AND ((tbl_allTransactions.[transMonth])=[#transMonth]))
GROUP BY tbl_allTransactions.transMonth
,
SELECT Sum(tbl_allTransactions.transAmount) AS SumOftransAmount
FROM tbl_allTransactions
WHERE (((tbl_allTransactions.[transType])='Expense')) OR (((tbl_allTransactions.[transType])='Budget') AND ((tbl_allTransactions.[transMonth])=[#transMonth]))
GROUP BY tbl_allTransactions.transMonth
)
FROM tbl_allTransactions WHERE (((tbl_allTransactions.[userID])=[#userID]))
I am getting following error:-
System.Data.OleDb.OleDbException (0x80040E14) Syntax error in query
expression '(SELECT Sum(tbl_allTransactions.transAmount) AS
SumOftransAmount FROM tbl_allTransactions WHERE
(((tbl_allTransactions.[transType])='Expense')) OR
(((tbl_allTransactions.[transType])='Budget') AND
((tbl_allTransactions.[transMonth])=[#transMonth])) GROUP '. at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResulthr) at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS
dbParams, Object& executeResult) at
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Obje
The original code was missing a closing parenthesis on the first subquery. The code was also simplified by consolidating the transType conditional from 2 statements to 1. Extra brackets were removed to enhance readability. Also column aliases were added to output of each subquery. Since both queries render the same data, the second column was called "SumOftransAmount2"
SELECT
(SELECT Sum(tbl_allTransactions.transAmount)
FROM tbl_allTransactions
WHERE tbl_allTransactions.transType in ('Expense', 'Budget') AND tbl_allTransactions.transMonth = #transMonth
GROUP BY tbl_allTransactions.transMonth) as SumOftransAmount
,
(SELECT Sum(tbl_allTransactions.transAmount)
FROM tbl_allTransactions
WHERE tbl_allTransactions.transType in ('Expense', 'Budget') AND tbl_allTransactions.transMonth = #transMonth
GROUP BY tbl_allTransactions.transMonth) as SumOftransAmount2
FROM tbl_allTransactions
WHERE tbl_allTransactions.userID=#userID

How to reuse a dynamic column in another column calculation in MySQL?

I have this kinda query:
SELECT
IF(daycode=1,(SELECT...),(SELECT...)) AS weekavg,
(SELLOFF1 / weekavg) AS procent
FROM .....
it tells me: Unknown column 'weekavg' in 'field list', this happens after I've added the divide, prior to that worked ok.
You can use a sub-query:
SELECT (selloff1 / weekavg) AS procent
FROM (
SELECT
selloff1, IF(daycode=1,(SELECT...),(SELECT...)) AS weekavg
FROM ...
)