Append If the item does not exist in another table? - ms-access

I have a table record source (contains tons of info), and I have a table that contains 1200 records. I would like to append into this destination table (containing 1200 records) any records that currently do not exist. the criteria for my items to be appended is:
"Not In ([TABLE - To Work].[Item Number])"
Problem is, it is returning the record I want, back 1200 times, instead of once.
For Example:
Table A: Table B:
Item Number Item Number
12345 45678
45678
"12345" would append into table B only once (and then never append again!) I looked for a few solutions, and I tried using the unmatched query wizard, but I do not think it was really what I Wanted (It generated where the number is null). What do I need to do to make this sort of look at entire scope of the table and not item by item (I assume thats why it is populating the same number of times as existing records)? What step am I leaving out?.

The general form of your query will be something like
INSERT INTO [Table B] ( [Item Number] )
SELECT [Table A].[Item Number]
FROM [Table A]
WHERE [Table A].[Item Number] NOT IN (SELECT [Item Number] FROM [Table B]);
Note that [Table B] is not in the FROM clause of the main query, it is only in the FROM clause of the NOT IN subquery.

This worked for me:
INSERT INTO [Table B] ( [Item Number] )
SELECT DISTINCT [Table A].[Item Number]
FROM [Table A] LEFT JOIN [Table B] ON [Table A].[Item Number] = [Table B].[Item Number]
WHERE ((([Table B].[Item Number]) Is Null));

Related

Matching values from another table - access

Hoping someone can help me on this. I have two tables where I am trying to grab a field from one table if it contains a value.
Table 1
Value Level
Officer C-Level
Exec C-Level
Table 2
Title Level2
Chief Executive C-Level (desired output)
Info Officer C-Level (desired output)
Oper Officer C-Level (desired output)
Essentially, in table 2, if the "Title" field contains a value from Table 1, then I would want the "Level" from table 1 to be populated in Table 2.
I am essentially looking for 'value' appearing anywhere in 'title'.
When trying to a Dlookup query, I see no results.
Level2: DLookup ("[Level]", "Table1", "[Title]" Like [Value])
I am missing something but not sure what.
As #June7 proposed, or:
SELECT *
FROM Tbl1 AS T1 inner join
Tbl2 AS T2
on T2.Title like '*' & T1.Value & '*'
But this query has a high chance of returning more than one Tbl1 row for any given Tbl2 row (hypothetical example: 'manager' would match both 'senior manager', and 'executive manager'), in these cases DLookup function returns the first occurrence.
For DLOOKUP you might use:
Level2: DLookUp("[Level]","Table1", "InStr('" & Table2.[Title] & "', [Value]) > 0")
Or
Level2: DLookUp("[Level]","Table1","'" & Table2.[Title] & "' like '*'&[Value]&'*'")

MS Access - Outer Join ON condition not behaving

I'm working with a query that does a simple outer join on 2 tables. Originally, the join is coded:
FROM [T1]
LEFT JOIN [T2]
ON [T1].[Bank Account #] = [T2].Account)
AND [T1].[Payment Reference] = [T2].[Reference #]
Simple enough. Until the "Reference" numbers started overlapping. Easiest solution is to code the join ONLY when the T1."payment dt" is <= T2."cashed date"
I have a ref# 222456 with a "cashed date" of 1/1/2011.
I have a new row w/ 222456 (different types, and numbers now overlapping. Type not available to match) with a payment date of 1/1/2016
To complicate matters, T1's Check date field is text, T2 is a date/time.
If I add this line:
and (Format(cDate([T1].[Payment Date]),'YYYYMMDD')
It DROPS the T1 row altogether.
I test the condition in the select, and it looks to be functioning as it should.
Where this condition is TRUE - I get appropriately matched T2 data
Where this condition is FALSE - and NO T2 record exists - I get just the T1 rec.
Where this condition is FALSE - but there is an existing T2 row where the first 2 fields match (but the date check fails) - then I get NO row returned at all.
I need T1 to return as if a T2 match is not found (which the ON should indicate)
Ideas?
Thanks!

Mysql select rows from multiple tables in specific order

I have 2 tables in a mysql database ("comments" and "replies")
They have the schemas:
comments: id,userId,text
replies: id,userId,commentId,text
I need a mysql query that will fetch all comments from the comment table, and after each comment, the corresponding replies from the replies table...
so if we had: [comment 1] and [comment 2] in the comments table, and [reply 1] (to comment 1) and [reply 2] (to comment2) in the replies table
then it would return:
[comment 1]
[reply 1]
[comment 2]
[reply 2]
You would need to join the two tables & then order based on the commentID & replyID for multiple replies.
In the following I have added a dummy replyID of 0 for the original comment. The tables are joined using UNION ALL. Note that I have renamed the original id column of the comments table & reply id so they do not clash.
SELECT id commentID, userID, text, 0 replyID FROM test.comments
UNION ALL
SELECT commentID, userID, text, id replyID FROM test.replies
ORDER BY commentID, replyID;

Writing a Iif statement to update a table

I am using Access 2010.
I am trying to do an operation between three tables. The first and second table have an identical ID field, and so are joined using this. The second and third table have the IDs of departments, and so are joined using this. Not all of the IDs in the second table exist in the third table, though.
I am trying to run a query that will update a field in the first table. The query will determine whether the Department ID in the second table exists in the third table. If it does, it uses another field in that third table (Department ID Full Name) to update the first table. If it doesn't, it uses the existing Department ID in the second table to update the first table.
This is what I've got so far:
Field: Relevant field in Table 1
Table: Table 1
Update To: IIf([Table 2].[Dept ID] In (SELECT [Table 3].[Dept ID] FROM [Table 3]),[Table 3].[Dept ID Full Name],[Table 2].[Dept ID])
I've never written anything like this before and am totally new to Iif queries, so am not sure if this is even remotely close! Currently I have an error: "Operation must use an updateable query".
Edit - In SQL:
UPDATE
([Table 1] INNER JOIN
[Table 2 ON [Table 1].[ID] = [Table 2].[ID]) INNER JOIN
[Table 3] ON [Table 2].[Dept ID] = [Table 3].[Dept ID]
SET [Table 1].[Dept] =
IIf([Table 2].[Dept ID] In (SELECT [Table 3].[Dept ID] FROM [Table 3]),
[Table 3].[Dept Name],[Table 2].[Dept ID]);
If not all of the ID's that are in [Table 2] exist in [Table 3] then a solution involving a LEFT (or RIGHT) JOIN may be required. I just tried the following SQL statement and it seems to be working for me:
UPDATE
(
[Table 1]
INNER JOIN
[Table 2]
ON [Table 1].Key1 = [Table 2].Key1
)
LEFT JOIN
[Table 3]
ON [Table 2].[Dept ID] = [Table 3].[Dept ID]
SET
[Table 1].FieldToUpdate = IIf(IsNull([Table 3].[Dept ID]),[Table 2].[Dept ID],[Table 3].[Dept ID Full Name]);
(I made up some of my own column names before you edited your question, so you'll have to adjust to suit.)

Joining ID's for a combobox

I have two tables with "Field Name" columns. Some Table B field names are the same as Table A field names. If that is the case, I want to exclude those from the combobox so I don't have a double (I only want the Table A field name in that case). I also need the ID's (unique to each table) in the combobox.
I can't seem to come up with the right SQL logic. Right now, I'm trying the following
SELECT [fldID], [fldName] FROM OISInfo UNION
(SELECT [ID], [Field Name] FROM FldDef
LEFT JOIN OISInfo ON [Field Name] = [fldName] WHERE [fldName] IS NULL)
but Access keeps telling me that the join expression is not supported (in the bracketed part). The table names are definitely correct.
What am I doing wrong?
Tested.Worked perfectly. Table5 is your table A or maybe OISInfo. Table 6 is your table B (FldDef)
SELECT Table5.ID, Table5.Field1
FROM Table5
UNION
SELECT Table6.ID, Table6.Field1
FROM Table6 LEFT JOIN Table5 ON Table6.[Field1] = Table5.[Field1]
WHERE (((Table5.Field1) Is Null));
Union takes cares of doubles, this is all you have to do
SELECT [fldID], [fldName] FROM OISInfo
UNION
SELECT [ID], [Field Name] FROM FldDef