Update using data from another table - mysql

I want the minimum price from Table 2 to be filled in price column of Table 1 for a particular id.
Table 1
pid price
111 0
222 0
333 0
Table 2
pid price
111 100
111 200
222 120
222 90
333 200
333 150
Expected output: Table 1
pid price
111 100
222 90
333 150

You would do something like:
UPDATE Table1 t
SET t.price = (SELECT MIN(t2.price) FROM Table2 t2 WHERE t2.pid = t.pid);

this is query to get lowest price from table2
(SELECT price FROM table2 WHERE price = ( SELECT MIN(price) FROM table2 ) ) now you can update the table 1 ( update table1 set price="result you got from above query" where id=given id)

Related

Daily Sales from Total Sales

I have a database that looks like this:
ID
Sale_Date(YYYY-MM-DD)
Total_Volume
123
2022-01-01
0
123
2022-01-02
2
123
2022-01-03
5
456
2022-04-06
38
456
2022-04-07
40
456
2022-04-08
45
I want to get a daily sale column from Total Volume. which is just by subtracting the total volume on date x with total volume on date x-1 for each id.
ID
Sale_Date(YYYY-MM-DD)
Total_Volume
Daily_Sale
123
2022-01-01
0
0
123
2022-01-02
2
2
123
2022-01-03
5
3
456
2022-04-06
38
38
456
2022-04-07
40
2
456
2022-04-08
45
5
My initial attempt was using a rank function and self join but that didnt turn out correct.
with x as (
select
distinct t1.ID,
t1.Sale_Date,
t1.Total_volume,
rank() over (partition by ID order by Sale_Date) as ranker
from t t1 order by t1.Sale_Date)
select t2.ID, t2.ranker, t2.Sale_date, t1.Total_volume, t1.Total_volume - t2.Total_volume as Daily_sale
from x t1, x t2 where t1.ID = t2.ID and t2.ranker = t1.ranker-1 order by t1.ID;
You should use:
the LAG window function to retrieve last "Sale_Date" value
the COALESCE function to replace NULL with "Total Volume" for each first rows
Then subtract Total_Volume from the previous value of Total_Volume and coalesce if the value of the LAG is NULL.
SELECT *,
COALESCE(`Total_Volume`
-LAG(`Total_Volume`) OVER(PARTITION BY `ID`
ORDER BY `Sale_Date(YYYY-MM-DD)`), `Total_Volume`) AS `Daily_Sale`
FROM tab
Check the demo here.

Mysql SELECT when there are multiple values for the search term

I'm trying to select all user_id's from the following table (excerpt) where product_id '3' is not associated with the user_id at all.
user_id product_id status terms_id
100 3 1 10
100 22 0 0
100 402 0 20
101 22 1 10
101 68 1 0
101 120 1 20
201 22 0 0
201 3 1 10
In this example, only user_id 101 should be selected because it doesn't have product_id 3 at all. Each user_id can have multiple entries.
I've tried the following, but it incorrectly selects all the user_id's
SELECT distinct user_id FROM table WHERE product_id <> 3
How could I accomplish this? The actual table has more than 3.5 million rows.
Thanks!
You can use a NOT EXISTS subquery to check that the user has no associated row with product_id = 3:
SELECT DISTINCT user_id
FROM `table` t1
WHERE NOT EXISTS (SELECT * FROM `table` t2 WHERE t2.user_id = t1.user_id AND t2.product_id = 3)
Output
101
Demo on dbfiddle
An alternate solution is to GROUP BY user_id and to assert that the count of rows with product_id = 3 is 0:
SELECT user_id
FROM `table`
GROUP BY user_id
HAVING SUM(product_id = 3) = 0
Demo on dbfiddle

SQL Distinct Group By Query

Sample table data's
id id_order name phone price
1 4E0 A 789 $100
2 4E0 A 789 $100
3 4LK A 789 $200
4 2LP B 420 $50
5 DK2 B 420 $80
i want result be like the rows of distinct (id_order) where phone = "789"
id id_order name phone price
1 4E0 A 789 $100
3 4LK A 789 $200
or
id id_order name phone price
2 4E0 A 789 $100
3 4LK A 789 $200
I tried this but didn't get desired output:
SELECT DISTINCT (id_order), * from table_name WHERE phone= "789";
select
min(id) id,
id_order,
name,
phone,
price
from yourtable
group by id_order, name, phone, price
To always return one row per id_order - even if the name, phone or price is different - have a sub-query that returns each id_order's lowest id. JOIN with that result:
select t1.*
from table_name t1
join (select min(id) minid, id_order
from table_name
group by id_order) t2
on t1.id = t2.minid and t1.id_order = t2.id_order
where t1.phone = 789
Executes as:
SQL>select * from table_name;
id id_order name phone price
=========== ======== ==== =========== ===========
1 4E0 A 789 100
2 4E0 A 789 100
3 4LK A 789 200
4 2LP B 420 50
5 DK2 B 420 80
5 rows found
SQL>select t1.*
SQL&from table_name t1
SQL&join (select min(id) minid, id_order
SQL& from table_name
SQL& group by id_order) t2
SQL& on t1.id = t2.minid and t1.id_order = t2.id_order
SQL&where t1.phone = 789;
id id_order name phone price
=========== ======== ==== =========== ===========
1 4E0 A 789 100
3 4LK A 789 200
2 rows found

Getting Max date from multiple table after INNER JOIN

I have two following tables
table 1)
ID | HOTEL ID | NAME
1 100 xyz
2 101 pqr
3 102 abc
table 2)
ID | BOOKING ID | DEPARTURE DATE | AMOUNT
1 1 2013-04-12 100
2 1 2013-04-14 120
3 1 2013-04-9 90
4 2 2013-04-14 100
5 2 2013-04-18 150
6 3 2013-04-12 100
I want to get reault in mysql such that it take the row from table two with MAX DEPARTURE DATE.
ID | BOOKING ID | DEPARTURE DATE | AMOUNT
2 1 2013-04-14 120
5 2 2013-04-18 150
6 3 2013-04-12 100
SELECT b.ID,
b.BookingID,
a.Name,
b.departureDate,
b.Amount
FROM Table1 a
INNER JOIN Table2 b
ON a.ID = b.BookingID
INNER JOIN
(
SELECT BookingID, MAX(DepartureDate) Max_Date
FROM Table2
GROUP BY BookingID
) c ON b.BookingID = c.BookingID AND
b.DepartureDate = c.Max_date
SQLFiddle Demo
Well,
SELECT * FROM `table2` ORDER BY `DEPARTURE_DATE` DESC LIMIT 0,1
should help

MySQL Group By not producing expected result

This is my table structure:
rec_id product_id quantity quantity_in quantity_out balance stock_date status
1 2 342 NULL 17 325 2009-10-23 1
2 2 325 NULL 124 201 2009-10-23 1
3 1 156 NULL 45 111 2009-10-23 1
4 2 201 NULL 200 1 2009-10-23 1
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
All I want is the last transaction done for a given product: product_id, quantity, quantity_out and balance from this table.
Example, there are 2 transaction done for product 2 (ids 1 & 2):
final balance for product_id 2 is 0 -> stored in rec_id 5
final balance for product_id 1 is 76 -> stored in rec_id 6
Final result/output should be like this:
recid productid quantity quantityin quantityout balance stock_date status
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
You can find the latest record for each product like:
select max(rec_id) as MaxRec
from YourTable
group by product_id
Using a subquery, you can retrieve the latest rows for their product:
select *
from YourTable
where rec_id in (
select max(rec_id) as MaxRec
from YourTable
group by product_id
)
Here's a single query with no subqueries:
SELECT main.*
FROM YourTable main
LEFT JOIN YourTable newer
ON newer.product_id = main.product_id AND newer.rec_id > main.rec_id
WHERE newer.rec_id IS NULL;
You can tweak the field list however you want--make sure you select fields from main, not newer, which should be all null.