Access combo box to return value from another table - sql

I have a table called initial_eval that has a field called Partno.
What I would like to do is match the Partno value in a field
also called 'Partno' from a table called 'update'.
What I would then like to do is return the value in the
'consumption' field of 'update' where the 'Partno' values match.
I'm ok with SQL and join stuff, but I can't crack this one
Please help.

SELECT U.CONSUMPTION FROM UPDATE U
WHERE U.PARTNO IN (SELECT PARTNO FROM INITIAL_EVAL)
This assumes there is no condition while fetching from INITIAL_EVAL
SELECT U.CONSUMPTION FROM UPDATE U
WHERE U.PARTNO = (SELECT PARTNO FROM INITIAL_EVAL WHERE **some condition**)
In this case you need to frame the inner query in a such a way that it returns exactly one row which will automatically map to your CONSUMPTION table row.

Related

Update one column where another column within same table equals a column in another table

I have a Query for retrieving data from one table where one column equals one specific value and one column equals a column in another table.
Like this:
SELECT a.IdOrdre, b.FR_Ordre
FROM Ordre a
INNER JOIN OrdreKategori b ON a.IdOrdre=b.FR_Ordre
WHERE b.FR_Kategori=57
I now would like to update the column FR_Kategori to another value than 57. How would I structure the UPDATE Query in order to achieve this?
Since you didn't tag your DBMS, and every one has different syntax for UPDATE..JOIN , here is an ANSI-SQL answer:
UPDATE OrdreKategori t
SET t.fr_kategori = Other_Val
WHERE EXISTS(SELECT 1 FROM Ordre s
where s.IdOrdre=t.FR_Ordre)
AND t.fr_kategori = 57

How to update table field value in one table from field value in another table

I am trying to update field value from one table to another.
Item with bomRev='A' in Destination table look like show below
Same Item bomRev='A' in source table looks like
I want to update partid field in destination table for bomRev=A by the value in Source filed i want to destination looks exactly like the source.
I tried this but no luck
UPDATE [MIBOMD]
SET [MIBOMD].[partId] = [assy].[partId]
FROM [MIBOMD] INNER JOIN [assy] ON [MIBOMD].[partId] = [assy].[partId]
WHERE bomRev='A' and [bomItem]='600797' AND [MIBOMD].[partId]!=[assy].[partId];
UPDATE m
SET [partId] = a.[partId]
FROM
[MIBOMD] m
INNER JOIN
[assy] a
ON m.[bomItem] = a.[ItemId]
AND m.bomEntry = a.bomEntry
WHERE
m.bomRev='A'
AND m.[bomItem]='600797'
AND m.[partId]!=a.[partId];
You actually were pretty close! Just a couple of key differences. Before I explain I have used Table Aliases in the code I provided it is a shorthand way of referring to the table throughout the query that will make it a little easier to follow and read. To Alias a table after the table name in the from statement simply add a space and an alias or a space " AS " alias.
Now your Join as on partid in your version and that was your main issue. Because you want the records where partid are not the same so you can change the partid of the assy table. Looking at your dataset I was able to determine that the shared key was mibomd.bomItem and assy.ItemId. After clearing that up everything should be good.
Per your comment the only other thing that needed to be added was a second condition on the join to make it unique. [MIBOMD].bomEntry = assy.bomEntry
A little about join conditions. Typically you always want to figure out what the unique relationship between the 2 tables are (bomItem = ItemId and bomEntry = bomEntry) and that is what will go in the ON area of the join. Rarely that will be different and will be for very specific purposes.
Per your comment on how to insert the missing records
INSERT INTO MIBOMD (bomItem, bomRev, bomEntry, lineNbr, dType, partId)
SELECT
bomItem = a.ItemId
,bomRev = 'A' --or change the value to what you want
,a.bomEntry
,lineNbr = ???? --not sure how you are figure this out do if you wan it to be the next line number you can figure that out automatically if you need
,a.partId
FROM
assy a
LEFT JOIN MIBOMD m
ON a.ItemId = m.bomItem
AND a.bomEntry = m.bomEntry
WHERE
m.bomItem IS NULL
This time you would use a left join from assy to mibomd and figure out when they don't match mibomd.bomItem IS NULL

Need a count query in access but one column only needs unique values

I have a table 'tblBaseResults' setup like this
Column 1 = Name
Column 2 = Activity
The Activity column is supposed to be unique. However there are multiple Name fields. So im not sure how to get the Activity column to unique values and keep just one value from Name.
I would prefer to just keep the values from name that DO NOT have any '?' characters.
The current Count query i have works great. However i need only ONE of the Name fields to carry over with it
SELECT tblBaseResults.Activity, Count(tblBaseResults.Activity) AS CountOfActivity INTO tblCountResults
FROM tblBaseResults
GROUP BY DISTINCT tblBaseResults.Activity;
In Excel if i do a VLookup i get what i need, but want to do it in access.
INSERT INTO tblCountResults (Activity, Name, CountOfActivity)
SELECT Activity, min(Name), Count(tblBaseResults.Activity)
FROM tblBaseResults
WHERE instr(Name, '?') = 0
GROUP BY Activity

SQL INNER JOIN vs. WHERE ID IN(...) not the same results

I was surprised by the outcome of these two queries. I was expecting same from both. I have two tables that share a common field but there is not a relationship set up. The table (A) has a field EventID varchar(10) and table (B) has a field XXNumber varchar(15).
Values from table B column XXNumber are referenced in table A column EventID. Even though XXNumber can hold 15 chars, none of the 179K rows of data is longer than 10 chars.
So the requirement was:
"To avoid Duplicate table B and table A entries, if the XXNumber is contained in a table A >“Event ID” number, then it should not be counted."
To see how many common records I have I ran this query first - call it query alpha"
SELECT dbo.TableB.XXNumber FROM dbo.TableB WHERE dbo.TableB.XXNumber in
( select distinct dbo.TableA.EventId FROM dbo.TableA )
The result was 5322 rows.
The following query - call it query delta which looks like this:
SELECT DISTINCT dbo.TableB.XXNumber, dbo.TableB.EventId
FROM dbo.TableB INNER JOIN dbo.TableA ON dbo.TableB.XXNumber= dbo.TableB.EventId
haas returned 4308 rows.
Shouldn't the resulting number of rows be the same?
The WHERE ID IN () version will select all rows that match each distinct value in the list (regardless of whether you code DISTINCT indide the inner select or not - that's irrelevant). If a given value appears in the parent table more than once, you'll get multipke rows selected from the parent table for that single value found in the child table.
The INNER JOIN version will select each row from the parent table once for every successful join, so if there are 3 rows in the child table with the value, and 2 in the parent, then there will be 6 rows rows in the result for that value.
To make them "the same", add 'DISTINCT' to your main select.
To explain what you're seeing, we'd need to know more about your actual data.

SQL query to select * from table, but return ID column as looked-up usernames

I'm using Bugzilla, and I essentially want to SELECT * FROM bugs table in the "bugs" database. However, the "assigned_to" column actually contains integer values (IDs) instead of a string with the user name.
These IDs match primary keys in the "profiles" table (the "userid" column), and the string I want my query to return is actually stored in the "realname" column in that table.
How can I modify this query to capture all columns in "bugs," but perform a lookup on the assigned_to column and return usernames?
SELECT b.*, p.realname FROM bugs b
JOIN profiles p
ON b.assigned_to = p.userid