Update Row, Inner Join With Getting Top Row - sql

Using MariaDB
I am trying to update the membershipRenewDate(In officiantsDetails Table) using the MAX value from renewDate(in officiantsRenewals Table)
Inner Join on officiant_id (same column name for both tables)
I have something like this but am getting syntax error.
UPDATE officiantsDetails offd
SET offd.membershipRenewDate = offr.renewDate
FROM (SELECT TOP (1) renewDate, officiant_id FROM officiantsRenewals ORDER BY renewDate DESC ) as offr
WHERE offd.officiant_id = offr.officiant_id

You should be able to use a Common Table Expression (CTE) for this. You use the WITH clause to define your "subquery", and then INNER JOIN the CTE to the table you want to update. It would look something like this:
WITH Top1RenewDate AS
(
SELECT TOP (1)
renewDate,
officiant_id
FROM officiantsRenewals
ORDER BY renewDate DESC
)
UPDATE offd
SET offd.membershipRenewDate = offr.renewDate
FROM officiantsDetails offd
INNER JOIN Top1RenewDate offr ON offd.officiants_id = offr.officiants_id
This same syntax will also work for SQL Server, although you'd need to add a ; before the WITH keyword on that system.

The syntax you are using is not MariaDB. In MariaDB, you can use a JOIN:
UPDATE officiantsDetails offd JOIN
(SELECT renewDate, officiant_id
FROM officiantsRenewals
ORDER BY renewDate DESC
LIMIT 1
) offr
ON offd.officiant_id = offr.officiant_id
SET offd.membershipRenewDate = offr.renewDate;
The syntax you are using is more reminiscent of SQL Server.

Related

Group BY clause to temporary table

I am beginner in SQL trying to group by a result
SELECT
API.PROJECTNUMBER, Project.ProjectName, Project.PROJMGRID, Staff.Email
INTO
#TEMPUnmatchedProjectWithSMoD
FROM
{ProjectsIncidentsIntegration} API WITH(NOLOCK)
JOIN
{Project} project WITH(NOLOCK) ON Project.ProjectNumber = API.PROJECTNUMBER
JOIN
{COMMON_STAFF} Staff WITH(NOLOCK) ON Staff.EmplId = Project.PROJMGRID
WHERE
NOT EXISTS (SELECT 1
FROM #DBName.DBO.NCompasS_TBL_SMoDIncident SMoD WITH(NOLOCK)
WHERE SMoD.ProjectCode = API.PROJECTNUMBER)
SELECT *
FROM #TEMPUnmatchedProjectWithSMoD
GROUP BY Project.PROJMGRID
I get an error:
Database returned the following error:
Error in advanced query UnmatchedProjectWithSMoD2: The multi-part identifier "Project.PROJMGRID" could not be bound.
The problem appears to be in this query:
SELECT *
FROM #TEMPUnmatchedProjectWithSMoD
GROUP BY Project.PROJMGRID ;
There are two major issues:
SELECT * with GROUP BY is not correct. What should be done with the columns that are not aggregation keys? You might have some idea. SQL generates an error.
Project is not defined.
It is quite unclear what this code is supposed to be doing, so I cannot suggest anything useful. You can just select all rows from the temporary table using:
SELECT p.*
FROM #TEMPUnmatchedProjectWithSMoD p;

SQL - select only newest record with WHERE clause

I have been trying to get some data off our database but got stuck when I needed to only get the newest file upload for each file type. I have done this before using the WHERE clause but this time there is an extra table involved that is needed to determine the file type.
My query looks like this so far and i am getting six records for this user (2x filetypeNo4 and 4x filetypeNo2).
SELECT db_file.fileID
,db_profile.NAME
,db_applicationFileType.fileTypeID
,> db_file.dateCreated
FROM db_file
LEFT JOIN db_applicationFiles
ON db_file.fileID = db_applicationFiles.fileID
LEFT JOIN db_profile
ON db_applicationFiles.profileID = db_profile.profileID
LEFT JOIN db_applicationFileType
ON db_applicationFiles.fileTypeID = > > db_applicationFileType.fileTypeID
WHERE db_profile.profileID IN ('19456')
AND db_applicationFileType.fileTypeID IN ('2','4')
I have the WHERE clause looking like this which is not working:
(db_file.dateCreated IS NULL
OR db_file.dateCreated = (
SELECT MAX(db_file.dateCreated)
FROM db_file left join
db_applicationFiles on db_file.fileID = db_applicationFiles.fileID
WHERE db_applicationFileType.fileTypeID = db_applicationFiles.FiletypeID
))
Sorry I am a noob so this may be really simple, but I just learn this stuff as I go on my own..
SELECT
ff.fileID,
pf.NAME,
ff.fileTypeID,
ff.dateCreated
FROM db_profile pf
OUTER APPLY
(
SELECT TOP 1 af.fileTypeID, df.dateCreated, df.fileID
FROM db_file df
INNER JOIN db_applicationFiles af
ON df.fileID = af.fileID
WHERE af.profileID = pf.profileID
AND af.fileTypeID IN ('2','4')
ORDER BY create_date DESC
) ff
WHERE pf.profileID IN ('19456')
And it looks like all of your joins are actually INNER. Unless there may be profile without files (that's why OUTER apply instead of CROSS).
What about an obvious:
SELECT * FROM
(SELECT * FROM db_file ORDER BY dateCreated DESC) AS files1
GROUP BY fileTypeID ;

UPDATE query with inner joined query

I haved saved SELECT query. I need create update query to update table field with value from saved select query.
Im getting error "Operation must use an updatable query".
Problem is that saved select query result not contain primary key.
UPDATE [table] INNER JOIN
[saved_select_query]
ON [table].id_field = [saved_select_query].[my_field]
SET [table].[target_field] = [saved_select_query]![source_field]);
Im also try with select subquery instead of inner join, but same error.
Perhaps a DLookUp() will do the trick:
UPDATE [table] SET
[target_field] = DLookUp("source_field", "saved_select_query", "my_field=" & id_field)
... or, if the joined field is text ...
UPDATE [table] SET
[target_field] = DLookUp("source_field", "saved_select_query", "my_field='" & id_field & "'")
I'm not sure I completely understand what you are asking.
If you are asking what syntax to use when performing an update with an inner join.
UPDATE tableAlias
SET Column = Value
FROM table1 as tableAlias
INNER JOIN table2 as table2Alias on tableAlias.key = table2Alias.key
your query is incorrect , try this.
UPDATE [table]
SET [table].[target_field] = [table2].
[source_field])
from (select *from
[table] INNER JOIN
[saved_select_query] a
ON [table].id_field =
a.[my_field] )
table2
I got it to work using the following:
UPDATE [table]
SET [table].[target_Field] = (SELECT [source_field] FROM [Saved_Select_Query]
WHERE [table].[id_field] = [Saved_Select_Query].[my_field])
You can't use an JOIN on an UPDATE statement directly, so you need to join the tables in a subquery.

Sqlite Update query using a subquery

I have to update table test_test column "testconsent_id" with the id value of table test_groupedconsent, where the patient_id in test_test and patient_id in test_groupedconsent table match and
also creation_date in both table match.
I'm using the below query but getting error -- "near "as": syntax error".
what is wrong with the query?
Update test_test as Tinner join (select id,patient_id,creation_date from test_groupedconsent) as Aon A.patient_id = T.patient_id and A.creation_date = T.creation_dateset T.testconsent_id = A.id;
You cannot use a join directly in an UPDATE statement.
You have to use a correlated subquery to look up the desired value (in the subquery, you can do whatever you want, but in this case, you don't even need a join):
UPDATE test_test
SET testconsent_id = (SELECT id
FROM test_groupedconsent
WHERE patient_id = test_test.patient_id
AND creation_date = test_test.creation_date);
sounds like it took the 'as' after join for the joined tables so either put as in the (... as ...) or bring "ON COMMAND" before as!
like this -> (table1 join table2 on table1.fiel = table2.field) as something
UPDATE TABLE test_test
SET testconsent_id =
(SELECT testconsent_id FROM test_groupedconsent AS A, test_test AS B
WHERE A.patient_id = B.patient_id AND A.creation_date = B.A.creation_date)

Error with the sum function in sql with Access

I am trying to sum the total price from invoices (named Total_TTC in table FACT) depending on the code of the taker ( named N_PRENEUR in the two concerned tables) and store the result in the DEBIT_P column of the table table_preneur.
Doing so i get a syntax error (missing operator) In access and can't seem to understand why. I tried other posts and the usggestions returned me the same error.
UPDATE P
SET DEBIT_P = t.somePrice
FROM table_preneur AS P INNER JOIN
(
SELECT
N_PRENEUR,
SUM(Total_TTC) somePrice
FROM
FACT
GROUP BY N_PRENEUR
) t
ON t.N_PRENEUR = p.N_PRENEUR
thx in advance
with cte as
(select t.somePrice
from table_preneur as P
inner join (select SUM(Total_TTC) as somePrice
from FACT
group by N_PRENEUR) t
on t.N_PRENEUR = p.N_PRENEUR)
update P
set DEBIT_P = cte.somePrice
-- DO YOU NEED A WHERE CLAUSE?
--or maybe
update table_preneur
set DEBIT_P = (select t.somePrice
from table_preneur as P
inner join (select SUM(Total_TTC) as somePrice
from FACT
group by N_PRENEUR) t
on t.N_PRENEUR = p.N_PRENEUR)
You're missing the as keyword before your column alias somePrice:
UPDATE P
SET DEBIT_P = t.somePrice
FROM table_preneur AS P INNER JOIN
(
SELECT
N_PRENEUR,
SUM(Total_TTC) as somePrice
FROM
FACT
GROUP BY N_PRENEUR
) t
ON t.N_PRENEUR = p.N_PRENEUR
Just to clarify the above, in MS Access SQL, you need to use AS when declaring a table alias - this is missing from your SQL (SUM(Total_TTC) AS somePrice)