[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Access 2007 Query to show all master records even when no activity

Posted on 2014-08-20
8
Medium Priority
?
257 Views
Last Modified: 2014-08-26
I have a basic query with 2 tables.

Table A
Name | Description

Table B
Name | Date | Quantity

I need a list of all descriptions in Table A with a Total Quantity from Table B

What I get is a list of all records from Table A that have activity in Table B, but I need to see ALL records in Table A and just assign a 0 to the ones that don't have anything in table B.

I have a right join on Name, so that's not the issue, any ideas how I can get all records from Table A?
0
Comment
Question by:wlreimer
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Expert Comment

by:DoveTails
ID: 40274818
Something like this ?
SELECT TableA.Description,
       TableA.Name2,
       Sum(TableB.Quantity) AS SumOfQuantity
  FROM TableA
  LEFT JOIN TableB
    ON TableA.Name2 = TableB.Name2
 GROUP BY TableA.Description, TableA.Name2;

Open in new window


note: "name" is a reserved word, so using "name2".
0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 40274857
I made several queries -- a Totals query to add up the quantities, one showing clients with purchases, and one showing those without purchases, and a union query to join them.

BTW, if you really have fields called Name and Date, that is not a good idea, since they are reserved names.  Also, it is best to join on a unique ID, such as an AutoNumber ClientID field (but I used ClientName in case you have to work with that field).

Here is a screen shot of the queries:
QueriesQueries.accdb
0
 

Author Comment

by:wlreimer
ID: 40274863
I wish it was that easy.  I have done exactly that and I still just get the records with activity.  Any other ideas?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Accepted Solution

by:
Helen Feddema earned 2000 total points
ID: 40274866
Here is the union query showing the final results:
Final results
0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 40274868
Did you try mine?  It is more complicated, but does include the clients with no purchases.
0
 
LVL 5

Expert Comment

by:DoveTails
ID: 40274929
Huh?.... seems the query shows all values from Table A in my example.  I must be misunderstanding something.
Couple things to consider ... are there any 'rules' for data on your tables ?
Maybe the IIF function can be useful;
    iif ( condition, value_if_true, value_if_false )

I'll attach the test db if you care to look at the results I'm seeing ... you have me curious.

I see where Helen is going with the Union query ... wondering if a straightforward join will work.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40275611
It is much simpler. Just use Nz() and one query:

SELECT
    [TableA].[Name],
    Nz(Sum([TableB]![Quantity]), 0) AS TotalQuantity
FROM
    TableA
LEFT JOIN
    TableB ON
        [TableA].[Name] = [TableB].[Name]
GROUP BY
    [TableA].[Name];

/gustav
0
 

Author Closing Comment

by:wlreimer
ID: 40286185
Thanks for the help everyone.  The Nz didn't work and just adjusting the join wouldn't do it.

I ended up doing the union query to make it work.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question