Results 1 to 2 of 2
  1. #1
    dandoescode is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Dec 2011
    Posts
    85

    Structuring Subqueries

    Written the following master query called abundance, which calculates the abundance of a certain species per year. It relies on two other queries C2 and C1. I would like to know if there is a simple way to write Abundance so that the entirety of the query is contained in just one file, rather than three. I have provided a brief summary of each query followed by the sql code.



    C1, takes data from the tables Species and CatchSummary, and does a search for a given species by name. It then provides a listing of all catches and associated event numbers.




    SELECT Species.SpeciesID, CatchSummary.EventNum, CatchSummary.TotCt
    FROM Species INNER JOIN CatchSummary ON Species.SpeciesID=CatchSummary.SpeciesID
    WHERE (((Species.CommonName)=[Enter CommonName]))
    GROUP BY Species.SpeciesID, CatchSummary.EventNum, CatchSummary.TotCt;


    C2 takes catches for the given species and associates them with a given cruise and station by event number.

    SELECT C1.SpeciesID, C1.EventNum, Station.CruiseID, C1.TotCt
    FROM Station INNER JOIN C1 ON Station.EventNum = C1.EventNum;


    Abundance takes data from C2 and Cruise and sums the catch by year.

    SELECT Cruise.Year, Sum(C2.TotCt) AS [Count]
    FROM C2 INNER JOIN Cruise ON C2.CruiseID = Cruise.CruiseID
    GROUP BY Cruise.Year;


    Thanks for the help.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    You can build nested queries in the SQL View window.

    So where Abundance has FROM C2 put:

    FROM (SELECT C1.SpeciesID, C1.EventNum, Station.CruiseID, C1.TotCt FROM Station INNER JOIN C1 ON Station.EventNum = C1.EventNum) AS C2

    And were query C2 has INNER JOIN C1 put:

    INNER JOIN (the c1 sql without semicolon) AS C1
    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.

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

Similar Threads

  1. Creating subqueries in SQL view
    By AmyM in forum Queries
    Replies: 2
    Last Post: 11-20-2011, 05:21 PM
  2. LEFT/RIGHT/OUTER JOINS in a set of subqueries
    By luckycharms in forum Queries
    Replies: 1
    Last Post: 08-01-2011, 05: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