Comparing 3 tables in an Access Database - sql

I am trying to compare a series of tables within an access database, 2 local and one linked.
Table A (local) contains UserID, Title, Position; Table B (linked) contains UserID, Title, and Position from the previous week (records could possibly change on a week to week basis); Table C (local) contains UNIQUE UserID's and Titles.
I need to ensure that all UserID's contained in Table C still exist
in Table A.
I need to ensure that all UserID's contained in Table C have not had
a change in Title or Position from the previous week. If so Add to a temp table.
I'd prefer to use Access VBA or SQL in accomplish this task and the information will be displayed in a report.

Basically the same logic for both examples. use a left join to to identify mismatches.
Identify missing users in A
Insert into TableA (userID,Title)
select TableC.UserID, TableC.Title
from TableC
left join TableA on TableC.UserID=TableA.UserID
where TableA.UserID is null
Identify changes from B to A
insert into temp (userID,title,position)
select c.userID,c.title,c.position
from TableA a
left join tableB b on b.userid=a.userID and b.title=a.title and b.position=a.position
where b.userID is null

Related

a query to compare data from two tables A AND B and give the results as per table A

I have a list of phone numbers that am sending messages to. in table A I record all the delivery status of each number.
I created another table B and stored some contacts in it.
I want an SQL query that I can use to compare the data in A and B such that, I want to get delivery status of numbers in table B from table A. If a number is appearing in the table A and B then I get the delivery status of that number.
I am using this SQL query:
SELECT address, delivery_status FROM safaricom_receipt
WHERE address IN (SELECT mobile FROM saf_dispute);
This will work for you :
SELECT address, delivery_status
FROM safaricom_receipt
INNER JOIN saf_dispute
ON safaricom_receipt.address=saf_dispute.mobile;
Assuming you are using SQL Server; this should work for you
SELECT sd.mobile, sr.delivery_status
FROM saf_dispute sd with (nolock)
LEFT OUTER JOIN safaricom_receipt sr with (nolock) on sd.mobile=sr.address
where sr.delivery_status is NOT NULL
you would get duplicate rows if you are storing multiple rows for the same mobile number in any table. I would suggest adding a datetime column in the future

JOIN of 4 tables, how to restrict SELECT columns to one table only?

I am working on ABAP program - user input is to query column ANLAGE and output is to get all records from table EADZ (and only fields of EADZ) based on ANLAGE.
Statement and joins should work like this:
Input ANLAGE, find in table EASTL, gets LOGIKNR
Input LOGIKNR, find in table EGERR, gets EQUNR
Input EQUNR, find in table ETDZ, gets LOGIKZW
Input LOGIKZW, find in table EADZ, gets all records (this is the final output)
Here is the code I tried:
DATA: gt_cas_rezy TYPE STANDARD TABLE OF eadz,
lv_dummy_eanl LIKE eanl-anlage.
SELECT-OPTIONS: so_anl FOR lv_dummy_eanl NO INTERVALS NO-EXTENSION.
SELECT * FROM eadz
INNER JOIN etdz ON eadz~logikzw EQ etdz~logikzw
INNER JOIN egerr ON etdz~equnr EQ egerr~equnr
INNER JOIN eastl ON egerr~logiknr EQ eastl~logiknr
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE eastl~anlage IN #so_anl.
I got the records from table EADZ except that the date fields are empty (even though, they are filled in database table). I am assuming there is a problem with JOINs since in statement like this I join all the fields of all 4 tables into one "record" and then to corresponding fields of internal table.
How to get the values of date fields?
You can find the answer in the documentation.
If a column name appears multiple times and no alternative column name was granted, the last column listed is assigned.
In your case, at least two tables share the same column name. Therefore the values from the last mentioned table are used in the join.
You can solve this by listing the columns explicitly (or eadz~* in your case), giving an alias if required.
SELECT EADZ~* FROM EADZ INNER JOIN ETDZ ON EADZ~LOGIKZW = ETDZ~LOGIKZW
INNER JOIN EGERR ON ETDZ~EQUNR = EGERR~EQUNR
INNER JOIN EASTL ON EGERR~LOGIKNR = EASTL~LOGIKNR
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE EASTL~ANLAGE IN #SO_ANL.
If you require additional fields, you can add them explicily with e.g. EADZ~*, EASTL~A.

SQL - Set field as count from another table

I am trying to set a field in my target table as the count of the email addresses in another table. An email address can appear many times in my other table and I wanted to count the total up and set that count as my target field value.
SELECT
a.*
,a.PickedUp_Count AS COUNT(b.Emailaddress)
FROM
Master_List a
INNER JOIN
Picked_Up b
ON
a.Emailaddress = b.Emailaddress
It's best not to use * selectors because usually you want to know specifically what you're getting back from a table. You don't have to expand it in the select, but you DO have to identify how all of the individual columns will also be aggregated in the group by column.
SELECT
a.<field1>
a.<field2>
....
,COUNT(*) as PickedUp_Count
FROM
Master_List a
INNER JOIN Picked_Up b
ON a.Emailaddress = b.Emailaddress
GROUP BY
<field1>
,<field2>
....
The group by must include all fields listed in the select which are not already being aggregated in the select - i.e. any field that isn't the count.
Additionally, it's worth noting that PickedUp_Count is whatever random name you choose.

Joining two tables to update results in third table using like

I am trying to join two tables and update the results in the third table.
So table A is the results table and it has the columns customer number and score.
Table B has customer number and ind_code and table C has ind_code and ind_score.
So the output of the query should be such that the ind_code in table B and C should join together based on the first two digits and ind_score should be updated in Table A in the score column. Table A and Table B should be joined on the basis of customer number.
Could anyone please help. I tried multiple queries but nothing seems to work. i am using oracle sql developer
Generally, the JOIN operation mustn't cut field information but if your structure (for me not correct) is that ...
If I understand better:
UPDATE TableA
SET score =
(SELECT MAX(C.ind_score)
FROM TableC C
JOIN TableB B
ON C.ind_code = SUBSTRING(B.ind_code, 1, 2)
WHERE B.customernumber = TableA.customernumber)
I use on subquery MAX aggregate function, because I don't know if your cut of ind_code of TableB can be not unique (i.e. you have ind_code 5555 and 5554)

How to know the number difference between 2 tables in sql

I really need to know the query to display another field which is I want to name it as "number_difference" between 2 tables that required a numeric (in this case as a quantity).
I have 2 tables that totally same, let say, I modify the value from table A so some value in table A different with table B. And I want to join it into 1 table that display rows that some values were modified. I already get the result by this query :
**
select a.T1, a.T2 a.T3 ... from A where not exists (select * from B
where a.T1=b.T1 and a.T2=b.T2 and a.T3=b.T3)
**
This query works well. But, I want to add more field, the difference number between this 2 field (quantity) in 2 different tables. So let say, a.T3 and b.T3 are quantities. And want to display it as "number_difference" next to field (T2) which I display. Sorry I can't post images, they say I need at least 10 reputation to post. Please help me master, how can I make it everytime I use join/inner join it always display soo many rows that I only need the rows which a value from 1 table I have modified.
Thanks in advance.
You can get something like this
Select * From A Where A.id not in( Select Id from B)
or
Select A.* From A left join B on A.id = B.id Where A.id <> B.id
If you are specifically targeting a situation with two identical tables, where one underwent UPDATE operations only (no INSERT), and you want to identify those records which were modified, then:
select a.* from a, b where a.id=b.id and ( a.c1!=b.c1 or a.c2!=b.c2)