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..
6 1452
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..
- Create a Table named tblResults with the following Fields
- User [TEXT]
- IncomingCalls [LONG]
- OutGoingCalls [LONG]
- I'm assuming User Names are Unique and spelled correctly if they exist in both the Incoming and OutGoing Calls Tables.
- 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.
- Do not rely 100% on this solution, the SQL guys will probably have a better one!
- The entire code block is listed below, but, again download the Attachment:
-
Dim MyDB As DAO.Database, MyRS As DAO.Recordset, strSQL As String
-
Dim rstResults As DAO.Recordset
-
-
'DELETE any existing Records from tblResults
-
DoCmd.SetWarnings False
-
DoCmd.RunSQL "Delete * From tblResults;"
-
-
'Add [User] and [IncomingCalls] to tblResults
-
strSQL = "INSERT INTO tblResults ( [User], IncomingCalls ) SELECT [tblIncomingCalls].[User], " & _
-
"[tblIncomingCalls].[IncomingCalls] FROM tblIncomingCalls;"
-
DoCmd.RunSQL strSQL
-
-
DoCmd.SetWarnings True
-
-
'Create a Recordset based on tblOutgoingCalls
-
Set MyDB = CurrentDb()
-
Set MyRS = MyDB.OpenRecordset("tblOutGoingCalls", dbOpenForwardOnly)
-
-
'Create a Recordset based on tblResults
-
Set rstResults = MyDB.OpenRecordset("tblResults", dbOpenDynaset)
-
-
Do While Not MyRS.EOF
-
'Does the User already exist? If not then Add to tblResults, but if he/she does exist
-
'then Update tblResults with the OutGoingCalls values
-
If DCount("*", "tblResults", "[User] = '" & MyRS![User] & "'") = 0 Then
-
rstResults.AddNew
-
rstResults![User] = MyRS![User]
-
rstResults![OutGoingCalls] = MyRS![OutGoingCalls]
-
rstResults.Update
-
Else
-
'Find the right User
-
rstResults.FindFirst "[User] = '" & MyRS![User] & "'"
-
rstResults.Edit
-
rstResults![OutGoingCalls] = MyRS![OutGoingCalls]
-
rstResults.Update
-
End If
-
MyRS.MoveNext
-
Loop
-
-
MyRS.Close: Set MyRS = Nothing
-
rstResults.Close: Set rstResults = Nothing
- Create a Table named tblResults with the following Fields
- User [TEXT]
- IncomingCalls [LONG]
- OutGoingCalls [LONG]
- I'm assuming User Names are Unique and spelled correctly if they exist in both the Incoming and OutGoing Calls Tables.
- 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.
- Do not rely 100% on this solution, the SQL guys will probably have a better one!
- The entire code block is listed below, but, again download the Attachment:
-
Dim MyDB As DAO.Database, MyRS As DAO.Recordset, strSQL As String
-
Dim rstResults As DAO.Recordset
-
-
'DELETE any existing Records from tblResults
-
DoCmd.SetWarnings False
-
DoCmd.RunSQL "Delete * From tblResults;"
-
-
'Add [User] and [IncomingCalls] to tblResults
-
strSQL = "INSERT INTO tblResults ( [User], IncomingCalls ) SELECT [tblIncomingCalls].[User], " & _
-
"[tblIncomingCalls].[IncomingCalls] FROM tblIncomingCalls;"
-
DoCmd.RunSQL strSQL
-
-
DoCmd.SetWarnings True
-
-
'Create a Recordset based on tblOutgoingCalls
-
Set MyDB = CurrentDb()
-
Set MyRS = MyDB.OpenRecordset("tblOutGoingCalls", dbOpenForwardOnly)
-
-
'Create a Recordset based on tblResults
-
Set rstResults = MyDB.OpenRecordset("tblResults", dbOpenDynaset)
-
-
Do While Not MyRS.EOF
-
'Does the User already exist? If not then Add to tblResults, but if he/she does exist
-
'then Update tblResults with the OutGoingCalls values
-
If DCount("*", "tblResults", "[User] = '" & MyRS![User] & "'") = 0 Then
-
rstResults.AddNew
-
rstResults![User] = MyRS![User]
-
rstResults![OutGoingCalls] = MyRS![OutGoingCalls]
-
rstResults.Update
-
Else
-
'Find the right User
-
rstResults.FindFirst "[User] = '" & MyRS![User] & "'"
-
rstResults.Edit
-
rstResults![OutGoingCalls] = MyRS![OutGoingCalls]
-
rstResults.Update
-
End If
-
MyRS.MoveNext
-
Loop
-
-
MyRS.Close: Set MyRS = Nothing
-
rstResults.Close: Set rstResults = Nothing
Thanks! that worked!
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.
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 : - Join the two recordsets into a single recordset using a UNION query. Each recordset would populate a DIFFERENT [Calls] field.
- Form this into a subquery.
- GROUP the results together by [User].
Assuming [tblIncoming] & [tblOutgoing] then, we want something like the following : - SELECT [User],
-
Max(subU.IncomingCalls) AS [IncomingCalls],
-
Max(subU.OutgoingCalls) AS [OutgoingCalls]
-
FROM (SELECT [User],
-
[Calls] AS [IncomingCalls],
-
Null AS [OutgoingCalls]
-
FROM [tblIncoming]
-
UNION ALL SELECT [User],
-
Null AS [IncomingCalls],
-
[Calls] AS [OutgoingCalls]
-
FROM [tblOutgoing]) AS subU
-
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 ;)
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 : - Val(subName.OutgoingCalls) AS [OutgoingCalls]
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 :- Join the two recordsets into a single recordset using a UNION query. Each recordset would populate a DIFFERENT [Calls] field.
- Form this into a subquery.
- GROUP the results together by [User].
Assuming [tblIncoming] & [tblOutgoing] then, we want something like the following : - SELECT [User],
-
Max(subU.IncomingCalls) AS [IncomingCalls],
-
Max(subU.OutgoingCalls) AS [OutgoingCalls]
-
FROM (SELECT [User],
-
[Calls] AS [IncomingCalls],
-
Null AS [OutgoingCalls]
-
FROM [tblIncoming]
-
UNION ALL SELECT [User],
-
Null AS [IncomingCalls],
-
[Calls] AS [OutgoingCalls]
-
FROM [tblOutgoing]) AS subU
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |