Results 1 to 6 of 6
  1. #1
    gangel is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    194

    query with 2 link tables, needing a SUM with 0 for no records!

    Hey everyone,

    So i have a confusing query that is screwed perhaps because i have been looking at it for so long.

    The query is based on info for a program im making for wife.

    She notes what meals she has every day
    The program notes the meals, which contain "serves" of categories

    eg.
    Roast chicken
    1 serve protein
    2 serves vegetables etc etc
    Click image for larger version. 

Name:	Image 002.jpg 
Views:	10 
Size:	49.3 KB 
ID:	26072Click image for larger version. 

Name:	Image 001.jpg 
Views:	10 
Size:	154.0 KB 
ID:	26073
    She then records the meals she ate on each day and the program knows which categories she hasnt had enough of!!!

    The attached pic the query at the moment... which shows how many categories she has on each day, but i need this to say 0 for the categories she hasnt had any of.!!

    I have done this previously with subqueries for a different program, but i think im getting lost with the 2 link tables



    Any help/advice would be great, im totally confusing myself with this!

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    not sure which table it will apply to (perhaps tbCategories?) but you will need a left join to list all the categories - which means all the other joins 'down the chain' will need to be left joins.

    Basically looks like you have just accepted the links as defined by the relationships rather than considered the data you need to display. Although they look the same, links in queries do not need to mirror relationships which are more to do with maintaining data integrity

  3. #3
    gangel is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    194
    Hi Ajax
    I actually tried a few different types and just put the standard result up for the forum.

    This is All left joins:
    Click image for larger version. 

Name:	Image 003.jpg 
Views:	7 
Size:	70.6 KB 
ID:	26078

    i think im at a loss. I might have to add a record for linkcatmeal for every mealid and categoryid and have serves as 0 value... because i just cant get this query to work!

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    which shows how many categories she has on each day, but i need this to say 0 for the categories she hasnt had any of.!!
    OK, we'll try something different

    first, create an Cartesian query (no joins) - call it Qall

    Code:
    SELECT *
    FROM tblDates, tblCategories
    this will show all categories for all dates

    now create a second query to show what has actually been served on each day - call it Qactual

    Code:
    SELECT linkdatemeal.dateID, linkCatMeal.*
    FROM linkdatemeal INNER JOIN linkCatMeal
    now create a third query to link these two queries together

    Code:
    SELECT Qall.recdate, Qall.catname, sum (nz(Qactual.serves,0)) as servings
    FROM Qall LEFT JOIN Qactual ON Qall.DateID=Aactual.dateID and Qall.catID=Aactual.catID
    GROUP BY Qall.recdate, Qall.catname
    The above is freetyped so may contain typos, but give it a try.

  5. #5
    gangel is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    194
    yep typos buit OMG THANK YOU

    tbh im so happy i have learnt how to do this. My brain just couldnt fit around it at all!

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    if you wanted to do it in one query then you would have

    Code:
    SELECT Qall.recdate, Qall.catname, sum (nz(Qactual.serves,0)) as servings
    FROM (SELECT *FROM tblDates, tblCategories) Qall LEFT JOIN (SELECT linkdatemeal.dateID, linkCatMeal.*
    FROM linkdatemeal INNER JOIN linkCatMeal) Qactual ON Qall.DateID=Aactual.dateID and Qall.catID=Aactual.catID
    GROUP BY Qall.recdate, Qall.catname

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

Similar Threads

  1. Trying to link tables for a query
    By brittain12 in forum Access
    Replies: 10
    Last Post: 04-06-2016, 11:41 AM
  2. Replies: 1
    Last Post: 10-14-2015, 03:38 PM
  3. Replies: 1
    Last Post: 03-05-2013, 02:17 PM
  4. Replies: 3
    Last Post: 01-05-2012, 12:04 PM
  5. Multiple Records = needing max row
    By alohawahine77 in forum Queries
    Replies: 1
    Last Post: 12-20-2010, 02:53 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