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;
Related
I want to join one to many table with single row on many table by limit 1 and order by create date
tbl_cart :
id fullname
1 myname1
2 myname2
3 myname3
tbl_cart_status:
id cart_id status created_at
1 1 33 2018-09-20
2 1 34 2018-09-23
3 2 34 2018-09-21
4 1 100 2018-09-25
5 2 35 2018-09-29
How can i get output with sql like this:
I want to get lastest status of my cart by ordered with created_at column
myname cart_id status created_at
myname1 1 100 2018-09-25
myname2 2 35 2018-09-29
Think filtering for this type of query:
select c.name, cs.*
from tbl_cart c join
tbl_cart_status cs
on c.id = cs.cart_id
where cs.created_at = (select max(cs2.created_at)
from tbl_cart_status cs2
where cs2.cart_id = cs.cart_id
);
i Need some help.. i have some case like this below
i have 2 table.. call ("in_table" and "out_table")
data "in_table" look like
stock_id item_id date qty_in
-----------------------------------------
1 11 2017-07-11 12
2 11 2017-07-11 10
3 12 2017-07-11 10
4 12 2017-07-19 10
And i have "out_table" is like
id_tr item_id date qty_out
-------------------------------------
1 11 2017-07-19 2
1 12 2017-07-19 1
2 11 2017-07-19 2
2 12 2017-07-19 1
And i want to combine the date and display all the data like this,
Update: the join is by item_id but i want to select by date
date item_id qty_in qty_out
---------------------------------------
2013-07-11 11 22 0
2013-07-11 12 10 0
2013-07-19 11 0 4
2013-07-19 12 10 2
Thank you for your help.
It looks like you need kind of a full outer join of two aggregate subqueries. But in your case I would get item_id and date in a union subquery (derived table) and the sums in correlated subqueries (subselect).
select item_id, date,
(select sum(qty_in) from in_table i where i.item_id = sub.item_id and i.date = sub.date) as qty_in,
(select sum(qty_out) from out_table o where o.item_id = sub.item_id and o.date = sub.date) as qty_out
from (
select item_id, date from in_table
union
select item_id, date from out_table
) sub
order by date, item_id
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 the current SQL query I am working with:
SELECT Merchant.Product, Merchant.Name, Merchant.Price
FROM a_table AS Merchant
JOIN
(
SELECT Product, MIN(Price) AS MinPrice
FROM a_table
GROUP BY Product
) AS Price
ON Merchant.Product = Price.Product
AND Merchant.Price = Price.MinPrice
From this data set:
Product Name Price
11 Merch1 19.00
11 Merch2 20.00
11 Merch3 19.00
11 Merch4 19.50
12 Merch1 20.00
12 Merch2 20.00
13 Merch1 17.00
13 Merch3 15.00
The current SQL outputs multiple product records when prices are the same like this:
Product Name Price
11 Merch1 19.00
11 Merch3 19.00
12 Merch1 20.00
12 Merch2 20.00
13 Merch3 15.00
I want to Group By product and display the lowest price with corresponding row data. If two prices are the same on a product, use first record found.
Trying to get this result:
Product Name Price
11 Merch1 19.00
12 Merch1 20.00
13 Merch3 15.00
You don't need any joins to do this.
If you are looking to get the min price for every product by merchant you can do this:
SELECT Product, Name, MIN(Price) as MinPrice
FROM a_table
GROUP BY Product, Name
If you just want the min price of a product regardless of merchant you can do this:
SELECT Product, MIN(Price) as MinPrice
FROM a_table
GROUP BY Product
Here is the query that finally worked for my needs...
http://sqlfiddle.com/#!9/c8937c/35/0
SELECT emp2.product,
emp1.name,
emp2.MinPrice
FROM (
SELECT product,
Min(price) as MinPrice
FROM Merchant
GROUP BY product
) as emp2 JOIN Merchant as emp1 ON emp1.price = emp2.MinPrice
GROUP BY product;
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