ODBC SQL - ROUND in HAVING clause is invalid - sql

I have the following command that I use to extract data through ODBC:
SELECT SALES_LEDGER.ACCOUNT_REF, SALES_LEDGER.BALANCE
FROM SALES_LEDGER
INNER JOIN AUDIT_SPLIT ON SALES_LEDGER.ACCOUNT_REF = AUDIT_SPLIT.ACCOUNT_REF
GROUP BY
SALES_LEDGER.ACCOUNT_REF, SALES_LEDGER.BALANCE
HAVING
ROUND(SUM(AUDIT_SPLIT.GROSS_AMOUNT), 2) <> SALES_LEDGER.BALANCE;
Running this in Excel without the ROUND is fine but I need the ROUND because otherwise the values of the SUM are not equal to BALANCE and I get false results.
The error in Excel is:
"ODBC: ERROR [42000] Syntax error: Invalid filter in HAVING clause"
Why can't I have the ROUND command there? I try with CAST as well but it is the ROUND that I really need there.

Related

What does XI8 mean in a SQL query? (Teradata/Big Query)

At work I saw I saw a SQL query in Teradata that had the following:
CASE WHEN (sp_ce / ‘0000001000000000’XI8) MOD 2 = 1 THEN ‘Y’ ELSE ‘N’ END AS res_p
What is the meaning of XI8? What is it doing to the number string?
Also when I tried to run this in Big Query it’s not supported, it errors out with
Syntax error: Unexpected identifier \XI8\
How would I write that case statement in big query?
Using MOD on big query without the numbers as a string and without the XI8 yields different results.
Thank you

SQL data type mismatch even after checking for numbers

I have the following sql statement:
SELECT SUM(temp.[ENDING TRUST BALANCE] * temp.[BORROWER FICO])
FROM (SELECT *
FROM [tape$A1:BR535]
WHERE ISNUMERIC([BORROWER FICO])) temp
WHERE temp.[BORROWER FICO] > 0
The nested sql statement should return a table that is all numeric. In fact, the * operation works however, when I add the WHERE clause I get the data type error. Any ideas?
UPDATE: got this working btw. for some reason blank string escaped this logic ie considered numbers. i just cleared those before running this and other scripts.

Error with group by in DB2 - SQLSTATE 42803

I've got a SQL script and its failing. This is what it looks like:
SELECT P.SOORT,P.TYPEBETALING, P.MIDDELCODE, P.HOEVEELHEID, P.EENHEID, P.BEDRAG,P.MIDDEL
FROM DAAO01.BETALINGEN as A join
DAAO01.KLANTEN P
on p.KLANT_ID = 1
GROUP BY P.TYPEBETALING
ORDER BY P.TYPEBETALING
When I execute this I get an error:
COLUMN OR EXPRESSION IN THE SELECT LIST IS NOT VALID. SQLCODE=-122, SQLSTATE=42803, DRIVER=4.18.60
What am I doing wrong?
It is quite difficult to tell what you're trying to do without seeing your data.
But the error is saying that you have not specified how you are going to deal with the rest of the fields in your Group by aggregation:
P.SOORT, P.MIDDELCODE, P.HOEVEELHEID, P.EENHEID, P.BEDRAG, P.MIDDEL
If they're numbers, then you could sum them or take the avg etc.
If they're strings, then you either need to group by them or remove them from your selection.

ORA-01722: invalid number error

I am running the below mentioned query in my Oracle client and i am getting
ORA-01722: invalid number
error. I know the issue is due to the TAG_VALUE column being of type "varchar2" and i am converting it to number and then using that field in where clause. I have tried using "CAST" function but that is also not helping.
If i run the query neglecting the last where condition with code WHERE (P.TAG_VALUE > '100') then i am getting the result but including the last where clause gives me error.
SELECT DISTINCT
count(P.CREATED_DATETIME)
FROM
(
select OUTPUT_TAG_ID,TO_NUMBER(TAG_VAL,'9999.99') AS
TAG_VALUE,TAG_VAL_TS,CREATED_DATETIME
from OV80STG.PRCSD_DATA_OUTPUT_ARCHIVE
where MODEL_CODE='MDLADV1538'
AND TAG_VAL <> 'U_Transfer_rate'
) P
WHERE
(P.TAG_VALUE > '100')
Any suggestion will be appreciated. Thanks.
Remove the single quotes from around the value in the where, you don't need them when its an integer. query will be like this:
SELECT DISTINCT
COUNT(P.CREATED_DATETIME)
FROM
(
SELECT
OUTPUT_TAG_ID,
TO_NUMBER(TAG_VAL, '9999.99') AS TAG_VALUE,
TAG_VAL_TS,
CREATED_DATETIME
FROM OV80STG.PRCSD_DATA_OUTPUT_ARCHIVE
WHERE MODEL_CODE = 'MDLADV1538'
AND TAG_VAL <> 'U_Transfer_rate'
) P
WHERE(P.TAG_VALUE > 100);
TO_NUMBER function returns a numeric value so, as mentioned in comment, you shouldn't compare it with string value.
I solved the issue by including outer where clause inside the subquery and then I got the required result without any error.

"Not Valid Expression" using SUM function with a single column (decimal, 11, 2)

In excel, I am rewriting the commands within a query from AS400. One column that I am trying to rewrite is decimal 11, 2.
Here is exactly what I wrote:
SUM (CA.B7A5QT) AS "Transaction Quantity"
Each time that I attempt to run this, it returns that it is an invalid expression.
Since you didn't provide the whole query: is there a chance, that 'CA.B7A5QT' eventually becomes NULL?
If so try the following syntax:
SUM (IFNULL(CA.B7A5QT, 0)) AS "Transaction Quantity"