I am having a hard time using MS Access because the syntax is a little finicky compared to other db's.
I am trying to validate a table and compare that table to a master table with multiple columns of information. At the moment I am trying to update a table with a field name of Difference_Value in table ct2011 to be equal to (ct2011.Distribution_Amount - AggregateFinal.SumOfDollars).
Also specifying the lines in which are going to be updated because not all rows in the MASTER are in the table ct2011.
Below is my query.
UPDATE ct2011
SET ct2011.Difference_Value = (ct2011.Distribution_Amount - AggregateFinal.SumOfDollars)
FROM
ct2011 as ct
INNER JOIN
AggregateFinal af
ON
ct.Employee_ID = af.EmpId AND ct.Legal_Name = af.LegalName AND ct.Distribution_Plan_Year = af.CalculationAwardPeriod AND ct.Award_Year = af.AwardPeriod;
I am getting a Syntax error (missing operator). It specifies that it is encountering the error during the SET expressions after the =.
In an MS Access update query, the join criteria should follow the update keyword, e.g.:
update
ct2011 ct inner join aggregatefinal af on
ct.employee_id = af.empid and
ct.legal_name = af.legalname and
ct.distribution_plan_year = af.calculationawardperiod and
ct.award_year = af.awardperiod
set
ct.difference_value = (ct.distribution_amount - af.sumofdollars)
Related
With 20+ years of experience with MS Access and SQL Server, I'm not a novice with respect to SQL, but I am new to PostgreSQL and I have encountered an issue that makes me feel like a complete noob. I have a simple UPDATE query in which I want to update the destination table d with data from the source View m:
UPDATE chgman.tc_data
SET reporttime = m.reporttime, endtime = m.endtime,
itismessage = m.itismessage, shortdesc = m.shortdesc,
longdesc = m.longdesc, severity = m.severity,
tc_source = m.tc_source, tc_state = m.tc_state,
ushr_state = m.ushr_state, mainroad = m.mainroad,
start_location = m.start_location, end_location = m.end_location
FROM
chgman.tc_matched_raw AS m
INNER JOIN
chgman.tc_data AS d ON d.tc_id = m.tc_id;
The result of the query is that EVERY row in table d is populated with data from the FIRST row of View m.
I am prepared for the embarrassment - please enlighten me as to what I have done wrong...
The from/update in Postgres works a bit differently from SQL Server/MS Access.
This should do what you want:
UPDATE chgman.tc_data d
SET reporttime = m.reporttime, . . .
FROM chgman.tc_matched_raw m
WHERE d.tc_id = m.tc_id;
You don't repeat the table in the FROM clause -- that is a new reference to the table.
This SQL is beyond my expertise. I think it should be fairly easy for someone with experience. Here is what I have so far..
SQL is as follows:
UPDATE (Tbl_Stg_Project_Schedule_Dates
INNER JOIN Tbl_Child_ITN ON Tbl_Stg_Project_Schedule_Dates.ms_itn = Tbl_Child_ITN.ITN)
INNER JOIN Tbl_Schedule ON Tbl_Child_ITN.Id = Tbl_Schedule.ID SET Tbl_Schedule.a_construction_start = [Tbl_Stg_Project_Schedule_Dates].[ms_start_date]
WHERE (((Tbl_Stg_Project_Schedule_Dates.ms_tempt_id) In (16,17,18,19,20,21,22,23)));
I want to add one last condition to this being that I only want the minimum of [Tbl_Stg_Project_Schedule_Dates].[ms_start_date] to update the table. I've tried the obvious of wrapping the field in Min, and also tried creating a separate aggregate select statement first (to get the min value with other criteria) that I then tried to create the update query from in new query but no luck.
I believe this is valid Access/Jet SQL. The idea here is to use a subquery to look up the earliest date among all the rows in your subset. I'm not sure if ms_itn was the right column to correlate on but hopefully you get the idea:
UPDATE (Tbl_Stg_Project_Schedule_Dates
INNER JOIN Tbl_Child_ITN ON Tbl_Stg_Project_Schedule_Dates.ms_itn = Tbl_Child_ITN.ITN)
INNER JOIN Tbl_Schedule ON Tbl_Child_ITN.Id = Tbl_Schedule.ID
SET Tbl_Schedule.a_construction_start = [Tbl_Stg_Project_Schedule_Dates].[ms_start_date]
WHERE (((Tbl_Stg_Project_Schedule_Dates.ms_tempt_id) In (16,17,18,19,20,21,22,23)))
and [Tbl_Stg_Project_Schedule_Dates].[ms_start_date] = (
select min(sd.[ms_start_date])
from [Tbl_Stg_Project_Schedule_Dates] as sd
where sd.ms_itn = [Tbl_Stg_Project_Schedule_Dates].ms_itn
)
trying to update a single column based on criteria. The rows that need to be updated are returned by the query
SELECT
it.objectid,
it.versionnumber,
it.itemnumber "Item Number",
it.itemtype,
itcovprem.basisofsettlement,
itcovprem.coverage "Coverage"
FROM
itemcoveragesandpremiums itcovprem,
items it,
policies pl
WHERE
pl.objectid = it.parentobjectid AND
pl.policytype in ('H','F') AND
it.objectid = itcovprem.itemobjectid AND
it.itemtype in ('SHOA','SHOB','SHOC','SHOD','SHOK','SHOL') and
it.versionnumber = itcovprem.itemversionnumber AND
((itcovprem.coverage like ('A - Dwg Bldg%')) or
(itcovprem.coverage like ('#42 - Repl Cost Plus%')) or
(itcovprem.coverage like ('#42 - Limited GRC%'))) and
it.effectivedate >= TO_DATE('01-JAN-2006', 'DD-MON-YYYY') and
exists (select * from itemcoveragesandpremiums icp where icp.itemobjectid = it.objectid and icp.itemversionnumber = it.versionnumber and icp.coverage like ('#42%')) and
itcovprem.basisofsettlement not in ('LGRC')
I've tried a few options to convert this to an update query, but no joy.
I get Error - line 3 - SQL command not properly ended when using
update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
from itemcoveragesandpremiums as itcovprem
inner join items as it
on it.objectid = itcovprem.itemobjectid and it.versionnumber = itcovprem.itemversionnumber
inner join policies as pl
on pl.objectid = it.parentobjectid
where [cut, rest of query was below]
I get Error - line 6 - missing right parenthesis when trying use an inline query
update
(
SELECT
itcovprem.basisofsettlement as OLD
FROM
itemcoveragesandpremiums as itcovprem inner join items as it on it.objectid = itcovprem.itemobjectid and it.versionnumber = itcovprem.itemversionnumber inner join policies AS pl on pl.objectid = it.parentobjectid
WHERE [query snipped]
) t
set t.old = 'LGRC'
Seems that SQL*Plus just wants to stop looking at the update before it gets to the meat of my select query. I'm not sure if I'm formatting it incorrectly or going at it from the wrong direction. Any advice?
In your first attempt, the error at line 3 is because update doesn't support a from or join clause. In your second attempt the immediate error at line 6 is because you're trying to alias a table with as itcovprem; but you can only use as for column aliases, not for table aliases. (The first attempt is trying to do that too, but it isn't getting as far as encountering that problem). But you can't generally update an inline-view with joins anyway, so it would still error with that - something like an ORA-01779.
You would need to do something like:
update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
where (itemobjectid, itemversionnumber, basisofsettlement, coverage) in (
select it.objectid,
it.versionnumber,
itcovprem.basisofsettlement,
itcovprem.coverage
from ...
)
Assuming that itemobjectid, itemversionnumber, basisofsettlement, coverage identifies a row sufficiently such that you don't risk updating anything you shouldn't. It might be safer to add a rowid to the select and use that for the update instead to avoid ambiguity:
update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
where rowid in (
select itcovprem.rowid as target_rowid
from ...
)
The script below returns the 01427 error that the single row sub-query returns more than one row. The rownum<2 gets a few rows updated. The obvious solution is looping through with pl/sql, but I am trying to determine if there is a SQL only solution.
UPDATE ldl.clens le
SET master_song_id =
(SELECT cf.song_id#
FROM lt.master_songs cf
WHERE le.lot_id = cf.lot_id
AND song_id#_fk =
(SELECT msc_songs.song_id#
FROM lt.msc_songs
WHERE msc_songs.song_name = le.song_name)
---- AND ROWNUM < 2
);
Any and all help and suggestions deeply appreciated!
MD
I'm not sure I grasped the relation between the tables, but if I did, you can use below UPDATE:
UPDATE ldl.clens le
SET master_song_id =
(SELECT cf.song_id#
FROM lt.master_songs cf
JOIN lt.msc_songs ms ON (cf.song_id#_fk = ms.song_id#)
WHERE
ms.song_name = le.song_name
AND le.lot_id = cf.lot_id)
;
It will work if msc_songs.song_name and master_songs.lot_id will give you a unique master_songs.song_id#.
I get multipart can not be bound error on following query
update nfltx
set
b.boxno = a.boxno,
b.message = a.message,
b.nameboxno = a.nameboxno,
b.namemsg = a.namemsg,
b.phoneboxno = a.phoneboxno,
b.phonemsg = a.phonemsg
FROM ofltx a JOIN nfltx b
ON a.ls_fullnam = b.ls_fullnam
but if i remove b from boxno message and all i do not get the error . What is the reason behind this. Thank You using sql server 2008
A table alias specified in a FROM clause cannot be used as a qualifier in SET column_name. This is not valid:
update nfltx
set
b.boxno = a.boxno,
b.message = a.message,
b.nameboxno = a.nameboxno,
b.namemsg = a.namemsg,
b.phoneboxno = a.phoneboxno,
b.phonemsg = a.phonemsg
FROM ofltx a JOIN nfltx b
ON a.ls_fullnam = b.ls_fullnam
To make it work, remove the b. alias from the column name.
update nfltx
set
boxno = a.boxno,
message = a.message,
nameboxno = a.nameboxno,
namemsg = a.namemsg,
phoneboxno = a.phoneboxno,
phonemsg = a.phonemsg
FROM ofltx a JOIN nfltx b
ON a.ls_fullnam = b.ls_fullnam
Raj
What is the reason behind this?
An UPDATE (and DELETE, INSERT) can affect one, and only one, table. You've already identified which table you want to affect here:
update nfltx
Therefore, it doesn't make sense to allow an alias for the left hand side of assignments in the SET clause. They must be columns belonging to the previously identified table.
If the same table is included in the FROM clause multiple times (and it's the table you wish to update), you would need to provide an alias to indicate which instance of the table is to be updated - but you'd provide it (once) in the UPDATE clause rather than in the SET clause.
just use
update b
instead of
update nfltx
Man you guys make things too difficult for those that are learning.