Update Statement Syntax error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patriciashoe
    New Member
    • Feb 2008
    • 41

    #1

    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 teacherresource s AS A
    SET A.student_enrol l = 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.yea r='2008 -2009')
    WHERE (((a.trschool_i d)=12) And ((a.tryear)='20 09')) Or (((a.tryear)='2 009') And ((a.trschool_id )=13));
  • Fiddler2
    New Member
    • Mar 2008
    • 19

    #2
    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.

    Comment

    • Stewart Ross
      Recognized Expert Moderator Specialist
      • Feb 2008
      • 2545

      #3
      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
      [code=SQL]SELECT <list of fields> FROM <some table>
      WHERE <one of the fields> IN
      (SELECT <a matching field> FROM
      <another table>);[/code]
      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

      Comment

      • patriciashoe
        New Member
        • Feb 2008
        • 41

        #4
        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_i d entries for tr_school_id # 12 and # 13 which are high schools and update the same records for the 2008-2009 year (strudent_enrol l) 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 teacherresource s.

        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

        Comment

        • Stewart Ross
          Recognized Expert Moderator Specialist
          • Feb 2008
          • 2545

          #5
          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 teacherresource s 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 teacherresource s 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).

          [code=sql]qryTotalEnrolme nts
          SELECT Sum(Enrollment. Enroll_Data) AS [Total Enrolments], Enrollment.Year
          FROM Enrollment
          GROUP BY Enrollment.Year ;

          qryYearTotals
          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]
          FROM qryTotalEnrolme nts AS [Current], qryTotalEnrolme nts AS [Next]
          WHERE (((Val(Left([Next].[Year],4))=Val(Left([Current].[Year],4))+1)=True));

          qryProjectedTot als
          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
          FROM teacherresource INNER JOIN qryYearTotals ON teacherresource .tryear = qryYearTotals.[Current Year];

          qryAppendProjec ted
          INSERT INTO teacherresource ( tryear, trschool_id, student_enroll )
          SELECT qryProjectedTot als.[Next Year], qryProjectedTot als.trschool_id , qryProjectedTot als.Projected
          FROM qryProjectedTot als
          WHERE (((qryProjected Totals.trschool _id)=12 Or (qryProjectedTo tals.trschool_i d)=13));[/code]

          Sample data:
          Code:
          Total Enrolments  Year
          1756			  2007-2008
          1662			  2008-2009
           
          Curr Total Enr  Next Total Enr  Current Year  Next Year
          1756			1662			2007-2008	 2008-2009
           
          student_enroll  Curr Total Enr  Next Total Enr  Next Year  trschool_id  Projected
          1353			1756			1662			2008-2009  13		   1280
          100			 1756			1662			2008-2009  12			 94
          10			  1756			1662			2008-2009  10			  9
          10			  1756			1662			2008-2009  10			  9
           
          Next Year  trschool_id  Projected
          2008-2009  13		   1280
          2008-2009  12			 94
          -Stewart

          Comment

          • patriciashoe
            New Member
            • Feb 2008
            • 41

            #6
            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

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              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).

              Comment

              Working...