473,796 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing two tables and finding the number of matching records

4 New Member
I have 2 lists (unlinked and unrelated), where one can be called the parent and the other the child with one to many relationship between parent-child. The child table is really big, over a million records. The link between the two, is a set of 3 fields that must be equal between the two. So when the two match, there can be a number of records in the child field, because it is related to the usage history of the parent record.

So here is my problem, I want to add a column in the parent field to correspond to the number of matching records in the child table for that particular record. I am guessing it should not too advanced... so but can anyone please help?

For. eg. i know ID 1 has 20 records matching in child table and so i would like to add 20 in the column for ID 1 in parent table. ID 2 has no records so 0 for that and so on...
Jun 22 '09 #1
9 4637
orangeCat
83 New Member
@warrior2009
What have you tried so far?
Jun 22 '09 #2
warrior2009
4 New Member
Well anything and everything to do with design view of query building but so far no luck. I was hoping that Access would provide some functionality, afterall the idea doesnt sound too hard...

Now I am thinking maybe query may not do, and will need to create a VB script to loops through the entire parent table and create query condition for child table and store the total count of query in a variable and add it to that record. Unfortunately its been like 4 years since I did Access/VB stuff (infact any programming at all) :(

Any ideas?
Jun 22 '09 #3
patjones
931 Recognized Expert Contributor
Well, have you done anything in SQL view yet, in Query Design?

Suppose 'tblA' is the parent table, while 'tblB' is the child. As an intermediate step, you could create a table which lists the counts for all the id's:

Expand|Select|Wrap|Line Numbers
  1. SELECT * INTO tblTemp FROM (SELECT id, COUNT(*) AS id_count FROM tblB GROUP BY id);
The only thing is that this won't list id's that aren't in the table. For instance, if no entries have id = 5 then it won't appear in tblTemp. Next to update your new field in tblA which will hold the counts:

Expand|Select|Wrap|Line Numbers
  1. UPDATE tblA LEFT JOIN tblTemp ON tblA.id = tblTemp.id SET tblA.id_count = tblTemp.id_count;
Again, the entries having no match in the child table will be blank. This will fill those in:

Expand|Select|Wrap|Line Numbers
  1. UPDATE tblA SET id_count = 0 WHERE id_count Is Null;
This is a little roundabout, and I tried doing it in one statement using a correlated subquery, but Access didn't like it for some reason. But, try this and see if you get the results you expect.
Jun 23 '09 #4
warrior2009
4 New Member
Good idea zepphead80, this might work except that my tables are a bit more than going by one ID only. So here you showed me if I were to compare the ID between the two tables, but what if it is 3 fields that I need to compare between the tables? So e.g.

TblA has ID=1, Quality = X and Facility = A should correspond to the same record with the identical three fields? Sorry I should have mentioned that in my question. Because unfortunately same ID can have different other attributes which screws up the situation a little bit for me. So basically, need to find compare the two tables based on three fields instead of just one. Could you please tell me how to modify the queries in that case?

Thanks alot!
Jun 23 '09 #5
orangeCat
83 New Member
warrior2009,

I have mocked up your tables and suggest the following:
table_name field_name data_type length
ByteTbl Idx Long 4
ByteTbl ID Text 50
ByteTbl Quality Text 50
ByteTbl Facility Text 50
ByteTbl CntHistRecords Long 4

table_name field_name data_type length
ByteUseHist UsageId Long 4
ByteUseHist ID Text 50
ByteUseHist Quality Text 50
ByteUseHist Facility Text 50
ByteUseHist Usagedate Date 8
ByteUseHist Name Text 50

Create a new Table MyByte that contains the Counts for each ID+Quality+Faci lity

Expand|Select|Wrap|Line Numbers
  1. SELECT ByteTbl.ID, ByteTbl.Quality, ByteTbl.Facility, 
  2.     Count(ByteUseHist.UsageId) AS CountOfUsageId  INTO MYByte 
  3. FROM ByteTbl INNER JOIN ByteUseHist ON (ByteTbl.ID = ByteUseHist.ID)
  4.     AND (ByteTbl.Facility = ByteUseHist.Facility) AND (ByteTbl.Quality = ByteUseHist.Quality)
  5.     GROUP BY ByteTbl.ID, ByteTbl.Quality, ByteTbl.Facility 
Then, using your initial table and the new MyByte table, update the Count Field
using a query such as:

Expand|Select|Wrap|Line Numbers
  1.  UPDATE ByteTbl INNER JOIN MYByte ON (ByteTbl.ID = MYByte.ID) AND (ByteTbl.Quality = MYByte.Quality) AND (ByteTbl.Facility = MYByte.Facility) SET ByteTbl.CntHistRecords = MYByte!CountOfUsageId;
Jun 23 '09 #6
orangeCat
83 New Member
warrior2009,

Not sure what happened with my previous post. There was a final piece to place a 0 in the CntHistRecords field where no History usage records exist. To do this you would ruun an update query as follows.

Expand|Select|Wrap|Line Numbers
  1. UPDATE ByteTbl SET ByteTbl.CntHistRecords = 0
  2. WHERE (((ByteTbl.CntHistRecords) Is Null));
  3.  
Good luck.
Jun 23 '09 #7
FishVal
2,653 Recognized Expert Specialist
Hello gentlmen.

You should take care counting matching records via table join in a case when tables involved could themselves contain duplicate records.

e.g. Table1 which has 2 duplicate records and Table2 has 3 duplicate records which match those in Table1. Table join will give 6 combinations.

Regards,
Fish
Jun 23 '09 #8
patjones
931 Recognized Expert Contributor
Hi warrior -

Sorry about that, I think I was focusing too much on your example at the end of your initial post. I guess I would modify my initial statement (still keeping tblB as your child) as:

Expand|Select|Wrap|Line Numbers
  1. SELECT * INTO tblTemp FROM (SELECT id, quantity, facility, COUNT(*) AS usage_count FROM tblB GROUP BY id, quantity, facility);
Then to update the count column in the parent, tblA:

Expand|Select|Wrap|Line Numbers
  1. UPDATE tblA LEFT JOIN tblTemp ON (tblA.id = tblTemp.id AND tblA.quantity = tblTemp.quantity AND tblA.facility = tblTemp.facility) SET tblA.usage_count = tblTemp.usage_count;
And, as before, to capture the nulls:

Expand|Select|Wrap|Line Numbers
  1. UPDATE tblA SET usage_count = 0 WHERE usage_count Is Null;
Since, using this procedure, tblTemp will end up with one record each for each combination of id, quantity and facility that is in your child table, and also since your parent table has one record for each combination (based on your initial post), we should be able to avoid the situation that FishVal is referring to.

Notice that my UPDATE statement is similar to orangeCat's, although I'm trying to think about how it would turn out differently using an INNER as opposed to LEFT join. Nonetheless, our procedures are very similar; I believe you'll get the results you're looking for.
Jun 23 '09 #9
warrior2009
4 New Member
great!! Both works well! Thanks alot gents!
Jun 23 '09 #10

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

Similar topics

5
2427
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My problem is in comparing the customer number I just read in with those in the list. I'm not sure how to go about comparing c-style strings for greater than, less than.. here is how I am currently trying to do it: while( ( custinfo.number >...
6
3445
by: Taxi Driver | last post by:
Hi Everyone - Can I get your help with this, it is driving me crazy. I have 2 arrays listed below: @req=75 @req=76 @req=77 @req=78 --- @bid=75 @bid=76
2
3309
by: B Moor | last post by:
I have a database with 100,000's records, each with a unique reference, eg A123BNK456 I would like to generate a search facility whereby we can choose an exact match or partial match, where the partial match could be any of the following:- Partial match 1: 1 character maybe wrong Partial match 2: 2 characters maybe wrong Partial match 3: 3 characters maybe wrong
4
4168
by: osmethod | last post by:
Hello, I have read many articles about comparing tables, like - loops, delete queries, appending to temp table with index etc Problem: 2 tables T1 & T2. Data is suppposed to be the same in both tables but i need to check that this is so. (Maybe a user deleted a row in one table only or changed a value in a field!). The tables have no unique index.
2
2096
by: Gary Lynch | last post by:
I am looking for a simple solution to a recurrent problem with imported data in Access 97. The example below is a simplification of a problem with a much larger database. Let's say I start out with a single table, describing the colors of 4 national flags. I have 4 fields: Country Text 1stColor Text 2ndColor Text 3rdColor Text
3
2578
by: brian kaufmann | last post by:
Hi, I had sent this earlier, and would appreciate any suggestions on this. I need to make calculations for unemployment rate for three different data sources (A,B,C) for many countries and age groupings. The calculation is: unemployment rate = number of unemployed/number of labour force
2
2853
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. The SELECT query is built as follows: $itemtype = stripslashes(trim($_POST));
2
1789
by: JonoB | last post by:
Need some tips to help me approach this problem. I would consider myself advanced in Access and VBA, so just looking for conceptual pointers. In essence, I have a number of text files, each of which contains data that is parsed into various database tables. The data thus stored in the tables contains 4 fields of information. I have this part of the system working 100%. The 4 fields are: ParseID, lngTime (times are derived in thousands of a...
11
11003
by: jennifersalcido | last post by:
Hello All, I am working with two tables that contain inventory data: 1) INVENTORY contains item_number, description, etc. This data is relatively straight-forward, one record per item_number. 2) INV_CROSS_REF contains item_number, and a data column containing either a scancode or a manufacturer part number. The type column indicates what type of data is in the data column (4=scancode, 5=mfr pn). Finally, because the software allows...
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.