MSSQL use result from case statement [duplicate] - sql

This question already has answers here:
getting "invalid column" when trying to use column alias in a query
(3 answers)
Closed 1 year ago.
SELECT .....
'NewReserveSum' =
case
when h.ApprovedCurrencyID = 417 then new.ReserveSumV
else [dbo].[GetCurrencyRate](h.ApprovedCurrencyID, #PresentDate) * new.ReserveSumV
end,
(o.ReserveSummN - NewReserveSum) as 'DifferenceReserveSummN'
FROM ......
I want to use the ** NewReserveSum ** variable in the following columns, but I got an error:
Invalid column name 'NewReserveSum'
How can I do it correctly in MSSQL?

In SQL, = is comparison, not assignment
In a SELECT statement, you can use AS to give a name top an expression column:
SELECT
case
when h.ApprovedCurrencyID = 417 then new.ReserveSumV
else [dbo].[GetCurrencyRate](h.ApprovedCurrencyID, #PresentDate) * new.ReserveSumV
end as NewReserveSum,
...

Related

How to fix error in sql "Execution finished with errors. Result: near "(": syntax error"? [duplicate]

This question already has an answer here:
Replacement for Left command in SQLite SQL
(1 answer)
Closed 7 months ago.
How to remove all characters from / to &.
UPDATE bal SET Perevod=substr(Perevod, INSTR(Perevod, '/.+&')-1)
WHERE INSTR(Perevod, '/.+&')>0;
You can use this because I believe there is no LEFT function in SQLite:
UPDATE bal
SET Perevod = substr(Perevod, 1, INSTR(Perevod, '/')-1)
WHERE INSTR(Perevod, '/')>0;
DEMO

SQL Error [909] [42000]: ORA-00909: invalid number of arguments [duplicate]

This question already has an answer here:
Getting error ORA-00909: invalid number of arguments
(1 answer)
Closed 1 year ago.
I'm trying to retrieve report from postgreSQL database to Oracle data base, but it isn't working as I thought. I tried to find related tables in Oracle database and found them as SOR tables, some data is in Datamarts.
SQL Error [909] [42000]: ORA-00909: invalid number of arguments
SQL statement that works just fine in postgreSQL:
`SELECT
'loan' as product,
count(cc.id),
cc."type",
cc.topic,
cc.direction,
TRIM(cc.subject) AS subject,
case when EXISTS (SELECT caf.communication_id
FROM serp_bigmoney_lv.comm_attachment_files caf where cc.id = caf.communication_id) then
'yes'
else 'no' end as file_attached,
concat(us."name",' ',us.surname) as user_name,
cc.createdat::date
from erp_bigmoney_lv.comm_communication cc
left join erp_bigmoney_lv.erp_users us on us.user_id = cc.created_by
where cc."type" = 'note'
GROUP BY cc.createdat::date, user_name, file_attached, cc.direction, cc.topic, cc."type",
product, cc.subject`
CONCAT function only accept two arguments use || to concatenate the fields like:
FIELD_1 || ‘ ‘ || FIELD_2

Database query select where most recent date - MAX not working [duplicate]

This question already has answers here:
SQL How to Select the most recent date item
(6 answers)
Closed 2 years ago.
I would like to select last inserted data in my table.
I tried :
Source = "SELECT * FROM HistoSpreadLiq WHERE DateSpread=MAX(DateSpread)"
But this doesn't work -> returns : Error Automation..
MAX(DateSpread) should return 04/11/2020
DateSpread is a date format in my AccessDB
And when I hard code:
Source = "SELECT * FROM HistoSpreadLiq WHERE DateSpread=04/11/2020"
It does work, what am I missing?
Please note that I execute this SQL request in Excel and my database is an Access database (.accdb)
Then answer was that I had to do an other select :
"SELECT * FROM HistoSpreadLiq WHERE DateSpread=(SELECT Max(DateSpread) FROM HistoSpreadLiq)"

Error when trying to create case statement In MS Access [duplicate]

This question already has an answer here:
MS Access Query with CASE statement
(1 answer)
Closed 3 years ago.
Getting Syntax Error( missing operator) in query expression, What am I getting wrong?
SELECT
ExportUF_NEW.Position,
ExportUF_NEW.[User Defined Field 03]
(CASE
WHEN ExportUF_NEW.[User Defined Field 03] = OP THEN "Production"
WHEN ExportUF_NEW.[User Defined Field 03] = STM THEN "Thermal"
ELSE NULL
END) AS OperationGroup
FROM ExportUF_NEW
WHERE (((ExportUF_NEW.[User Defined Field 03]) Is Not Null))
Expect an outcome to new column "OperationGroup" based on ExportUF_NEW[User Defined Field 03].
MS Access did not support CASE WHEN, use switch instead.
Similar link: What is the equivalent of Select Case in Access SQL?

Simple SQL command not properly ended [duplicate]

This question already has answers here:
SQL Oracle LEFT JOIN and SUBQUERY error: ORA-00905: missing keyword
(2 answers)
Closed 9 months ago.
I have the following query:
SELECT *
FROM DELUSR.AGREEMENT AS agreement
WHERE agreement.MASTER_AGGREMENT_ID = 4;
After trying to run it, I get this error:
Error code 933, SQL state 42000:
ORA-00933: SQL command not properly ended
Line 1, column 1
Execution finished after 0 s, 1 error(s) occurred.
I am not sure how to decode this error message. The statement looks fine to me. Any insights into whats wrong with it?
Oracle accepts no AS for table aliases:
SELECT * FROM DELUSR.AGREEMENT agreement WHERE agreement.MASTER_AGGREMENT_ID = 4;