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

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)"

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

MSSQL use result from case statement [duplicate]

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,
...

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?

Selecting values from RECORD variable [duplicate]

This question already has answers here:
EXECUTE...USING statement in PL/pgSQL doesn't work with record type?
(2 answers)
Closed 6 years ago.
As part of my k-medoid algorithm, I have declared a record variable called o_random in my function, which I use to store a random row retrieved from a table of crimes as in this query:
EXECUTE 'SELECT * FROM algorithms.kmedoid_crimes_' ||k||' WHERE
cluster_id='||k_count||' OFFSET floor(random()*'||row_count||') LIMIT 1'
INTO o_random;
However, the below query is giving me an error saying "could not identify column "latitude" in record data type". The crimes table I am getting the row from definitely has a field called latitude.. so I'm thinking I'm trying to get the data from the record variable wrongly?
EXECUTE 'UPDATE algorithms.km_cluster_centres_' ||k||'
SET latitude = $1.latitude, longitude = $1.longitude, geom =
ST_Transform(ST_SetSRID(ST_MakePoint($1.longitude, $1.latitude), 4326),3435)
WHERE id=' ||k_count
USING o_random;
Found some solutions on stack but none of them seem to work.. help would be appreciated.
Just a guess
EXECUTE 'UPDATE algorithms.km_cluster_centres_' || quote_literal(k) ||'
SET latitude = ($1).latitude, longitude = ($1).longitude, geom =
ST_Transform(ST_SetSRID(ST_MakePoint(($1).longitude,($1).latitude), 4326),3435)
WHERE id=' || quote_literal( k_count )
USING o_random;
UPDATE: If it dosen't work try to log with http://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html

Fetching SQL data in R with parameters [duplicate]

This question already has answers here:
Pass R variable to RODBC's sqlQuery? [duplicate]
(4 answers)
Closed 7 years ago.
I would like to extract some data from a MySQL server. Therefore Im using the following code:
#connect to database
getPlayersHome <- dbGetQuery(con,"SELECT * FROM match_player_home WHERE match_id = 1;")
This works fine, however I would like include it in a function with a parameter. But if I do this:
getData <- function(selector){
getPlayersHome <- dbGetQuery(con,"SELECT * FROM match_player_home WHERE match_id = selector;")
}
My query does not work (it returns everything). Any thoughts on what goes wrong here?
You are using the selector parameter incorrectly, as you wrote it selector it's interpreted as a literal. You can use sprintf to create the sql string passing parameters:
getData <- function(selector){
sql <- sprintf("SELECT * FROM match_player_home WHERE match_id = %s", selector)
rs = dbSendQuery(con, sql)
}
The string "%s" inside sprintf will be substitute by the value of selector.