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.)
Related
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]&'*'")
I have a table, "Table A", that has two values ("user" and "assessor") that are used as identifiers for data in another table, "Table B". Within "Table B" any existing user is an assessor, they can assess others.
Because "user" and "assessor" link to the same table I can't perform a search on Table B if I wish to filter user and assessor at the same time, below is an example of what I mean:
select `tableA`.*
from `tableA`
left join `tableB` on `tableA`.`user_id` = `tableB`.`id`
left join `tableB` on `tableA`.`assessor_id` = `tableB`.`id`
where LOWER(tableB.first_name) REGEXP "John" # user's name
AND LOWER(tableB.first_name) REGEXP "Bill" # assessor's name
Is there are a quick way around this problem or will I have to create a separate table specifically for assessors, or clone existing table of users some how?
You are joining the same table for different mapping, use table alias to differentiate the joining table
SELECT `tableA`.* FROM `tableA` AS TA
LEFT JOIN `tableB` AS TB1 ON TB1.id = TA.`user_id`
LEFT JOIN `tableB` AS TB2 ON TB2.id = TA.`assessor_id`
WHERE
LOWER(TB1.first_name) REGEXP "John" -- user's name
AND
LOWER(TB2.first_name) REGEXP "Bill" -- assessor's name
I have one result table and two source tables. One source have same column name for key as result table, and second one have slightly different (same values, just different column name).
So if i use code as:
--Works
Update new_table set new_column_1 = source_column_1
from new_table t0
Left join source_table_1 t1
On t0.key_column = t1.id_column
Where key_column = t0.key_column
--Don't
Update new_table set new_column_2 = source_column_2
from new_table t0
Left join source_table_2 t1
On t0.key_column = t1.key_column
Where key_column = t0.key_column
2nd example would give me "Ambiguous column name" error in Where statement. (1st key_column should belong to update destination, 2nd to update source)
I manage around that problem by wrapping source for update into subquery which would rename key column into something else, but i'm curious why that happens in a first place. How come that key names for join make effect on where clause?
You must use the alias since both table have a column named key_column.
This query should work:
Update t0 set new_column_2 = t1.source_column_2 -- or t0.source_column_2?
from new_table t0
Left join source_table_2 t1
On t0.key_column = t1.key_column
Where t1.key_column = t0.key_column
The alias will replace the table name between update and set.
But the where clause does not seem necessary. It is identical to the ON clause. A INNER JOIN would be more appropriate here.
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
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));