473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Display results from 2 tables including empty values

vavc1980
Hi!
I have a problem with a query, I need to put together into one table the contents of 2 tables, but in the correspondent row for each match, and include the no-matches too.

Table A:
User | IncomingCalls|
---------|-----------------------|
smith | ___10_______ |
jones | ____5_______ |
lee __| ____8 _______ |
harp _| ____2 _______ |

Table B:
User | OutgoingCalls|
---------|-----------------------|
taylor | _____15 _____ |
jones | ______6______ |
lee __| ______1 ______ |

And the result table or view that I want is:

Results Table:
User | IncomingCalls | OutgoingCalls|
---------|---------------------|----------------------|
smith | ____10_____ | ____________|
jones | _____5_____ | ______6_____ |
lee __| _____8_____ | ______1_____ |
harp _| _____2_____ | ____________|
taylor | ___________ | ______15____ |

How can I accomplish this?
I appreciate the help..
Feb 26 '08 #1
6 1452
ADezii
8,834 Expert 8TB
Hi!
I have a problem with a query, I need to put together into one table the contents of 2 tables, but in the correspondent row for each match, and include the no-matches too.

Table A:
User | IncomingCalls|
---------|-----------------------|
smith | ___10_______ |
jones | ____5_______ |
lee __| ____8 _______ |
harp _| ____2 _______ |

Table B:
User | OutgoingCalls|
---------|-----------------------|
taylor | _____15 _____ |
jones | ______6______ |
lee __| ______1 ______ |

And the result table or view that I want is:

Results Table:
User | IncomingCalls | OutgoingCalls|
---------|---------------------|----------------------|
smith | ____10_____ | ____________|
jones | _____5_____ | ______6_____ |
lee __| _____8_____ | ______1_____ |
harp _| _____2_____ | ____________|
taylor | ___________ | ______15____ |

How can I accomplish this?
I appreciate the help..
  1. Create a Table named tblResults with the following Fields
    1. User [TEXT]
    2. IncomingCalls [LONG]
    3. OutGoingCalls [LONG]
  2. I'm assuming User Names are Unique and spelled correctly if they exist in both the Incoming and OutGoing Calls Tables.
  3. I've made the Test Database for this Thread available to you, it is a lot easier then trying to figure out what is going on.
  4. Do not rely 100% on this solution, the SQL guys will probably have a better one!
  5. The entire code block is listed below, but, again download the Attachment:
    Expand|Select|Wrap|Line Numbers
    1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset, strSQL As String
    2. Dim rstResults As DAO.Recordset
    3.  
    4. 'DELETE any existing Records from tblResults
    5. DoCmd.SetWarnings False
    6.   DoCmd.RunSQL "Delete * From tblResults;"
    7.  
    8. 'Add [User] and [IncomingCalls] to tblResults
    9. strSQL = "INSERT INTO tblResults ( [User], IncomingCalls ) SELECT [tblIncomingCalls].[User], " & _
    10.          "[tblIncomingCalls].[IncomingCalls] FROM tblIncomingCalls;"
    11. DoCmd.RunSQL strSQL
    12.  
    13. DoCmd.SetWarnings True
    14.  
    15. 'Create a Recordset based on tblOutgoingCalls
    16. Set MyDB = CurrentDb()
    17. Set MyRS = MyDB.OpenRecordset("tblOutGoingCalls", dbOpenForwardOnly)
    18.  
    19. 'Create a Recordset based on tblResults
    20. Set rstResults = MyDB.OpenRecordset("tblResults", dbOpenDynaset)
    21.  
    22. Do While Not MyRS.EOF
    23.   'Does the User already exist? If not then Add to tblResults, but if he/she does exist
    24.   'then Update tblResults with the OutGoingCalls values
    25.   If DCount("*", "tblResults", "[User] = '" & MyRS![User] & "'") = 0 Then
    26.     rstResults.AddNew
    27.       rstResults![User] = MyRS![User]
    28.       rstResults![OutGoingCalls] = MyRS![OutGoingCalls]
    29.     rstResults.Update
    30.   Else
    31.     'Find the right User
    32.     rstResults.FindFirst "[User] = '" & MyRS![User] & "'"
    33.     rstResults.Edit
    34.       rstResults![OutGoingCalls] = MyRS![OutGoingCalls]
    35.     rstResults.Update
    36.   End If
    37.   MyRS.MoveNext
    38. Loop
    39.  
    40. MyRS.Close: Set MyRS = Nothing
    41. rstResults.Close: Set rstResults = Nothing
Feb 26 '08 #2
  1. Create a Table named tblResults with the following Fields
    1. User [TEXT]
    2. IncomingCalls [LONG]
    3. OutGoingCalls [LONG]
  2. I'm assuming User Names are Unique and spelled correctly if they exist in both the Incoming and OutGoing Calls Tables.
  3. I've made the Test Database for this Thread available to you, it is a lot easier then trying to figure out what is going on.
  4. Do not rely 100% on this solution, the SQL guys will probably have a better one!
  5. The entire code block is listed below, but, again download the Attachment:
    Expand|Select|Wrap|Line Numbers
    1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset, strSQL As String
    2. Dim rstResults As DAO.Recordset
    3.  
    4. 'DELETE any existing Records from tblResults
    5. DoCmd.SetWarnings False
    6.   DoCmd.RunSQL "Delete * From tblResults;"
    7.  
    8. 'Add [User] and [IncomingCalls] to tblResults
    9. strSQL = "INSERT INTO tblResults ( [User], IncomingCalls ) SELECT [tblIncomingCalls].[User], " & _
    10.          "[tblIncomingCalls].[IncomingCalls] FROM tblIncomingCalls;"
    11. DoCmd.RunSQL strSQL
    12.  
    13. DoCmd.SetWarnings True
    14.  
    15. 'Create a Recordset based on tblOutgoingCalls
    16. Set MyDB = CurrentDb()
    17. Set MyRS = MyDB.OpenRecordset("tblOutGoingCalls", dbOpenForwardOnly)
    18.  
    19. 'Create a Recordset based on tblResults
    20. Set rstResults = MyDB.OpenRecordset("tblResults", dbOpenDynaset)
    21.  
    22. Do While Not MyRS.EOF
    23.   'Does the User already exist? If not then Add to tblResults, but if he/she does exist
    24.   'then Update tblResults with the OutGoingCalls values
    25.   If DCount("*", "tblResults", "[User] = '" & MyRS![User] & "'") = 0 Then
    26.     rstResults.AddNew
    27.       rstResults![User] = MyRS![User]
    28.       rstResults![OutGoingCalls] = MyRS![OutGoingCalls]
    29.     rstResults.Update
    30.   Else
    31.     'Find the right User
    32.     rstResults.FindFirst "[User] = '" & MyRS![User] & "'"
    33.     rstResults.Edit
    34.       rstResults![OutGoingCalls] = MyRS![OutGoingCalls]
    35.     rstResults.Update
    36.   End If
    37.   MyRS.MoveNext
    38. Loop
    39.  
    40. MyRS.Close: Set MyRS = Nothing
    41. rstResults.Close: Set rstResults = Nothing
Thanks! that worked!
Feb 26 '08 #3
ADezii
8,834 Expert 8TB
Thanks! that worked!
You are quite welcome but like I previously stated, there may be a better, SQL based approach, so I would periodically check back. Take care and good luck.
Feb 27 '08 #4
NeoPa
32,556 Expert Mod 16PB
This is quite fiddly to do in Access Jet SQL. I think in Transact-SQL it is referred to as a FULL [OUTER] JOIN and is really very straightforward. Jet SQL doesn't support this. It is possible to do, but a simple LEFT JOIN or RIGHT JOIN link would ensure ONE of the tables would have all its records shown but not the Null ones from the other.

To do it then we need to employ the following basic steps :
  1. Join the two recordsets into a single recordset using a UNION query. Each recordset would populate a DIFFERENT [Calls] field.
  2. Form this into a subquery.
  3. GROUP the results together by [User].

Assuming [tblIncoming] & [tblOutgoing] then, we want something like the following :
Expand|Select|Wrap|Line Numbers
  1. SELECT [User], 
  2.        Max(subU.IncomingCalls) AS [IncomingCalls],
  3.        Max(subU.OutgoingCalls) AS [OutgoingCalls]
  4. FROM (SELECT [User],
  5.              [Calls] AS [IncomingCalls],
  6.              Null AS [OutgoingCalls]
  7.       FROM [tblIncoming]
  8.       UNION ALL SELECT [User],
  9.                        Null AS [IncomingCalls],
  10.                        [Calls] AS [OutgoingCalls]
  11.       FROM [tblOutgoing]) AS subU
  12. GROUP BY subU.User
Of course a normalised database would have a single [tblCall] table with all the data in it and simply add a flag to indicate whether it's incoming or outgoing ;)
Feb 27 '08 #5
NeoPa
32,556 Expert Mod 16PB
A further point worth mentioning is that the [OutgoingCalls] field is interpreted by Jet as non-numeric because the first reference to it is simply Null. If this is a problem there are ways around it.
One is to add a very basic query as the first SELECT line of the UNION query which ensures all the fields are interpreted correctly but then filter out the resultant line. Another would be to use the whole query (either as a saved QueryDef or as another level of subquery) as the source of another and cast the field using :
Expand|Select|Wrap|Line Numbers
  1. Val(subName.OutgoingCalls) AS [OutgoingCalls]
Feb 27 '08 #6
ADezii
8,834 Expert 8TB
This is quite fiddly to do in Access Jet SQL. I think in Transact-SQL it is referred to as a FULL [OUTER] JOIN and is really very straightforward. Jet SQL doesn't support this. It is possible to do, but a simple LEFT JOIN or RIGHT JOIN link would ensure ONE of the tables would have all its records shown but not the Null ones from the other.

To do it then we need to employ the following basic steps :
  1. Join the two recordsets into a single recordset using a UNION query. Each recordset would populate a DIFFERENT [Calls] field.
  2. Form this into a subquery.
  3. GROUP the results together by [User].

Assuming [tblIncoming] & [tblOutgoing] then, we want something like the following :
Expand|Select|Wrap|Line Numbers
  1. SELECT [User], 
  2.        Max(subU.IncomingCalls) AS [IncomingCalls],
  3.        Max(subU.OutgoingCalls) AS [OutgoingCalls]
  4. FROM (SELECT [User],
  5.              [Calls] AS [IncomingCalls],
  6.              Null AS [OutgoingCalls]
  7.       FROM [tblIncoming]
  8.       UNION ALL SELECT [User],
  9.                        Null AS [IncomingCalls],
  10.                        [Calls] AS [OutgoingCalls]
  11.       FROM [tblOutgoing]) AS subU
  12. GROUP BY subU.User
Of course a normalised database would have a single [tblCall] table with all the data in it and simply add a flag to indicate whether it's incoming or outgoing ;)
Hello NeoPa, thanks a lot for your valuable input on this Thread, it is greatly appreciated. To me, it just seemed like approximately 3 dozen lines of code to solve this problem was a little excessive, but after looking at your explanation, I realize that it may not have been - thanks again.
Feb 27 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: matthewemiclea | last post by:
I am trying to display information in Access on a form that takes information from many different queries and puts them together on a spreadsheet or chart. Some specific info: The information I...
18
by: Alpha | last post by:
Hi, I'm working on a Windows applicaton with VS 2003 on windows 2000. I have a listbox that I have binded to a dataset table, "source" which has 3 columns. I would like to display 2 of those...
2
by: Olav Tollefsen | last post by:
I'm creating an ASP.NET e-commerce web site and I have to conditionally (depending on site / user settings) display prices either excluding or including tax. Prices are typically read from a...
0
by: M. David Johnson | last post by:
I cannot get my OleDbDataAdapter to update my database table from my local dataset table. The Knowledge Base doesn't seem to help - see item 10 below. I have a Microsoft Access 2000 database...
2
by: rn5a | last post by:
The different Validation controls like RequiredFieldValidator, RangeValidator etc. have a property named Display. This property can have 3 values - Dynamic, Static & None. What's the difference...
5
by: PHPBABY3 | last post by:
Hi, 1. I have two SQL tables. I will call them employees and departments: EMP: LAST_NAME, FIRST_NAME, DEPTNM DEPT: NUM, NAME Input: text string FIND Output: the LAST_NAME, FIRST_NAME...
4
by: rn5a | last post by:
A MS-Access DB has 3 tables - Teacher, Class & TeacherClass. The Teacher table has 2 columns - TeacherID & TeacherName (TeacherID being the primary key). The Class table too has 2 columns - ClassID...
4
by: dsdevonsomer | last post by:
Hello, I have one simple query joining two tables with left outer join on 3 fields and using MIN on two fields. These two tables have lot of data about 3 mil in total. I am trying to migrate db...
4
by: Yitzak | last post by:
Have a query that is arregated into crosstab query, it is working correctly. select Yes as selected from tblname or select true as selected from tblname Produces -1 ( vba value of True) in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.