Results 1 to 6 of 6
  1. #1
    Tajaldeen is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jan 2023
    Posts
    5

    Two SQL statments in one Query


    Hi guys, I put this statement in a Query SQl to retrieve the first item of a Table named Students.
    Code:
    SELECT Min(Name) AS FirstName
    FROM Students
    And I put this one in another query SQL to retrieve the first item of a Query named Substudents

    Code:
    SELECT Min(Name) AS LastName
    FROM Substudents;
    My question is:
    How can I put the two statements in one Query to show the results in the same Query table.
    Thank you very much

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,905
    Use a Union query.
    Names will need to be the same though?, so perhaps add another field to identify which table it came from?

    Otherwise join the tables and take each field from each table, if you want LastName, FirstName per record.
    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

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    I believe using Min() on text will return an alphabetical result, not necessarily the "First" record.

    What is your ultimate goal?
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,905
    Quote Originally Posted by moke123 View Post
    I believe using Min() on text will return an alphabetical result, not necessarily the "First" record.

    What is your ultimate goal?
    Fair point, you could/would get LastName for 1 student and the FirstName for another completely different student?
    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
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,041
    Maybe explain what you are trying to do, and what's the structure of table students? What is the SQL of query Substudents, what table is it based on?

    If you want unique names and query substudents is based on the same table Students:
    Code:
    select distinct Name, FirstName from Students
    might do the trick

  6. #6
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    132

    ?

    Quote Originally Posted by Tajaldeen View Post

    My question is:
    How can I put the two statements in one Query to show the results in the same Query table.
    Thank you very much
    SELECT TOP 1 FirstName, lastname
    FROM MyTable
    ORDER BY FirstName, LastName

    will return the first record in the table as determined by the sort order (Firstname, lastname).

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Iterate thru Listbox for INSERT SQL Statments
    By dccjr3927 in forum Programming
    Replies: 17
    Last Post: 06-12-2020, 09:15 AM
  2. Problems with subreports and IIF statments
    By ByronC in forum Reports
    Replies: 4
    Last Post: 12-15-2016, 02:42 PM
  3. randomizing statments on report
    By BatmanMR287 in forum Reports
    Replies: 2
    Last Post: 07-12-2015, 01:34 PM
  4. If statments??!!
    By wmagargle in forum Programming
    Replies: 4
    Last Post: 08-30-2012, 02:45 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