MySQL query using Update, Sum, Where, and Group By - mysql

I have a table below named deposit
dep_id
deposit_amount
comp_id
1
100
1
2
100
1
3
300
2
4
200
2
5
100
2
6
500
3
When I update the table with the query below I get the following table, which is not what I want
UPDATE deposit
SET deposit_amount = (SELECT SUM(deposit_amount) - 50)
WHERE comp_id =1
What the query above does is to subtract 50 from each of the corresponding comp_id
dep_id
deposit_amount
comp_id
1
50
1
2
50
1
3
300
2
4
200
2
5
100
2
6
509
3
But the table below is what I need.
Because seeing the first table and with the query I provided where comp_id =1, we have 100 + 100 = 200, and then 200 - 50 = 150. So because comp_id has 1 IDs two times, therefore we have 75 and 75 because 75 +75 is 150. So we have the table below, which is what I need.
dep_id
deposit_amount
comp_id
1
75
1
2
75
1
3
300
2
4
200
2
5
100
2
6
500
3
The amount supposed to be evenly split amongst the deposits that share a comp_id, even if they weren't before.
Please how do I write the query to suit the table I need? Help!

You can divide the 50 by the count of the records for this id:
UPDATE deposit d1,
(SELECT *, count(*) over (partition by d2.comp_id) as c FROM deposit d2) x
SET d1.deposit_amount = (SELECT SUM(d1.deposit_amount) - 50/x.c)
WHERE d1.comp_id =1
see: DBFIDDLE

Related

Update the mysql field according to the sorting

a simple task, but I can't figure it out. There is a Table:
id
name
sum
place
1
Alex
210
0
2
Bob
250
0
3
Sam
190
0
4
Bill
290
0
5
Jack
210
0
I need to UPDATE the PLACE
according to the maximum SUM and then the id
Those are the request :
SELECT id, name, sum, place FROM tableORDER BYsumDESC,id ASC;
According to this request, update the semi PLACE from 1++
Those places 1, 2, 3 and further throughout the table
Like this:
id
name
sum
place
1
Alex
210
3
2
Bob
250
2
3
Sam
190
5
4
Bill
290
1
5
Jack
210
4
SET #n_place := 0;
UPDATE `table` SET `place` = #n_place := #n_place + 1 ORDER BY `sum` DESC, `id` ASC;

How to add(sum) the column data in SQL

ID pcID contractor approver claimed
-------------------------------------------
1 1 one 1000 900
2 1 two 200 100
3 1 three 1000 1000
4 1 six 100 11
5 2 six 100 22
6 3 six 120 1
7 4 three 102 10
From the above table I need to sum the approver amount and claimed amount based on the contractor. Like the below given table. All has been done by using the stored procedure.
ID pcID contractor approver claimed TotalApprover TotalClaimed
--------------------------------------------------------------------------
1 1 one 1000 900 1000 900
2 1 two 200 100 200 100
3 1 three 1000 1000 1000 1000
4 1 six 100 11 100 11
5 2 six 100 22 200 33
6 3 six 120 1 320 34
7 4 three 102 10 1120 1001
Like the above table I need an output adding(sum) in ascending order based on contractor.
Thanks in advance.
You can use window function
select t.*,
sum(approver) over( partition by contractor order by ID) TotalApprover,
sum(claimed) over( partition by contractor order by ID) TotalClaimed,
from table1 t
You want cumulative sums:
select t.*,
sum(approver) over (partition by contractor order by claimid) as totalapprover,
sum(claim) over (partition by contractor order by claimid) as totalclaim
from t;
This is just accumulated the corresponding values by the dimensions you are asking for.

Need to derive count of occurrence in Table 2 based on the results of Occurrence in Table 1 using sql query

Table_1 has order_id, country_id details
table_ID order_id country_id
1 100 IN
2 200 USA
3 300 UK
4 400 IN
5 500 UK
6 600 UK
7 700 USA
8 800 USA
9 900 IN
10 1000 UK
Table_2 has shipment_id, order_id details
Shipment_ID order_id
1 100
2 100
3 100
4 200
5 200
6 300
7 300
8 400
9 500
11 500
12 600
13 700
14 700
15 700
16 700
17 800
18 800
19 800
20 900
21 900
22 1000
23 1000
24 1000
I used the following query to find out list of order_id which are for country_id='IN'
select `order_id`
from `Table_1`
where `country_id` = 'IN';
order_id
100
400
900
I need guidance to write the query to find the count of shipment_id which will are mapped to order_id from 'IN'
So order_id 100 has 3 shipment, 400 has 1 and 900 has 2 shipment
Desired final output
count_of_shipment_id
6
here is the query you need:
SELECT country_id, count(*) as count_of_shipment_id
FROM Table_1 a
inner join Table_2 b on a.`order_id` = b.`order_id`
group by country_id
if you need only one country you can always add "where" or "having" to filter the result.
here you can see the sample you posted:
http://sqlfiddle.com/#!9/c90424/2

Need MySQL query to sum and avrage

I have duplicate rows in table, i want sum of quantity of duplicate row and average of purchase rate of duplicate rows, let me explain you by example.
Please give me mysql query to acheive desired output.
Problem Table
===================================================
POID itemid quantity purchaserate
1 1 100 100
2 2 100 100
3 3 100 100
4 1 80 200
5 1 40 150
6 3 100 400
====================================================
Desired output
===================================================
itemid totalquantity avgpurchaserate
1 220 145.45
2 100 100
3 200 250
===================================================
Tried below query
select itemid, sum(quantity), avg(purchaserate)
from test
group by itemid
Output
itemid |sum(quantity) | avg(purchaserate)
1 | 220 | 150
2 | 100 | 100
3 | 200 | 250
You can use aggregation function as sum and avg and group by the column you need. In your case itemid
select itemid, sum(quantity) avg(purchaserate)
from my_table
group by itemid

GROUP BY using a SUM

This is a tough one to explain. I have this data set
Data set 1
counter | id
1 280
1 280
0 280
1 781
1 781
1 781
0 25
1 25
1 25
I want to GROUP BY the id, but with the SUM of counter so I end up with this data set
Data set 2
counted | id
2 280
3 781
2 25
I have tried a number of approaches but I always end up with the total SUM of rows for each ID like this
Data set 3
counted | id
3 280
3 781
3 25
EDIT
It might be worth noting that data set 1 comes from a sub query and is not a table in itself.
EDIT
I have used a query like this, it gives me the result of data set 3
select sum(counter) as counted, id
from t
group by id;
Did you do this?
select sum(counter) as counted, id
from t
group by id;