473,385 Members | 1,757 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,385 software developers and data experts.

Combining select queries produces dupes

I am new to MS Access and need help. I have built multiple select quieries and when I am joining them into one query I get dupes. Question: how do I eliminate dupes on joint?

Here is a sample of one of the select query:

SVP Account Name DOB CountOfEmpID
Name Account P¥FFN-N 13
Name Account P¥FFN-Y 19354


Another select query:

SVP Account Name PFName CountOfEmpID
Name Account P¥FSN-Y 3482
Name Account P¥FSN-N 15885


Here is what happenes when I join them together:

SVP Account DOB CountDOB PFName CountofEmpID
Name Account P¥DOB-N 19354 P¥FFN-N 15885
Name Account P¥DOB-Y 13 P¥FFN-N 15885
Name Account P¥DOB-N 19354 P¥FFN-Y 3482
Name Account P¥DOB-Y 13 P¥FFN-Y 3482
Feb 12 '11 #1
15 1786
Stewart Ross
2,545 Expert Mod 2GB
If you could post the SQL you are actually for both SELECT queries and the one you are trying to get to JOIN it would help us to see what is actually going on.

Normally, if you are joining two select queries and duplicates result you have a missing join in your query.

I can't tell from what you've posted as the lines you list do not contain test values within them - Name and Account are repeated on each line, which does not help us to know what they represent, and what might be a date of birth according to the heading (DOB) is not recognisable in the way you've listed it (P¥DOB-Y).

-Stewart
Feb 12 '11 #2
Thank you. Here is the SQL for the PDOB query:

SELECT [AccountName Query].[Ops SVP], [AccountName Query].Account, [AccountName Query].PDOB, Count([AccountName Query].EmpID) AS CountOfEmpID
FROM [AccountName Query]
GROUP BY [AccountName Query].[Ops SVP], [AccountName Query].Account, [AccountName Query].PDOB;

Here is the SQL for PFirstName query:

SELECT [AccountName Query].[Ops SVP], [AccountName Query].Account, [AccountName Query].PFirstName, Count([AccountName Query].EmpID) AS CountOfEmpID
FROM [AccountName Query]
GROUP BY [AccountName Query].[Ops SVP], [AccountName Query].Account, [AccountName Query].PFirstName;

Here is the SQL for the joint query:

SELECT [P-DOB].[Ops SVP], [P-DOB].Account, [P-DOB].PDOB, [P-DOB].CountOfEmpID, [P-FirstName].PFirstName, [P-FirstName].CountOfEmpID
FROM [P-DOB], [P-FirstName], [P-LastName], [P-MiddleName];
Feb 12 '11 #3
Please see the response above. Sorry for confusion
Feb 12 '11 #4
Stewart Ross
2,545 Expert Mod 2GB
Thanks for posting the SQL. As I suspected, there are no actual JOIN statements in in the final query. As there are four queries or tables in the FROM statement you will be combining all rows from all four tables together, effectively multiplying the number of rows accordingly (so if each table has 3 rows, say, then the number of rows in the output would be 3 * 3 * 3 * 3 = 3^4 = 81 rows).

In the Access query editor you need to drag a join from one table/query to the next so that all four queries are inner joined on the primary key field, which is the Account one I would guess.

In SQL it is the equivalent of using

Expand|Select|Wrap|Line Numbers
  1. SELECT { list of your fields as before }
  2. FROM [P-DOB] INNER JOIN [P-FirstName] ON [P-DOB].Account = [AccountName Query].Account [P-LastName] (INNER JOIN ... {and so on}
Access will do the join SQL for you if you simply drag from each Account field to the next in the query editor window.

-Stewart
Feb 13 '11 #5
Unfortunately, this did not help. I still have dupes in a query. They were just rearranged. Is there another way to troubleshoot my query?
Feb 13 '11 #6
May be this will help. I have a query with different fields representing Employiee ID, Account Name, Date of Birth, First Name, Last Name etc. In certain fields I have data that shows format of input that came from the specific application.So, DOB will say something like P¥DOB-Y, and first name will say P¥FFN-N. Also, Y and N at the end of each such code means if the client filled in or not this data. What I am trying to accomplish, to count in each column(field) totals of each Y and N entries and then generate a report. I was not able to get total counts in one query that is why I separated each field into separate query and I did get the totals that way. However, the challenge is to connect this select queries with each total and then create a report.

Here is SQL of the original query with all; entries. Perhaps there is a simpler way to get what I am trying to achieve?

SELECT AccountName.ID, AccountName.PCC, AccountName.Account, AccountName.EmpID, AccountName.PFirstName, Count(AccountName.PFirstName) AS CountOfPFirstName, AccountName.PMiddleName, AccountName.PLastName, AccountName.PDOB, AccountName.PGender, AccountName.[R-Field First Name], AccountName.[R-Filed Middle Name], AccountName.[R-Filed Last Name], AccountName.[R-Filed DOB], AccountName.[R-Filed Gender], [Master - Consolidated].[Ops SVP], [Master - Consolidated].[AM SVP (Global)]
FROM AccountName LEFT JOIN [Master - Consolidated] ON AccountName.Account=[Master - Consolidated].[Scode Description]
GROUP BY AccountName.ID, AccountName.PCC, AccountName.Account, AccountName.EmpID, AccountName.PFirstName, AccountName.PMiddleName, AccountName.PLastName, AccountName.PDOB, AccountName.PGender, AccountName.[R-Field First Name], AccountName.[R-Filed Middle Name], AccountName.[R-Filed Last Name], AccountName.[R-Filed DOB], AccountName.[R-Filed Gender], [Master - Consolidated].[Ops SVP], [Master - Consolidated].[AM SVP (Global)];
Feb 13 '11 #7
Stewart Ross
2,545 Expert Mod 2GB
Hi. You say that you still have duplicate rows; this in turn is telling me that your totals and select queries are not returning single rows for each unique identifier, which they MUST do if you hope to join them on that single identifier.

I have no idea why you need to have separate queries to extract names, DOBs etc for your accounts - this is not an approach I can recognise as valid, but then I have no idea how your application's tables are structured.

If you could post data that is more representative of actual data from your tables - changing names and IDs as appropriate - this would help me to see the structure of what you are recording. You need to retain the parts that are different for each row - don't make them all show as 'Name' and 'Account' as in post 1, which just obscures how your rows are structured.

Other than that I have no suggestions to offer at present, as I think the approach of having separate queries to return fundamental items such as the account holder's name and DOB is fundamentally incorrect and is so far off normal practice as to make it very difficult for me to suggest a way forward for you.

-Stewart
Feb 13 '11 #8
Thanks for your time. I attached a file with my query. Essentially, this file will contain over 100 diff accounts indicating similar data. What I am trying to accomplish, to total entries in coded fields to show how many records have data with Y and N ( at the end of code) and built a summary report that will show SVP,Account Name, and summary totals of those fields. Please let me know if you need additional details to understand this.
Attached Files
File Type: pdf AccountName Query1.pdf (6.0 KB, 305 views)
Feb 13 '11 #9
Stewart Ross
2,545 Expert Mod 2GB
The example below is one way to return totals for each grouping (in this case grouped by two fields, [Ops SVP PCC] and [Account EmpID] which I take it are forename and surname.

All that is being done is that for each grouping we test whether or not the current field has a "Y" or an "N" at the end of it. This is the bit inside the IIF statement, which is then returning a 1 if there is or a 0 if there isn't. We then SUM these over the whole group to produce the total number of "Y" or "N" characters ending each field:

Expand|Select|Wrap|Line Numbers
  1. SELECT X.[Ops SVP PCC], X.[Account EmpID], 
  2.   Count(*) as N,
  3.   Sum(IIf(Right([R-FIeld],1)="Y",1,0)) AS [RF-Y Total], 
  4.   Sum(IIf(Right([R-FIeld],1)="N",1,0)) AS [RF-N Total], 
  5.   Sum(IIf(Right([PGender],1)="Y",1,0)) AS [PG-Y Total], 
  6.   Sum(IIf(Right([PGender],1)="N",1,0)) AS [PG-N Total], 
  7.   Sum(IIf(Right([FirsRT],1)="Y",1,0)) AS [FR-Y Total], 
  8.   Sum(IIf(Right([FirsRT],1)="N",1,0)) AS [FR-N Total], 
  9.   Sum(IIf(Right([N-Failmede],1)="Y",1,0)) AS [NF-Y Total], 
  10.   Sum(IIf(Right([N-Failmede],1)="N",1,0)) AS [NF-N Total], 
  11.   Sum(IIf(Right([MidRdl-eF],1)="Y",1,0)) AS [MR-Y Total], 
  12.   Sum(IIf(Right([MidRdl-eF],1)="N",1,0)) AS [MR-N Total], 
  13.   Sum(IIf(Right([NileadmeLasRt],1)="Y",1,0)) AS [NL-Y Total], 
  14.   Sum(IIf(Right([NileadmeLasRt],1)="N",1,0)) AS [NL-N Total], 
  15.   Sum(IIf(Right([DOBR-Filed Gender],1)="Y",1,0)) AS [DOB-Y Total], 
  16.   Sum(IIf(Right([DOBR-Filed Gender],1)="N",1,0)) AS [DOB-N Total]
  17. FROM [YourQuery] AS X
  18. GROUP BY X.[Ops SVP PCC], X.[Account EmpID];
The FROM clause usess the name [YourQuery] as a placeholder; you would need to replace this by the actual name you have given to the left-joined query you posted in post 7 which lists all entries. I have aliased the YourQuery entries as X for brevity above.

This produces the following results on the test data you supplied:

Expand|Select|Wrap|Line Numbers
  1. Ops SVP PCC  Account EmpID  N  RF-Y Total  RF-N Total  PG-Y Total  PG-N Total  FR-Y Total  FR-N Total  NF-Y Total  NF-N Total  MR-Y Total  MR-N Total  NL-Y Total  NL-N Total  DOB-Y Total  DOB-N Total
  2. George       Smyit7hyd      8           0           8           8           0           0           8           0           0           0            0          0           0            0            0
  3. Marty        Tip            8           8           0           7           1           0           8           0           8           0            0          0           0            0            8
  4. Mary         Jane           6           3           3           2           4           0           5           0           3           0            0          0           1            0            3
  5.  
It may give you some ideas for how you can proceed in a slightly simpler way with what you want to do.

-Stewart
Feb 15 '11 #10
Thank you so much. I just wanted to ask for clarification. What you proposed is an additional query that I will use with my mane query in response 7? I am a little not clear on what I need to replace [YourQuery] in your SQL above. Also, would you please clarify what X is alied to? Thanks again !
Feb 17 '11 #11
Stewart Ross
2,545 Expert Mod 2GB
The use of "X" is internal to the query - you can leave this just as it is. All it does is to rename a longer name with a shorter one to make reading the query and referring to fields within that query easier.

You need to replace the one occurrence of the text "[YourQuery]" with the actual name of the query that you posted in response 7 - in other words, the name of the query as you see it when you look in the Queries tab within Access, enclosed in the [square brackets] if there is a space between parts of the name.

-Stewart
Feb 17 '11 #12
I think I am close to what I need. I was able to recreate your SQL in my query, however when I generated a new one and coded with all my fields, I do not get any result. I only receive error that my SELECT STATEMENT is incorrect or missing something. I checked over and over and cannot determine what I miss....I am not giving up yet...
Here is my latest SQL Statement based on your suggestion:
Feb 17 '11 #13
Apologies. Here is the correct SQL that I wanted to post:

Expand|Select|Wrap|Line Numbers
  1. SELECT AccountName_Query.[Ops SVP PCC], AccountName_Query.[Account EmpID],
  2. Count(*) as N, 
  3.   Sum(IIf(Right([PFirstName],1)="Y",1,0)) AS [PFirstName-Y Total],  
  4.   Sum(IIf(Right([PFirstName],1)="N",1,0)) AS [PFirstName-N Total],  
  5.   Sum(IIf(Right([PMiddleName],1)="Y",1,0)) AS [PMiddleName-Y Total],  
  6.   Sum(IIf(Right([PMiddleName],1)="N",1,0)) AS [PMiddleName-N Total], 
  7.   Sum(IIf(Right([PLastName],1)="Y",1,0)) AS [PLastName-Y Total],  
  8.   Sum(IIf(Right([PLastName],1)="N",1,0)) AS [PLastName-N Total],
  9.   Sum(IIf(Right([PDOB],1)="Y",1,0)) AS [PDOB-Y Total],  
  10.   Sum(IIf(Right([PDOB],1)="N",1,0)) AS [PDOB-N Total],
  11.   Sum(IIf(Right([PGender],1)="Y",1,0)) AS [PGender-Y Total],  
  12.   Sum(IIf(Right([PGender],1)="N",1,0)) AS [PGender-N Total],  
  13.   Sum(IIf(Right([RFirstName],1)="Y",1,0)) AS [RFirstName-Y Total],  
  14.   Sum(IIf(Right([RFirstName],1)="N",1,0)) AS [RFirstName-N Total],  
  15.   Sum(IIf(Right([RMiddleName],1)="Y",1,0)) AS [RMiddleName-Y Total],  
  16.   Sum(IIf(Right([RMiddleName],1)="N",1,0)) AS [RMiddleName-N Total],
  17.   Sum(IIf(Right([RLastName],1)="Y",1,0)) AS [RLastName-Y Total],  
  18.   Sum(IIf(Right([RLastName],1)="N",1,0)) AS [RLastName-N Total],   
  19.   Sum(IIf(Right([RDOB],1)="Y",1,0)) AS [RDOB-Y Total],  
  20.   Sum(IIf(Right([RDOB],1)="N",1,0)) AS [RDOB-N Total],  
  21.  Sum(IIf(Right([RGender],1)="Y",1,0)) AS [RGender-Y Total],  
  22.  Sum(IIf(Right([RGender],1)="N",1,0)) AS [RGender-N Total],    
  23. FROM [AccountName_Query] AS AccountName_Query
  24. GROUP BY AccountName_Query.[Ops SVP PCC],AccountName_Query.[Account EmpID];
Feb 17 '11 #14
Stewart Ross
2,545 Expert Mod 2GB
You have taken out the alias "X" which I put in and replaced it with the same name as the query itself:

Expand|Select|Wrap|Line Numbers
  1. FROM [AccountName_Query] AS AccountName_Query
You can't use an alias of the same name as the underlying query. Just remove the AS part altogether:

Expand|Select|Wrap|Line Numbers
  1. FROM [AccountName_Query]

-Stewart
Feb 17 '11 #15
I tried both ways with your suggestion for X alias and then without and both ways I get the following error: "The SELECT Statement includes a reserved word or an argument name that is mispelled..."

Never the less, I think it worked after I checked all the field names and syntax! You are brilliant!!!

I am going to spot check the date to make sure it captured all and then I will move onto building a report. Hopefully, it will be easier.. Thanks again!
Feb 17 '11 #16

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

Similar topics

7
by: frizzle | last post by:
Hi, I know this might sound strange but i think(/hope) it's quite simple: I'm running 2 queries in a mysql DB, first one returns 20 results. Now how can i echo results from the second query...
0
by: Tinku | last post by:
Hi All We are migrating one of our application from Informix to Oracle. Now one of our tables contains a column named "MODE" which is a reserved word. I could create the table with this name...
4
by: Paul | last post by:
I have run into a strange problem with a site I am working on. My SELECT queries work fine, but I just tried to add an UPDATE statement and it fails. A test showed that INSERT fails also. I created...
9
by: mfyahya | last post by:
Hi, I'm new to databases :) I need help speeding up select queries on my data which are currently taking 4-5 seconds. I set up a single large table of coordinates data with an index on the fields...
2
by: SomeDude | last post by:
Lo group, I am wondering if there is a way of combining two SELECT statements into a single query. Here's the obligatory example to clarify things: SELECT id WHERE name=mike SELECT bills...
1
by: Bruce MacDonald | last post by:
I've got a question/request for the SQL gurus. I'm building a model of bandwidth demand in MS Access and want to get aggregated results for demand at each PCP in each time period. The two...
14
by: Martin Lacoste | last post by:
Access 2000 Trying even the most basic queries - select queries joining (inner join) two tables - and the results are not updatable, and I can't tell why they aren't. All permissions are on,...
0
by: Rahul Babbar | last post by:
Hi, We have a situation in which we want to specify the isolation level ='UR' for all the select queries if no other isolation level is explicitly specified in the query. Is there some...
11
by: pattyd | last post by:
I have multiple queries that will run on my onopen event of my report. I use the docmd.openquery for the select queries the problem is I can see all these popup in the background. I do use the close...
3
by: acoppini | last post by:
I need help! I think that my problem is one of syntax using strings. Specifically I have defined a string field called issuername that I am later trying to use as an input into select queries. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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...

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.