I am trying to run an update query based on 2 fields in a seperate table. I know how to do it based on one field, add the two tables, create a join between the two related fields and run the update. However when I try and run it with two joins it says it cannot execute because it contains ambiquous joins. Here's a brief example of what I'm trying to achieve
Table 1 contains name, location and number of items.
Table 2 contains name, location and and empty field for the number of items.
When i try update table 2 with the information from table 1, with a join between the 2 name fields, it updates the same number of items for each different location.
UPDATE:
I've fixed it, I think I was linking the joins incorrectly.
Here's the finished SQL statement:
UPDATE Tbl_Hourly_Pick_Performance
LEFT JOIN Tbl_Temp_Count_Info
ON (Tbl_Hourly_Pick_Performance.[Sign On]=Tbl_Temp_Count_Info.[Picker ID])
AND (Tbl_Hourly_Pick_Performance.[Pick Floor]=Tbl_Temp_Count_Info.Floor)
SET Tbl_Hourly_Pick_Performance.[No of Stores] = Tbl_Temp_Count_Info.Count;
Related
I have two tables. The first is named master_list. It has these fields: master_id, item_id, name, img, item_code, and length. My second table is named types_join. It has these fields: master_id and type_id. (There is a third table, but it is not being used in the queries. It is more for reference.) I need to be able to combine these two tables so that I can sift the results to only show certain ones but part of the information to sift is on one table and the other part is on the other one. I don't want duplicate answers.
For example say I only want items that have a type_id of 3 and a length of 18.
When I use
SELECT * FROM master_list LEFT JOIN types_join ON master_list.master_id=types_join.master_id WHERE types_join.type_id = 3 AND master_list.length = 18"
it finds the same thing twice.
How can I query this so I won't get duplicate answers?
Here are the samples from my tables and the result I am getting.
This is what I get with an INNER JOIN:
BTW, master_id and name both only have unique information on the master_list table. However, the types_join table does use the master_id multiple times later on, but not for Lye. That is why I know it is duplicating information.
If you want unique rows from master_list, use exists:
SELECT ml.*
FROM master_list ml
WHERE ml.length = 18 AND
EXISTS (SELECT 1
FROM types_join tj
WHERE ml.master_id = tj.master_id AND tj.type_id = 3
);
Any duplicates you get will be duplicates in master_list. If you want to remove them, you need to provide more information -- I would recommend a new question.
Thank you for the data. But as you can see enter link description here, there is nothing wrong with your query.
Have you tried create an unique index over master_id, just to make sure that you do not have duplicated rows?
CREATE UNIQUE INDEX MyMasterUnique
ON master_list(master_id);
I have a query i have been working on trying to get a specific set of data, join the comments in duplicate phone numbers of said data, then join separate tables based on a common field "entry_id" which also happens to be the number on the end of the word custom_ to pull up that table.
table named list and tables containing the values i want to join is custom_entry_id (with entry_id being a field in list in which i need the values of each record to replace the words in order to pull up that specific table) i need entry_id from the beginning part of my query to stick onto the end of the word custom for every value my search returns to get the fields from that custom table designated for that record. so it will have to do some sort of loop i guess? sorry like i said I am at a loss at this point
this is where i am so far:
SELECT * ,
group_concat(comments SEPARATOR '\r\n\r\n') AS comments_combined
FROM list WHERE `status` IN ("SALEA","SALE")
GROUP BY phone_number
//entry_id is included in the * as well as status
// group concat combines the comments if numbers are same
i have also experimented on test data with doing a full outer join which doesnt really exist. i feel if you can solve the other part for me i can do the joining of the data with a query similar to this.
SELECT * FROM test
LEFT JOIN custom_sally ON test.num = custom_sally.num
UNION
SELECT * FROM test
RIGHT JOIN custom_sally ON test.num = custom_sally.num
i would like all of this to appear with every field from my list table in addition to all the fields in the custom_'entry_id' tables for each specific record. I am ok with values being null for records that have different custom fields. so if record 1 has custom fields after the join of hats and trousers and record 2 has socks and shoes i realize that socks and shoes for record 1 will be null and hats and trousers for record 2 will be null.
i am doing all this in phpmyadmin under the SQL tab.
if that is a mistake please advise as well. i am using it because ive only been working with SQl for a few months. from what i read its the rookie tool.
i might be going about this all wrong if so please advise
an example
i query list with my query i get 20,000 rows with columns like status, phone_number, comments, entry_id, name, address, so on.
now i want to join this query with custom fields in another table.
the problem is the custom tables' names are all linked to the entry_id.
so if entry_id is 777 then the custom table fields are custom_777
my database has over 100 custom tables with specials fields for each record depending on its entry_id.
when i query the records I don't know how to join the custom fields that are entry_id specific to the rest of my data.i will pull up some tables and data for a better example
this is the list table:
this is the custom_"entry_id"
Full Outer Join in MySQL
for info on full outer joins.
I have two tables that have different data that I need to merge. They do have similarities such as: Order number, Name, type or product. But they have separate data as well like: Order date, and Engravings.
Would I do two separate Append queries in Access into a merged table? Or one Append queries? Or just keep the data separate?
I am new to Access and trying to find the best way to approach this.
Merging the two tables into one completely defeats the purpose of using a database and you're better off using excel at that point. You want to split the data as much as possible along logical lines so that you can find, say... all the orders that Mr X has ever made for a specific product. And in that case you're going to want to have separate tables for customers, orders, engravings and the like.
The best practice from a design standpoint is to place fields that each table has in common into a third "master" table, then create relationships from that table to the existing tables and delete the data that has been transferred to the main table (except for the primary keys, which have to be common with your master table).
To create the master table, use a Make Table query to generate the master table based on one of your tables, then an append query to add any products in the master table that might not be common to both, based on the other table. Finally, delete queries for each table would rid you of redundant data in both original tables.
However, I strongly suggest you use Microsoft's tutorials and download the NorthWind sample database so you can get an idea of what a properly structured database looks like. The beginner's learning curve for access is very steep and having well built example databases is almost a requisite.
Make a backup of your database(s) and play with it until it turns out right. Do not make the mistake of playing with live data until you know what you're doing.
As you have similar fields on either table, take the Order number field from both tables using a union query. Something like:
SELECT tbl_Delivery_Details.OrderNo
FROM tbl_Delivery_Details
GROUP BY tbl_Delivery_Details.OrderNo
UNION
SELECT tbl_Delivery_Header.[Order number]
FROM tbl_Delivery_Header
GROUP BY tbl_Delivery_Header.[Order number];
This would take the order numbers from the delivery details table and from the delivery header table and merge them into one list with only one instance of each order number. Save the query.
You could then use this query in a new query. Bring in your 2 tables to this query and insert the fields from either table that you require.
As users add records to the tables they will be added to the union selet query when it is next run.
PB
It depends on what you want to do. Let's assume you have tables A (with 50 records) and B (with 75) records, and both tables have a similar column called OrderID.
Appending Rows:
If you want to create a table with 125 total records by combining records (rows) from A and records (rows) from B, run the following two queries:
Query 1:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1 as DATA INTO C
FROM A;
Query 2:
INSERT INTO C ( ORDER_NUMBER, DATA )
SELECT B.ORDER_NUMBER, B.TEXT_FIELD2
FROM B;
Appending Columns: If you want to create a table with 75 total records where you are appending columns from A to the columns in B, then run the following query:
SELECT B.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A RIGHT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
... in a similar way, you can append columns in B to columns in A in a new table C with a total of 50 records by running the following query:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A LEFT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
So this is my Select Query called qryHoursSUM.
SELECT DAL.Project
, DAL.Unit
, DAL.Activity
, Sum(([EndTime]-[StartTime])*24) AS Hours
FROM DAL INNER JOIN UnitTbl
ON (DAL.Project=UnitTbl.Project) AND (UnitTbl.Unit=DAL.Unit)
GROUP BY DAL.Project, DAL.Unit, DAL.Activity;
I'm trying to get it to update to the corresponding *** Actual field and matching row in my Table called UnitTbl, which has the Project and Unit fields as a composite primary key. I cant seem to get it to work. I've first tried running a update query based on the select query but received an "Operation must use an updateable query", and found out that its not possible. I've made a make table query call qrySumHoursTbl to turn the qryHoursSUM into a table
SELECT qryHoursSUM.* INTO SumHoursTbl
FROM qryHoursSUM;
Then created an update query called qryUpdateUnitTbl to just update the 002 Actual field
UPDATE SumHoursTbl INNER JOIN UnitTbl
ON (SumHoursTbl.Unit = UnitTbl.Unit)
AND (SumHoursTbl.Project = UnitTbl.Project)
SET UnitTbl.[002 Actual] = [Hours]
WHERE (([Activity]="002 Drafting Submittals"));
Is it possible to have an update query to update multiple fields at once or would I have to make a update for each field i want to update?
Also, for some reason it only update the UnitTbl with whole numbers only, I need to have it show up to 2 decimal places. After working with so many copies, I completely forgot to change the field size to double. After changing that, its showing up to 2 decimal places
My end goal is to be able to let a user click on a button that shows a variance report of hours for each units Budgeted hours and Actual hours (which would be [*** Budget] - [*** Actual]) Any tips for things to consider along the way to get there is appreciated, I've trying to figure this out for a week now.
I couldn't think of a better way to ask this question, but here goes:
I have 2 tables. The first is a table of items for label printing jobs. We'll call it pj_items The columns are simply: job_id, part_num and qty
The second table is a list of finishes called PartFinishes on parts with just 2 columns of: PartNumber and FinishId
When I want to retrieve information for the label printer I use:
select pj_items.part_num, pj_items.qty, PartFinishes.FinishId from pj_items
join PartFinishes on PartFinishes.PartNumber = pj_items.part_num
where job_id = 1
This query works fine and gives me the right result when each part has only one finish. The problem is when there are 2 or more finishes on a part.
For example: The part 99401326 has 2 finishes. On my pj_items table I have one row for that part so I can print one label. When I use this query it returns 2 rows for that part each with a different FinishId Unfortunately that would result in 2 labels printed. I need both of those finishes on one row since I only want one label printed with both FinishId's present.
I had a similar query which returned the finishes as new columns (f1,f2,f3) But it would still return 2 rows in the given example with f1 defined in the first row and f1 as null in the second row and f2 being defined in the second.
How can I get that into one row?
Hopefully that makes sense, anyone have any suggestions?
If you need anymore info, let me know.
Thanks!
EDIT:
The pj_items table may have duplicate entries and I need to preserve those as separate rows. I just need to have all the finishes for each part in one row with its respective part.
Example
Here is an example of the pj_items table:
Here is the relevant information from the PartFinishes table:
And here is the result using the original query:
What I need in the result is the two rows of 99401326 are one with both of the finishes in that row, while maintaining the 2 seperate rows for the 99401077 since I want 2 labels of those.
You can use GROUP_CONCAT to combine all finish ID's for a given part into one column.
SELECT pj_items.part_num,
pj_items.qty,
GROUP_CONCAT(PartFinishes.FinishId) as FinishIds
FROM pj_items
JOIN PartFinishes
ON PartFinishes.PartNumber = pj_items.part_num
WHERE job_id = 1
GROUP BY PartFinishes.PartNumber
#comment:
However if you want to keep rows from pj_items separated you will need to join PartFinishes already grouped by PartNumber:
SELECT pj_items.part_num,
pj_items.qty,
FinishesGrouped.FinishIds
FROM pj_items
JOIN
( SELECT PartNumber, GROUP_CONCAT(FinishId) as FinishIds
FROM PartFinishes
GROUP BY PartNumber ) FinishesGrouped
ON
pj_items.part_num = FinishesGrouped.PartNumber