update table query from other table - sql

I have the following problem:
In my database I have 1st table : item_price
Id_item Dept Price
1 7 1500
2 7 100
3 7 200
4 7 300
5 7 400
6 7 0
7 7 0
8 7 0
9 7 0
10 7 0
And I have 2nd table : Order_history
Order_no Id_item Dept Price Date_order
A1 1 2 700 01/05/2014
A2 2 3 800 02/21/2014
A3 3 7 200 03/25/2013
A4 3 7 300 04/15/2014
A5 4 7 300 05/05/2014
A6 5 7 400 06/15/2014
A7 6 7 120 07/16/2014
A8 7 7 1400 08/19/2014
A9 8 7 150 09/25/2014
A10 9 7 4500 10/31/2014
A11 10 7 8000 11/15/2014
All I want to do now is:
I want to update field price in item_price WHERE Id_item in item_price = Id_item in Order History
dept in item_price = dept in order_history AND
Date_Order = 01/05/2014 - 11/07/2014
something like ===
update item_price
set price = (select item_number from item_history )
where date_order = '01/01/2014' - '11/07/2014' and dept = '7';
Can you guys help me?

Oracle does provide support for updates with joins. What you can do is something like this (note I have not tested this):
MERGE
INTO item_price
USING (
SELECT ip.Id_item AS rid, oh.price as hist_price
FROM item_price ip
JOIN Order_history oh
ON oh.id_item = ip.id_item
WHERE
oh.date_order between TO_DATE('01/01/2014') and TO_DATE('11/07/2014')
)
ON (Id_item = rid)
WHEN MATCHED THEN
UPDATE
SET price = hist_price;
MySQL syntax would be something like this:
UPDATE item_price ip
JOIN Order_history oh ON oh.id_item = ip.id_item
WHERE
oh.date_order between
STR_TO_DATE('01/01/2014', '%m/%d/%Y') and
STR_TO_DATE('11/07/2014', '%m/%d/%Y')
SET ip.price = oh.price;

SQL
Test on Dev First and verify results! Please
UPDATE item
SET item.Price = history.price
FROM dbo.item_price item
JOIN dbo.Order_History history ON item.Id_Item= history.Id_Item AND item.Dept = History.Dept
WHERE history.Date_Order Between '01/05/2014' AND '11/07/2014'

Related

MS-Access - Merge data between two tables base on conditions

I am using MS Access and I am trying to create a query between two tables and merge same rows base on:
cust_id = cust_id and
a_date = f_date and
price = paid
and have the desire output.
My data now:
tblapp
app_id cust_id a_date price a_memo
------------------------------------------
1 1 10/10/20 20 hello
2 1 11/10/20 10 bye
3 2 12/10/20 30 hi
4 2 12/10/20 30 text
5 2 12/10/20 30 lol
6 2 12/10/20 30 ciao
7 3 14/10/20 25 peace
tblfin
fin_id cust_id f_date paid
----------------------------------
1 1 10/10/20 20
2 1 11/10/20 10
3 1 11/10/20 10
4 2 12/10/20 30
5 3 14/10/20 25
As you can see,
cust_id = 1 on 10/10/20 with bill 20 and paid 20
cust_id = 1 on 11/10/20 with bill 10 and paid 10 + 10
cust_id = 2 on 12/10/20 with bill 30 + 30 + 30 + 30 and paid 30
cust_id = 3 on 14/10/20 with bill 25 and paid 25
Derire query output:
app_id cust_id a_date price a_memo fin_id cust_id f_date paid
----------------------------------------------------------------------
1 1 10/10/20 20 hello 1 1 10/10/20 20
2 1 11/10/20 10 bye 2 1 11/10/20 10
3 2 12/10/20 30 hi 4 2 12/10/20 30
7 3 14/10/20 25 peace 5 3 14/10/20 25
Tried the following sql but i am getting duplicates(like cust_id 1 and 2 where data rows are not the same in two tables):
SELECT f.fin_id,
f.cust_id,
f.f_date,
f.paid,
a.app_id,
a.cust_id,
a.a_date,
a.price,
a.a_memo
FROM tblfin AS f
LEFT JOIN tblapp AS a ON (f.cust_id=a.cust_id)
AND (f.f_date=a.a_date)
AND (f.paid=a.price);
Solution using MySQL is welcome. Thank you.

SQL return row when sum value is null

I got two tables. One with a bill of material and one with purchasing orders. Now I want to display the full bill of material of a product with the total on order amounts from the table purchasing.
**Billofmaterials**
BOM_ID BOM_Prod_id BOM_item_id
1 5 11
2 5 13
3 6 11
4 6 15
5 6 20
Example prod_id (product id) 6 has 3 items (id 11, 15 and 20).
**Purchasing**
PU_ID PU_item_id PU_amount PU_status
1 11 100 On order
2 11 650 On order
3 11 40 Received
4 20 600 On order
5 8 10 On order
6 15 150 Received
Now i got the following SQL
SELECT
BOM_item_id,
SUM(DISTINCT purchasing.PU_amount) as total_on_order
FROM Billofmaterials
LEFT JOIN purchasing
ON Billofmaterials.BOM_item_id= purchasing.PU_item_id
AND purchasing.PU_status != 'Received'
AND BOM_prod_id = 6
GROUP BY BOM_item_id
This query returns the following:
**Query result**
BOM_item_id total_on_order
11 750
20 600
Because there is only one received purchase order for BOM_item_id 15 it doesn't return a value. Now i want to retun BOM_item_id 15 also but with a total_on_order as 0 like:
**Expected result**
BOM_item_id total_on_order
11 750
15 0
20 600
What SQL feature/function do I need to use to get the expected result?
You can try the below -
SELECT BOM_item_id,coalesce(total_on_order,0) as total_on_order
FROM Billofmaterials left join
(
select PU_item_id,SUM(purchasing.PU_amount) as total_on_order
from purchasing
where purchasing.PU_status != 'Received'
group by PU_item_id
) purchasing
ON Billofmaterials.BOM_item_id= purchasing.PU_item_id
where BOM_prod_id = 6

SQL Temp table Array to perfrom rolling caluclations

I wish to use some sort of SQL array to subtract values from a certain row (QTYOnHand) that decreases that row value every time and throws it into a rolling calculation for the other rows. I've been thinking of some sort of Self Join/Temp Table solution, but not sure how to formulate. Also, All the results will be partitioned by the ItemID below. Help would be appreciated.
Here's some data, If I do a simple row by row subtraction I will get this: 17-3 = 14, 17-5 = 12 and so on.
(Item_ID) (ItemQty) (QTYOnHand) (QtyOnHand - ItemQty)
123 3 17 14
123 5 17 12
123 4 17 13
456 7 12 5
456 8 12 4
456 2 12 10
456 3 12 9
789 2 6 4
789 2 6 4
789 2 6 4
These are the results that I want, where I subtract every next value from the new QTYOnHand-ItemQty column value. Looks like 17-3 then 14 -5 then 9 -4 for Item_ID (123):
(Item_ID) (ItemQty) (QTYOnHand) (QtyOnHand - ItemQty)
123 3 17 14
123 5 17 9
123 4 17 5
456 7 12 5
456 8 12 -3
456 2 12 -5
456 3 12 -8
789 2 6 4
789 2 6 2
789 2 6 0
try the following:
;with cte as
(
select *, ROW_NUMBER() over (partition by Item_ID order by Item_ID) rn
from YourTable
)
, cte2 as
(
select Item_ID, ItemQty, QTYOnHand, Case when rn = 1 then QTYOnHand else 0 end - ItemQty as calc, rn
from cte
)
select Item_ID, ItemQty, QTYOnHand, sum(calc) over (partition by Item_ID order by rn) as [QtyOnHand - ItemQty]
from cte2 t1
Please find the db<>fiddle here.

How to update a table with data from same table in sql [duplicate]

This question already has answers here:
SQL How to Update SUM of column over group in same table
(3 answers)
Closed 2 years ago.
I have a table in SQL which I want to update
NAME Emp_ID Points TotalPoints
ABC 1 50 0
ABC 1 40 0
XYZ 2 20 0
LMN 3 30 0
LMN 3 50 0
XYZ 2 10 0
LMN 3 5 0
Please help me to update the same table as shown below by summing up the points
NAME Emp_ID Points TotalPoints
ABC 1 50 90
ABC 1 40 90
XYZ 2 20 30
LMN 3 30 85
LMN 3 50 85
XYZ 2 10 30
LMN 3 5 85
I only tried the below SQL on Oracle 18c database, but I believe it is fairly standard SQL and so should work with all the major DBMS
update EMPS E1
set E1.TOTAL_POINTS = (select sum(E2.POINTS)
from EMPS E2
where E2.EMP_ID = E1.EMP_ID)
Try the following, here is the DEMO. This code work for MySQL 8.0, SQL Server and PostgreSQL.
select
name,
emp_id,
points,
sum(points) over (partition by emp_id) as total_points
from yourTable
order by
name

Finding double mrp's in SQL

I have table with productcode and mrp like
Pcode MRP
1 30
2 30
2 35
3 100
4 150
4 150
5 45
6 120
6 122
6 125
I want to find which productcodes have more than two mrp.
Thanks in advance.
If you get the count and use the having clause, you should get what you are looking for.
select pcode, count(pcode)
From tab
group by pcode
having count(pcode) > 1