473,385 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Update Statement Syntax error

I have been struggling with this update statement. At this point I get a syntax error if i go from SQL view to design view. Can anyone spot a problem?

UPDATE teacherresources AS A
SET A.student_enroll = student_enroll / SELECT sum(enroll_data) FROM enrollments B WHERE A.trSchool_id=b.er_School_id and b.year='2007-2008') SELECT sum(enroll_data) FROM enrollments WHERE a.trSchool_id=b.er_School_id and enrollments.year='2008 -2009')
WHERE (((a.trschool_id)=12) And ((a.tryear)='2009')) Or (((a.tryear)='2009') And ((a.trschool_id)=13));
Mar 28 '08 #1
6 2543
This looks like SQL server syntax to me, not Access SQL. I'm not sure if Access can handle all the alias references. Also, there is a whack "/" at the end of your first line, which wouldn't work in Access.
Mar 28 '08 #2
Stewart Ross
2,545 Expert Mod 2GB
Hi Patti. It isn't valid SQL syntax to use the two sequential SELECTs as part of your calculation. The only occasion when you can use a select within a select is when you are referring to a subquery, where the general syntax is along the lines of
Expand|Select|Wrap|Line Numbers
  1. SELECT <list of fields> FROM <some table> 
  2. WHERE <one of the fields> IN
  3. (SELECT <a matching field> FROM
  4. <another table>);
You may wish to join the tables involved directly onto the tables of this query, or create other summary queries which you can then join directly or to create a subquery for the Update. If you could tell us a bit more about what it is you are updating and what is feeding into the update I am sure that we could assist you with a solution.

-Stewart
Mar 28 '08 #3
Thanks for the reply. Here is what I am trying to do.

TeacherFTE num
trSchool_id num
grade_subjectid num
tryear text
strudent_enroll num
displayorder text
the primary key is trschool_id + grade_subjectid + tryear

Enrollment
Enroll_data num
Year text
Er_school_id num

These tables are not directly related. The strudent_enroll counts students in a given class which may or may not add up to the school’s total enrollment. Here is what I am attempting to do. The application has two years of data available for each course given at each school. I am working with strudent_enroll for year 2007-2008. I am trying to apply a formula that will take the strudent_enroll for all grade_subject_id entries for tr_school_id # 12 and # 13 which are high schools and update the same records for the 2008-2009 year (strudent_enroll) applying this formula. Strudent_enroll / Total school enrollment (which is the sum of all records in the enrollment table for that year and school). This number is multiplied against the total school enrollment for 2008-2009 (obtained the same way) The resulting number is updated into table teacherresources.

Simple example

Teacherresource table
Math strudent_enroll tryear grade_subjectid trschool_id
1353 2007-2008 177 13

The total enrollment for year 2007-2008 is 1756
The total enrollment for year 2008-2009 is 1662

1353/1756* 1662 = 1280

1280 Is the estimated enrollment for year 2008-2009 and needs to be updated to the teacherresource table for trschool_id 12 and 13 only.

Teacherresource table
Math strudent_enroll tryear grade_subjectid trschool_id
1280 2008-2009 177 13


Thanks for pointing me in the right direction.

Patti
Mar 28 '08 #4
Stewart Ross
2,545 Expert Mod 2GB
Hi Patti. The solution I propose relies on several queries, not one. I prefer to break problems down into smaller segments, each of which can be tested and verified as working correctly before moving to the next.

The solution to what you have asked for works as follows:

1. calculate the total enrolments for all schools in the enrollment table by year
2. extract from the total enrolments the current and next year's enrolment totals together
3. compute the projected total enrolments for all schools using the formula
projected total = school total / current year total * next year total
4. Append the projected totals to the Teacherresource table for schools 12 and 13 only.

Note that I am assuming there is no entry for 2008-2009 in the table, and am appending a new row for that year and not updating an existing row. The projected totals query cannot itself be used to update the teacher resources table - the combination does not result in an updatable query. If you do need to update the rows the projected totals query should become a make table query creating a temporary table which in turn can be joined to teacherresources for the update. I can advise you further on this if you are not able to use the append version listed.

Although I have prepared a working solution I would suggest that if there are only two schools to update it would have been much simpler to do the calculations by hand (as you had to do for one of the two schools in preparing test data) and append the data manually...

Anyway, I append the queries in order below. You need to store them under the names given. It is the last of these which you run to append the projected totals for 2008-2009 to the teacherresources table.
I have had to make some assumptions about the correct field names to use, as these have valried in your posts (student_enroll vs strudent_enroll, for example).

Expand|Select|Wrap|Line Numbers
  1. qryTotalEnrolments
  2. SELECT Sum(Enrollment.Enroll_Data) AS [Total Enrolments], Enrollment.Year
  3. FROM Enrollment
  4. GROUP BY Enrollment.Year;
  5.  
  6. qryYearTotals
  7. SELECT Current.[Total Enrolments] AS [Curr Total Enr], Next.[Total Enrolments] AS [Next Total Enr], Current.Year AS [Current Year], Next.Year AS [Next Year]
  8. FROM qryTotalEnrolments AS [Current], qryTotalEnrolments AS [Next]
  9. WHERE (((Val(Left([Next].[Year],4))=Val(Left([Current].[Year],4))+1)=True));
  10.  
  11. qryProjectedTotals
  12. SELECT teacherresource.student_enroll, qryYearTotals.[Curr Total Enr], qryYearTotals.[Next Total Enr], qryYearTotals.[Next Year], teacherresource.trschool_id, Int([Student_Enroll]/[Curr Total Enr]*[Next Total Enr]) AS Projected
  13. FROM teacherresource INNER JOIN qryYearTotals ON teacherresource.tryear = qryYearTotals.[Current Year];
  14.  
  15. qryAppendProjected
  16. INSERT INTO teacherresource ( tryear, trschool_id, student_enroll )
  17. SELECT qryProjectedTotals.[Next Year], qryProjectedTotals.trschool_id, qryProjectedTotals.Projected
  18. FROM qryProjectedTotals
  19. WHERE (((qryProjectedTotals.trschool_id)=12 Or (qryProjectedTotals.trschool_id)=13));
Sample data:
Expand|Select|Wrap|Line Numbers
  1. Total Enrolments  Year
  2. 1756              2007-2008
  3. 1662              2008-2009
  4.  
  5. Curr Total Enr  Next Total Enr  Current Year  Next Year
  6. 1756            1662            2007-2008     2008-2009
  7.  
  8. student_enroll  Curr Total Enr  Next Total Enr  Next Year  trschool_id  Projected
  9. 1353            1756            1662            2008-2009  13           1280
  10. 100             1756            1662            2008-2009  12             94
  11. 10              1756            1662            2008-2009  10              9
  12. 10              1756            1662            2008-2009  10              9
  13.  
  14. Next Year  trschool_id  Projected
  15. 2008-2009  13           1280
  16. 2008-2009  12             94
-Stewart
Mar 31 '08 #5
Stewart:

Thank you!!! THat worked very well. I made a few adjustments to suit my design. Thank you for for your time and effort. I certainly appreciate the effort.

Patti
Apr 2 '08 #6
NeoPa
32,556 Expert Mod 16PB
Patti,

I'm glad you've got another problem solved :)

Can I just ask that you pay careful attention to use of the [ CODE ] tags in future though. They do make life easier for members trying to help, as well as others who may want to learn the same lessons.

-NeoPa (Admin).
Apr 3 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Robert Mark Bram | last post by:
Hi All! I have the following two methods in an asp/jscript page - my problem is that without the update statement there is no error, but with the update statement I get the following error: ...
7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
5
by: S.Patten | last post by:
Hi, I have a problem with updating a datetime column, When I try to change the Column from VB I get "Incorrect syntax near '942'" returned from '942' is the unique key column value ...
8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
3
by: Falco Vermeer | last post by:
Hi, I tried to use the following query to update a value in one table with a value from another table: UPDATE tbl1 SET col1 = tbl2.col2 FROM tbl1, tbl2 WHERE tbl1. = tbl2.
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
6
by: HeadScratcher | last post by:
I am trying to speed up my update statements by removing inner select statements. Example: update orders set shipname = (select contactName from customers where customerid = orders.customerID)...
1
by: amitbadgi | last post by:
HI i am getting the foll error while conv an asp application to asp.net Exception Details: System.Runtime.InteropServices.COMException: Syntax error in UPDATE statement. Source Error: Line...
5
by: Al | last post by:
Hi, I need to update tables in access 97. The table names have spaces (not my choice). My update fails even though I use the OleDbCommandBuilder. Here is a code I am using myDataAdapter = New...
6
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.