Q. Create a new column DaysTakenForDelivery that contains the date difference between Order_Date and Ship_Date.
Tables available are: orders and shipping
CREATE TABLE orders (
Order_ID int DEFAULT NULL,
Order_Date text,
Order_Priority text,
Ord_id text
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE shipping (
Order_ID int DEFAULT NULL,
Ship_Mode text,
Ship_Date text,
Ship_id text,
DaysTakenForDelivery` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Row counts:
- shipping: 7701
- orders: 5506
Please note I changed the datatyes for date columns properly.
Things I did:
I added the required column in the table 'shipping', since nothing was specified with respect to that (which table it should be added to or otherwise). Query for the same:
ALTER TABLE shipping ADD DaysTakenForDelivery INT;
Next, I tried to update the column using various queries but nothing worked. A few of them are listed below:
FAILED ATTEMPTS:
UPDATE shipping SET DaysTakenForDelivery = (
select datediff(b.ship_date, a.order_date) AS DaysTakenForDelivery
from orders a
JOIN shipping b ON a.Order_ID = b.Order_ID
);
NOTE: this query led to the following error:
Error Code: 1093. You can't specify target table 'shipping_dimen' for update in FROM clause
Next query I tried:
UPDATE shipping b SET DaysTakenForDelivery = (
select datediff(b.ship_date, a.order_date) AS DaysTakenForDelivery
from orders a
WHERE a.Order_ID = b.Order_ID
);
NOTE: this query led to the following error:
Error Code: 1242. Subquery returns more than 1 row
How am I supposed to achieve the desired result?
Please note I am using MySQL and answers for the same RDBMS would be appreciated for better understanding.
Version I am using: 8.0.31
It would be better to not store the redundant data, as there is always the risk that it becomes inconsistent, and it is just unnecessary use of storage.
It is just a normal multi-table update:
As you appear to be having performance issues while trying to update the table, you could try updating in batches:
If that still does not work you can try reducing the batch size further.
Now you have added the DDL for your tables, we can see where some of your issues are coming from. The lack of a keyed relationship between the two tables is an issue and the number of rows returned by the join tells us that the
Order_IDis not unique in yourorderstable. You need to deal with the duplicateOrder_IDs before you can move forward.To find the duplicates you can use the following queries:
After you have dealt with the duplicates, and making sure the two date fields contain valid date strings (yyyy-mm-dd), you can run something like the following to add the primary keys, the foreign key for
Order_IDinshipping, and change the datatypes of theDATEcolumns:You will still need to address the other datatype issues but without seeing sample data it is impossible to say what the other
TEXTcolumns should be changed to.You might want to do some reading about: