Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    printsol is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2021
    Posts
    33

    calculating multiple columns

    hi can any body help me
    i have four tables as shown in image
    i want to compare marks in four tables and want to get best 3 marks for every student

    thanks
    Attached Thumbnails Attached Thumbnails best3 marks.jpg  

  2. #2
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Your design is wrong I'm afraid, have one table with a Semester identifier, not 4 tables.
    Then your query becomes very simple.

    Fields : SemesterNo , StudentName (or Student_ID), Marks
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    printsol is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2021
    Posts
    33
    now its better?
    if yes then how now?
    Attached Thumbnails Attached Thumbnails student table.jpg  

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    No, still not right.
    Have a field that holds semester number.

    ID
    SemesterNum
    StudentName
    StudentMark
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    printsol is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2021
    Posts
    33
    i think now i m close
    Attached Thumbnails Attached Thumbnails student table.jpg  

  6. #6
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Yes - That's better.
    Now you can start to get your query writing head on. Press the totals button in the query designer, and start pulling in some fields.
    This won't be a simple query - but would have been really difficult(impossible) with your original layout.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    printsol is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2021
    Posts
    33
    can u please write sql query ????

  8. #8
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I could, but you'll learn more trying to fathom it out for yourself.
    I'll give you a hint - group by Student, and select max for the mark. That should get you started.

    Like I said this isn't actually a simple query.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  9. #9
    printsol is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2021
    Posts
    33
    ok let me try, hope i will get something

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    IMO this isn't a simple Totals query because it will require a subquery. I started to post that I could spend 2 hours on it and not get it because subqueries are my nemesis. In fact, had been playing around for about that long when I had that message composed. Decided to try one more thing and BANG! I got it. Because that table should really be a junction table I constructed this (studentID would be the autonumber from tblStudents, not the student name). I guess you would have to swap StudentId in the query for StudentName (should not have spaces or special characters in object names):

    StudentMarkID StudentID StudentName Semester Mark
    1 1 Joe 1 20
    2 2 John 1 15
    3 3 Mary 1 25
    4 4 Ellen 1 30
    5 1 Joe 2 26
    6 2 John 2 31
    7 3 Mary 2 35
    8 4 Ellen 2 19
    9 1 Joe 3 27
    10 2 John 3 32
    11 3 Mary 3 40
    12 4 Ellen 3 16
    13 1 joe 4 44
    14 2 John 4 45
    15 3 Mary 4 46
    16 4 Ellen 4 47

    This seems to work
    Code:
    SELECT tbl1.StudentID, tbl1.Mark 
    FROM tblStudentMarks AS tbl1 
    WHERE tbl1.mark IN 
    (SELECT TOP 3 Mark FROM tblStudentMarks AS tbl2 WHERE tbl2.StudentID = tbl1.StudentID ORDER BY tbl2.Mark DESC)
     ORDER BY StudentID;
    RESULT
    StudentID Mark
    1 44
    1 27
    1 26
    2 45
    2 32
    2 31
    3 46
    3 40
    3 35
    4 47
    4 19
    4 30


    EDIT - Just noticed that #4 isn't sorted. To achieve that it seems the outer query requires a sort on Mark as well as the inner query. Change from ORDER BY StudentID to
    ORDER BY StudentiD, Mark DESC;
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    Nice, I was trying Top 3 in main query, and student in subquery.��
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I did say it wasn't a simple query

    To be fair I thought after I posted about the totals query and half figured it wasn't the right direction to go.
    Kudos to Micron for solving it.

    I used to be appalling at sub queries until I started using SQL server and SSMS.
    It's so simple to write a query, nest it in another one, and then nest another one you can write some nonsense complex stuff without really trying.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Original data structure would have required a UNION query to manipulate 4 tables into normalized structure, further complicating the process. The key word here is NORMALIZATION.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    printsol is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2021
    Posts
    33
    Quote Originally Posted by printsol View Post
    ok let me try, hope i will get something
    Thank u very much , u make my day

  15. #15
    printsol is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2021
    Posts
    33
    I think in union all tables must have same fields name????

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 12-24-2019, 10:46 AM
  2. Replies: 2
    Last Post: 05-21-2017, 05:40 PM
  3. Replies: 2
    Last Post: 05-15-2014, 06:15 AM
  4. Calculating columns in crosstab query
    By GraemeG in forum Queries
    Replies: 11
    Last Post: 06-05-2011, 06:43 PM
  5. Calculating the Sum of Mulitple Columns
    By ARickert in forum Queries
    Replies: 22
    Last Post: 12-27-2010, 09:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums