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
Related
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
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.
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
I have a table like this
id productName amount
1 Abc 10
2 xyz 20
3 Abc 10
4 Abc 10
5 Abc 10
6 Abc 10
7 xyz 20
8 xyz 20
9 xyz 20
10 xyz 20
I want to sum Amount and Count No. of rows for each products in mysql. Output like below
productName noQty totalAmount
ABC 5 50
xyz 5 100
select product_name,sum(amount)as totalamount,count(productName)as noQty
from table_name
group by productname
by using count you can get COUNT from your table and using SUM you can get sum of all same product name.
SELECT productName,
count(productName) AS noQty,
SUM(amount) AS totalAmount
FROM tableName
GROUP BY productName
select productname,count(productname) as qty,sum(amount)
from table_name
group by productname
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;