Access SQL - UPDATE If no record exists - sql

I'm trying to update checkbox in record If record in table doesn't exist in another table.
Example data:
Table1:
111 John Davies
222 Mike Johnson
333 Allen Mckenzie
Table2:
000 John Jackson
222 Laura Kent
444 Paul Saint
I want to update only records, that have all fields different together, so in other words - each field doesn't exist. In my example result for this would be:
444 Paul Saint
I've tried with NOT IN, LEFT JOIN, INNER JOIN, but can't figure out correct clause because I need to update checkbox - so clause MUST Involve UPDATE.
Any ideas ?

I think something like this should work:
Update Table1
SET Checkbox = True
WHERE NOT EXISTS(SELECT 1
FROM Table2
WHERE table1.id=table2.id
OR table1.name=table2.name
or table1.surname=table2.surname)

Related

proc sql function to find mulitple LIKE matches?

I'm having trouble with a LIKE function in proc sql.
PROC SQL;
CREATE TABLE NAMES_IDS AS
SELECT DISTINCT
T1.*
,T2.NAMES
,T2.NAME_ID
FROM WORK.table1 T1
LEFT JOIN data.table2 T2 ON T2.NAMES like T1.NAMES1
;QUIT;
I have several names in t2, lets say for example theres John 1, John 2, John 3, John 4, etc and in t1.Names1 there is %John%
proc sql is just pulling in the first match, John 1 and its associated ID, and applying it to all the data in T1, instead of duplicated a match for all matching names (this is what I want to achieve).
So the end table would have something like
COLUMN A COLUMN B
John John 1
John John 2
John John 3
John John 4
But instead, what I get is:
COLUMN A COLUMN B
John John 1
John John 1
John John 1
John John 1
Hopefully this makes some sort of sense...
I think I figured it out, I added TRIM to my code and I guess there may have been some erroneous spaces somewhere because that seems to fix my issue. Thanks for your responses!

Many to one merging sql

I have three tables as below:
First Table Second Table Third Table
Name PIN Id City City_id
David 1948 1 Roma 3
Susan 1245 2 Berlin 2
Jack 1578 3 New York 3
Hans 1247 2
Rose 8745 1
I want to merge first and second table according to third table. Result will be: Person
Name PIN City
David 1948 New York
Susan 1245 Berlin
Jack 1578 New York
Hans 1247 Berlin
Rose 8745 Roma
Firsty I can merge second and third table and then merge the result table with first table but I want to solve this problem without a medium table. How can I handle this? How can I combine first table's rows in sequence with a specified row in second table according to third table?
You would need a fourth table, PersonCity, with PersonID and CityID to link together. Think of relational databases like a grid (spreadsheet, roads). If you're going North and the street you want to get on is parallel (think |^| |^|) you're gonna need to use a different road that links the two. Currently, you have no such path.
The short answer is that your tables are not adequate for the task, what you need is along the lines of:
Table_1 Table_2 Table_3
Id Name PIN Id City Name_id City_id
1 David 1948 1 Roma 1 3
2 Susan 1245 2 Berlin 2 2
3 Jack 1578 3 New York 3 3
4 Hans 1247 4 2
5 Rose 8745 5 1
Then you can do your query as follow:
SELECT T1.Name, T1.PIN, T2.City
FROM Table_1 T1 LEFT JOIN Table_3 T2 ON T1.Id = T3.Name_id
LEFT JOIN Table_2 ON T3.City_id = T2.Id
ORDER BY T1.Name
Or you could ORDER BY City, name
I have good news and bad news.
The good news, Given the tables the way they were originally specified, in Oracle, this will give you something that looks like what you are asking:
---
--- Pay attention, This looks right but it is not!
---
select name,pin,city from
( select name,pin,rownum rn from first ) a,
( select city,id from second) b,
( select id,rownum rn from third ) c
where
a.rn=c.rn AND
b.id=c.id;
NAME PIN CITY
-------------------- ---- --------------------
Rose 8745 Roma
Susan 1245 Berlin
Hans 1247 Berlin
David 1948 New York
Jack 1578 New York
The bad news is this does not really work and is cheating. You will get results but they may not be what you would expect and they won't necessarily be consistent.
The database orders records in its own order. If you don't specify an order by clause, you get what they give you, which may not be what you want. This is cheating because Oracle does not REALLY support using rownum in this way because you can't bet on what you will get. This won't work in most other databases.
The only correct way is what #daShier gave, where you have to add something, say, ID, to allow connecting the rows in the order you want.

Join record between table only once in MS Access

I have tbl1 Structured like this
Name Type
====== =====
John 1
David 1
Jane 2
William 3
Alex 2
Ryan 1
And tbl2 structured like this
Index Type Job
1 1 Clean
2 1 Wash
3 2 Carry
4 2 Package
5 3 Sell
I would like to join record with matched Type, but each record in tbl1 only join once with one record in tbl2
Ie:
If
John is joined with Clean then David must be joined with Wash. Or if John is joined with Wash then David must be joined with Clean.
Doesn't matter if David is joined with Wash or Clean , I only need them to be joined with record that match the criteria and be joined ONCE.
I will make sure for each Type in 'tbl1' there will be equivalent amount of record in 'tbl2'
I mainly work on MS Access so Query on this environment would be the best~ Thank you all for reading.
Best regards
Try below query.
select name, (select TOP(1) job from tbl2 where tbl1.type = tbl2.type) from tbl1
Hope it help

Do not return any records if one value exist in a field?

I have the following records:
ID FIRST LAST CATEGORY
123 Tom Smith Teacher
123 Tom Smith Tutor
345 Julia Brown Banker
345 Julia Brown Tutor
567 Dan Davids Fireman
567 Dan Davids Golfer
567 Dan Davids Painter
I want to exclude all records that has 'Tutor' as a value.
My desired output would be this:
ID FIRST LAST CATEGORY
567 Dan Davids Fireman
567 Dan Davids Golfer
567 Dan Davids Painter
How do I go about doing so?
The names come from the NAMES table and the categories comes from the CATEGORY table. The primary key is the ID field.
SELECT id, first, last, category FROM names WHERE
ID NOT IN (SELECT id FROM names where category = 'Tutor')
Use a SELECT with a JOIN and a WHERE clause that excludes the 'Tutor' value.
You didn't describe your schema well enough for me to give an exact query, so you may have to change some naming, but this is the idea:
SELECT names.id, `first`, `last`, category
FROM names JOIN category on names.category_fk = category.id
WHERE category.category != 'Tutor';
Making a few assumptions on table structure it would look something like this:
SELECT
[Name].[ID]
,[Name].[first]
,[Name].[Last]
,[Category].[Category]
FROM
[Name]
INNER JOIN [Category]
ON [Category].[Category]
= [Name].[Category]
AND [Category].[Category]<>'Tutor'

Joining two tables without duplicating

I have tables like this
Table 1 :
ID NAME
001 John
Table 2 :
ID NAME FAMILY
001 John Kate
001 John Jane
Table 3 :
ID NAME TRAINING
001 John ERP
001 John CCNA
001 John Java
I want to join these tables and show data like this :
Join Table :
ID NAME FAMILY TRAINING
001 John Kate ERP
001 John Jane CCNA
001 John Java
Can someone help me find a SQL statement so that I can get that result?
I Try using UNION like this
SELECT table1.ID, table1.name, table2.family, null as training
FROM table1 INNER JOIN table2 ON table1.ID = table2.ID
UNION
SELECT table1.ID, table1.name, null as family, table3.training
FROM table1 INNER JOIN table2 ON table1.ID = table3.ID
i got result like this :
ID NAME FAMILY TRAINING
001 John Kate NULL
001 John Jane NULL
001 John NULL Java
001 John NULL CCNA
001 John NULL ERP
but, i want to got result like this
ID NAME FAMILY TRAINING
001 John Kate ERP
001 John Jane CCNA
001 John NULL Java
so, anyone here can help me to solve this problem?
Don't think this can be done in a generic way. There is no way to connect Training to Family, there is no connection between the 2. Kate has nothing to do with ERP, could just as easily be CCNA or Java. Therefor, you will always get 6 results: John-Kate-3 Trainings, John-Jane-3 Trainings. (which is the correct result, logically speaking)
If you really want the result you mentioned, and I can't think of a reason why you would, you'd have to write a non-generic query. This would probably be along the lines of:
First select John, Kate and a (random?) training (top 1, or something, I'm not familiar with firebird)
Then select the same for Jane
Finally select John, NULL and the left-over training.
Like I said, this would be a non-generic query where you use the values "Kate" and "Jane" in the where clause(s).
If I where you, I'd have some deep thoughts about the whole set-up, and what you're trying to accomplish. You are trying to use SQL to do something it was never meant to do, in fact, it's the opposite of what SQL was meant to do.