I want to sum Amount and Count No. of rows in mysql - mysql

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

Related

SQL query to join rows with same values

I've come across a problem that I don't really know how to solve.
I have a table which looks somewhat like this:
ID Name Price Quantity
1 BookA 5 10
2 BookB 10 15
3 BookA 15 15
4 BookA 5 25
How could I join rows which have same Name, same Price and sum Quantity? So it would look like this:
ID Name Price Quantity
1 BookA 5 35
2 BookB 10 15
3 BookA 15 15
Thank you in advance!
This is just a simple GROUP BY query:
SELECT Min(ID), Name, Price, Sum(Quantity) as Quantity
FROM yourtable
GROUP BY Name, Price;

How to sort & group by sql query?

I have Data
id Date CatId itemid itemname price
1 10/5/2019 1 1 ABC 20
2 10/5/2019 1 2 XYZ 30
3 10/5/2019 2 1 ABC 20
4 10/5/2019 3 1 ABC 20
5 11/5/2019 1 2 XYZ 30
6 11/5/2019 2 1 ABC 20
7 11/5/2019 2 3 PQR 40
8 12/5/2019 3 1 ABC 20
9 12/5/2019 3 2 XYZ 30
10 12/5/2019 1 2 XYZ 30
11 12/5/2019 2 1 ABC 20
expected result data
date CatId toal
10/5/2019 1 50
10/5/2019 2 20
10/5/2019 3 20
11/5/2019 1 30
11/5/2019 2 60
12/5/2019 1 30
12/5/2019 2 20
12/5/2019 3 50
I want result order by date and after sum of price group by catid,
I have tried multiple query applied but get exact solution. I have spent so much time.
I have tried bellow queries
SELECT * FROM (SELECT * FROM `table` ORDER BY `date` DESC) as tbl
GROUP BY tbl.`catid`
SELECT *
FROM `table`
GROUP BY `date` ORDER BY `date` DESC
SELECT *
FROM (
SELECT * FROM `table`
ORDER BY `date` DESC
) AS sub
GROUP BY `catid` ORDER BY `date` DESC
the columns you can use in order by and group by can be different
You could use group by and sum directly
select date, catid, sum(price)
from my_table
group by date, catid
order by date, sum(price), catid

MYSQL- Count set of data as 1 (Not Like Distinct)

I have a table like this for customer order and feedback some questions.table_name:customer_database
----------
ID NAME PHONE ORDER_NO ORDER_VALUE ANSWER DATE
----------
1 RAM 873 5 500 Super 1/1/2019
2 RAJ 876 1 400 super 1/1/2019
3 RAM 873 5 500 Bad 1/1/2019
4 RAM 873 2 100 Good 30/12/2018
I want the result as counting the same set of unique date row as 1 like
----------
ID NAME PHONE Total_visit total_order
----------
1 RAM 873 2 600
2 RAJ 876 1 100
i want to count as 1 for a set of answer like 1 and 3.
It is actually very simple group by query
select NAME, Phone count(*) total_visit, sum(order_value) total_order
from your_Table
group by NAME, Phone
Finally, I found the answer to my own by an inner query
SELECT NAME,PHONE,count(PHONE) as total_visit,SUM(ORDER_VALUE) as total_order_value
from (SELECT NAME,PHONE,ORDER_VALUE FROM customer_database GROUP BY PHONE,DATE) as qry
GROUP BY PHONE

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 clause taking too much time

Suppose I have following table (MYTable)
ID joindate AnyText deptid
1 2013/10/10 ABC 10
2 2013/10/11 XYZ 20
3 2013/10/10 PQR 10
4 2013/10/14 MNO 30
5 2013/10/11 UVW 20
Now I want result like this:
ID joindate deptid count(*)
1 2013/09/21 10 2
2 2013/09/22 20 2
4 2013/09/24 30
I am using mysql. my table have thousands of record . when i am applying group by it is taking 6-7 second how can i optimize it..
select id,latestdate,deptid,count(*) from MyTable
where joindate>=now()-interval 30 day group by deptid