MS ACCESS & VBA Matching Query - sql

I have two tables, I want to match values in both tables/pairing fields as below
In table 1 records 1 and 2 should be updated in MatchRef field as matched
In table 2 records 1 and 2 should be updated in matchref field as matched
but in Table 1 the record 3 should not updated as matched as there is no matching field in table 2
Table 1
ID MatchField MatchValue MatchRef
1 AAA 100
2 AAA 100
3 AAA 100
4 BBB 100
Table 2
ID MatchField MatchValue MatchRef
1 AAA 100
2 AAA 100
4 BBB 100
Here ID is not an unique filed, its just auto number.
How to do it in Ms Access? There will be 600 000 records on average in each table and working on a Citrix environment.

Here is how you would find matching records as far as I can tell --
SELECT
*
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.ID = T2.ID
AND T1.MATCHFIELD = T2.MATCHFIELD

Related

select rows with same ID but different value in another column between two tables

i have to similar tables table 1 and table 2 .I want to compare the 2 tables and show the rows that ID is the same but has a different value in another column.
table 1
ID
ACTIVE
100
1
221
1
341
1
and
table 2
ID
ACTIVE
100
1
221
0
341
1
the output should be like this:
ID
ACTIVE
221
1
select t1.*
from t1
join t2 on t1.id = t2.id
where t1.active <> t2.active

Update rows in a table based on grouping

I have a table with records of the same type with ID's and a grouping. I need to update the id's of table 1 into table 2 based on the record id from largest to smallest using the position.
Table 1
ID
Group
Name
1
A
Apple
2
A
Apple
3
B
Apple
4
B
Apple
Table 2
ID
recordid
position
group
1
1
250
A
2
2
350
A
3
null
450
A
4
null
550
A
5
3
250
B
6
4
350
B
7
null
450
B
8
null
550
B
update table2
set recordid=T1.ID
From Table1 T1
join Table2 T2 on T2.Group=T1.Group
This isn't distinct so I don't think it's right, but I can't figure it out.
where ?????
the problem is you have multiple Id per group , so you have to choose one :
update table2
set recordid= ( select top 1 T1.ID
From Table1 T1
join Table2 T2 on T2.Group=T1.Group
)
where recordid is null

group by id and random sample by id from two tables in big query sql

I have a 2 tables with the same structure:
table1
id text var
1 "bla bla" 100
1 "blabla1" 30
2 "qweweqty" 0
2 etc...
7
3
3
1
..
100
table2
id text var
101 "bla bla" 10
101 "bla1" 60
101 "bla" 5
103 etc...
102
103
102
110
..
200
I want to randomly sample data from table1 and table2 based on id. so basically, sample every observation for a random sample of ids from table1 and and every observation from random sample of ids from table2 so that 50 ids are from table 1 and 50 are from table 2 . any idea on how to do this on big query SQL?
en example would be the following where I want to get 3 ids from table1 and 3 from table2
randomly ids 1,2,3 are selected from table1, ids 101, 110 and 103 are selected from table2
the resulting table is then:
id. text var
1. .. ..
1
2
2
3
3
1
101
101
101
103
103
110
so basically any observation from table1 with id 1,2,3 and any observation from table2 with id 101, 103, 110 are selected and put in the same table:
so the passages are two: first randomly select a certain number of ids from table1, and a certain number of ids from table2, then I select any observation corresponding to those ids from both tables and I join them in the same table
If you want 50 ids from each table, then you can limit them using subqueries:
select t1.*
from t1 join
(select distinct id
from t1
order by rand()
limit 50
) ids
on t1.id = ids.id
union all
select t2.*
from t2 join
(select distinct id
from t2
order by rand()
limit 50
) ids
on t2.id = ids.id

Select a record based on a condition

I have 3 tables. Table 1 gets updated every time I upload a file. Table 2 is an archival table that stores records in all files uploaded. Table 3 stores data of table 2 after some validations.
1st File upload:
Table 1:
RefNo | Code | ID
--------+----------+-----
1234 30 1
5678 40 2
Table 2:
RefNo | Code | ID
--------+----------+------
1234 30 1
5678 40 2
2nd File upload
Table 1:
RefNo | Code | ID
--------+----------+-----
1234 50 3
5678 60 4
Table 2:
RefNo | Code | ID
--------+----------+------
1234 30 1
5678 40 2
1234 50 3
5678 60 4
3rd File upload (UPDATE code=100 for refno = 1234)
Table 1:
RefNo | Code | ID
--------+----------+------
1234 100 5
Table 2:
RefNo | Code | ID
--------+----------+------
1234 30 1
5678 40 2
1234 50 3
5678 60 4
1234 100 5
Now, there are 3 records for 1234 in table 2. I want to update code to 100 in table 3 only if last uploaded code is 0. How can I select the last uploaded record?
SELECT *
FROM table2 a
JOIN table 1 b ON a.refno = b.refno
WHERE a.id = (SELECT ???)
I don't know how to pick ID = 3 (last uploaded one) and not multiple IDs.
if the ids are type int, you can use this to get the last record for all refno's in table1 that exist in table2. Then you can filter those as needed:
select t2.*
from table2 t2
join (
select t2.RefNo,
MAX(t2.ID) lastId
from table2 t2
JOIN table1 t1 ON t2.RefNo = t1.RefNo
WHERE t2.Id < t1.Id
GROUP BY t2.RefNo
) ti on t2.Id = ti.lastId

Select records from a table where two columns are not present in another table

I have Table1:
Id Program Price Age
12345 ABC 10 1
12345 CDE 23 3
12345 FGH 43 2
12346 ABC 5 4
12346 CDE 2 5
12367 CDE 10 6
and a Table2:
ID Program BestBefore
12345 ABC 2
12345 FGH 3
12346 ABC 1
I want to get the following Table,
Id Program Price Age
12345 CDE 10 1
12346 CDE 2 5
12367 CDE 10 6
I.e get the rows from the first table where the ID+Program is not in second table. I am using MS SQL Server express 2012 and I don't want to add any columns to the original databases. Is it possible to do without creating temporary variables?
Several ways to do this, here's one using not exists:
select *
from table1 t1
where not exists (
select 1
from table2 t2
where t1.id = t2.id and t1.program = t2.program
)
One possible variant is to use LEFT JOIN:
SELECT
Table1.*
FROM
Table1
LEFT JOIN Table2
ON Table1.ID = Table2.ID
AND Table1.Program = Table2.Program
WHERE
Table2.ID IS NULL