I want to select a total sales of each Category
Here is my table: (catNo = Category No. prodNo = product No.)
OrderLine:
prodNo ordNo actualPrice qty
P0001 OR001 3.00 20
P0002 OR002 3.00 2
P0003 OR003 500.00 25
Product:
prodNo prodName prodPrice prodPhoto stockQty catNo suppNo
P0001 OverPower 1500.00 OP_C4_Black.jpg 10 CAT05 S0001
P0002 Vain 300.00 Vain.jpg 5 CAT04 S0002
P0003 test 500.00 test.jpg 40 CAT05 S0001
my SQL command is
SELECT `catNo` , sum(`actualPrice`*`qty`)as `Total Sales`
FROM `orderline` , `product`
WHERE `orderline`.`prodNo` = `product`.`prodNo`
GROUP BY `orderline`.`prodNo`;
What I want is
catNo Total Sales
CAT05 12560.00
CAT04 6.00
What actual output is
catNo Total Sales
CAT05 60.00
CAT04 6.00
CAT05 12500.00
How can I display the total sales of each Category?
SELECT `catNo` , sum(`actualPrice`*`qty`)as `Total Sales`
FROM `orderline` , `product`
WHERE `orderline`.`prodNo` = `product`.`prodNo`
GROUP BY `catNo`;
Try this, I hope it helps.
Instead of grouping by product number you should be grouping by category number:
SELECT `catNo` , sum(`actualPrice`*`qty`)as `Total Sales`
FROM `orderline` , `product`
WHERE `orderline`.`prodNo` = `product`.`prodNo`
GROUP BY `Product`.`catNo`;
Group by catNo. Then you will get the desired result:
SELECT `catNo` , sum(`actualPrice`*`qty`)as `Total Sales`
FROM `orderline` , `product`
WHERE `orderline`.`prodNo` = `product`.`prodNo`
GROUP BY `catNo`
Result:
catNo Total Sales
-------------------
CAT04 6
CAT05 12560
Sample result in SQL Fiddle
Related
In MySQL 5.7 I need to calculate the average discount per merchant and day.
There are 2 tables:
produts_manufacturers:
PROD_ID | MANUFACTURER_ID | PRICE_RECOM
1 1 10
2 1 20
merchants_prices
PROD_ID | MERCHANT_ID | PRICE_ACTUAL | DATE
1 10 9.00 21-01-20
1 11 8.80 21-01-20
1 11 9.00 22-01-19
My goal is a chart that gets its metric value from group by DATE, merchant_id
Now how is it possible to calculate the average discount of all products from a manufacturer at a particular merchant without also grouping by PROD_ID?
SELECT
date,
merchant_id,
1- (t.PRICE_ACTUAL / p.PRICE_RECOM)*100 AS discount2
-- (1 - ROUND(AVG(PRICE_ACTUAL),2) / p.PRICE_RECOM)*100 AS discount
FROM
merchants_prices t
INNER JOIN produts_manufacturers p ON t.PROD_ID = p.PROD_ID
WHERE
p.MANUFACTURER_ID = 1
AND PRICE_ACTUAL IS NOT NULL
GROUP by
date,
MERCHANT_ID
ORDER BY
date desc, merchant_id
To calculate average discount per merchant and day:
SELECT `DATE`, MERCHANT_ID, ROUND(AVG(discount), 2) as discount
FROM (
SELECT mp.`DATE`, mp.MERCHANT_ID, 1- (mp.PRICE_ACTUAL / pm.PRICE_RECOM)*100 AS discount
FROM produts_manufacturers pm
JOIN merchants_prices mp ON mp.PROD_ID = pm.PROD_ID
-- WHERE pm.MANUFACTURER_ID = 1 -- If you want to restrict to a specific manufacturer
) as x
GROUP BY `DATE`, MERCHANT_ID;
With your dataset ->
DATE MERCHANT_ID discount
2021-01-20 10 -89
2021-01-20 11 -87
2022-01-19 11 -89
To help you I made a fiddle here
I have two tables. Invoices and invoice_items. I am trying to get the sum of total_payment and total quantity for each client. I tried this query:
SELECT client_id,
sum(total_amount), sum(it.quantity) as days_hired
FROM `invoices` `iv`
join invoice_items it on it.invoice_id=iv.invoice_id
group by client_id, it.invoice_id
But for client_id 14, I am getting total payment as 1908 instead of 636. Looks like the sum of this column gets repeated for every invoie_item. Any help will be appreciated.
invoices
invoice_id client_id total_payment
36 13 530
38 14 636
invoice_items
invoice_id user_id quantity
36 2 2
38 3 2
38 4 2
38 5 2
Expected output:
13 530 2
14 636 6
You can try below -
SELECT client_id, sum(total_amount) as toal_amount, sum(it.quantity) as total_quantity
FROM `invoices` `iv`
join
(
select invoice_id,sum(quantity) as quantity from invoice_items group by invoice_id
)it on it.invoice_id=iv.invoice_id
group by client_id, it.invoice_id
Try:
select i.*, ii.total_quantity
from invoices i
join (
select invoice_id, sum(quantity) total_quantity from invoice_items
group by invoice_id
) ii on i.invoice_id = ii.invoice_id
i'm trying to get a sum of a column for each month. this is what i've done.
My SQL Query :
SELECT `ID_Pilot` ,`PilotStationDate` ,`PilotWaitingTime`, ( SELECT FORMAT(SUM( `PilotWaitingTime` ),2) FROM pilot ) AS Total
FROM pilot
this is the result :
ID_Pilot PilotStationDate PilotWaitingTime Total
P001 2013-01-01 0.2 39.20
P002 2013-01-02 19.2 39.20
P003 2013-01-03 7.8 39.20
P004 2013-02-04 6.4 39.20
P005 2013-02-06 5.6 39.20
and this is the result i want :
ID_Pilot PilotStationDate PilotWaitingTime Total
P001 2013-01-01 0.2 27.20
P002 2013-01-02 19.2 27.20
P003 2013-01-03 7.8 27.20
P004 2013-02-04 6.4 12.00
P005 2013-02-06 5.6 12.00
note: 27.20 in Total column is a sum of PilotWaitingTime in January and 12.00 is a sum of PilotWaitingTime in February
Thank you for your help
This query will return the sum for each month:
SELECT
DATE_FORMAT(PilotStationDate, '%Y-%m') as year_month,
FORMAT(SUM( `PilotWaitingTime` ),2) as total
FROM pilot
GROUP BY
DATE_FORMAT(PilotStationDate, '%Y-%m')
then you just can join back this query with the pilot table:
SELECT
p.`ID_Pilot`,
p.`PilotStationDate`,
p.`PilotWaitingTime`,
t.total
FROM
pilot p INNER JOIN (
SELECT
DATE_FORMAT(PilotStationDate, '%Y-%m') as year_month,
FORMAT(SUM( `PilotWaitingTime` ),2) as total
FROM pilot
GROUP BY
DATE_FORMAT(PilotStationDate, '%Y-%m')
) t ON DATE_FORMAT(p.PilotStationDate, '%Y-%m') = t.year_month
Your pilot table data would be helpful to provide the perfect query. But you can try these two queries.
SELECT `ID_Pilot` ,`PilotStationDate` ,`PilotWaitingTime`, ( SELECT FORMAT(SUM( `PilotWaitingTime` ),2) FROM pilot p2 where p2.ID_Pilot = p1.ID_Pilot ) AS Total
FROM pilot p1;
OR
SELECT `ID_Pilot` ,`PilotStationDate` ,`PilotWaitingTime`, FORMAT(SUM( `PilotWaitingTime`),2) AS Total
FROM pilot GROUP BY ID_Pilot;
Here's the query you could try out (Example http://sqlfiddle.com/#!9/96667/1)
select pilot.*, totals.tot
from pilot
inner join
(
select
year(pilotstationdate) as yr,
month(pilotstationdate) as mth,
sum(pilotwaitingtime) as tot
from pilot
group by
year(pilotstationdate),
month(pilotstationdate)
) totals
on year(pilot.pilotstationdate) = totals.yr
and month(pilot.pilotstationdate) = totals.mth
select p.id_pilot, p.pilotstationdate, p.pilotwaitingtime,
(select sum(pilotwaitingtime)
from pilot
where last_day(pilotstationdate)=last_day(p.pilotstationdate)) total
from pilot p
last_day() is a mysql function that returns the last day of the month.
I currently have a prices table with the following layout:
id codename price discount timestamp
1 1234 599 50 2015-06-10 00:00:00
2 1234 1099 25 2015-06-11 00:00:00
3 3344 199 33 2015-06-12 00:00:00
4 5565 2499 0 2015-06-13 00:00:00
5 5565 1299 50 2015-06-14 00:00:00
I need an SQL query that will give me a single row for each codename. Each row must contain the codename, then the lowest price (along with the associated discount and timestamp for that price), as well as the latest timestamp (again with the associated price and discount for that timestamp)
Desired output:
codename minTimePrice minTimeDis minTime latestPrice latestPriceDis latestPriceTime
1234 599 50 2015-06-10 00:00:00 1099 25 2015-06-11 00:00:00
3344 199 33 2015-06-12 00:00:00 199 33 2015-06-12 00:00:00
5565 1299 50 2015-06-14 00:00:00 1299 50 2015-06-14 00:00:00
EDIT: So I have gotten to where I can have the 2 seperate queries, one gets the row with the MIN(price) and the second gets the row with the MAX(timestamp) for each codename.
Now what I need to do is join them together so that they are all on the same row (grouped by codename) as in the example above.
SQL Fiddle of 2 queries
So after some playing with joins I was able to get the 2 queries to output onto a single row per codename:
SELECT *
FROM
(
SELECT p.*
FROM prices p
JOIN
(
SELECT codename, MIN(price) minPrice
FROM prices GROUP BY codename
) p2
ON p.price = p2.minPrice AND p.codename = p2.codename
) min
LEFT JOIN
(
SELECT p.*
FROM prices p
JOIN
(
SELECT codename, MAX(timestamp) maxTime
FROM prices GROUP BY codename
) p2
ON p.timestamp = p2.maxTime AND p.codename = p2.codename
) latest
ON latest.codename = min.codename
I'm sure the query is far from perfect, but it does give me the results I am looking for.
SQL Fiddle
If there is anything drastically wrong with this, please let me know and I can update.
Try following,
Select codename, minPrice, minDis, minTime, latestPrice, latestDis, latestTime from
(
Select T_Low.codename, minPrice, minDis, minTime, T_Latest.latestPrice, T_Latest.latestDis, T_Latest.latestTime from
(
select * from (
select row_number() over(partition by codename order by codename, price) row_id, codename, price as minPrice, discount as minDis, timestamp as minTime from
(
select codename, discount, timestamp , min(price) as price from prices
group by codename, discount, timestamp
)T
) T1
where row_id = 1
) T_Low
left join
(
select * from (
select row_number() over(partition by codename order by codename, timestamp desc) row_id, codename, price as latestPrice, discount as latestDis, timestamp as latestTime from
(
select codename, discount, timestamp , min(price) as price from prices
group by codename, discount, timestamp
)T
) T1
where row_id = 1
)t_Latest
ON T_Low.codename= T_Latest.codename and T_Low.row_id = T_Latest.row_id
)T
order by codename
I have a query;
select ProductID, name, (unitPrice*quantity) As 'Stock Equity'
from tproduct
group by productID with rollup
;
I wish for this to return a set of rows with the last being a calculation of the Total, instead its repeating the last result?
can anyone tell me why and how to overcome this please?
this is the query result at the moment;
ProductID Name Stock Equity
1 cannon 600 D 3360
2 cannon 550 D 1000
3 cannon 500 D 750
4 cannon 5D 5000
5 cannon 650 D 9000
6 Nikon D5100 1000
7 Nikon D3200 420
8 Nikon D7000 2700
9 Nikon D800 6030
10 Nikon D90 4770
null Nikon D90 4770
You can use UNION ALL:
select ProductID, name, (unitPrice*quantity) As 'Stock Equity'
from tproduct
union all
select 'total', 'total', sum(unitPrice*quantity)
from tproduct
See SQL Fiddle with Demo
Or you can use something like this:
select case when ProductID is null then 'total' else ProductId end Productid,
case when ProductID is null then 'total' else name end name,
sum(unitPrice*quantity) As 'Stock Equity'
from tproduct
group by ProductID with rollup
See SQL Fiddle with Demo
select ProductID,
name,
(unitPrice*quantity) As 'Stock Equity',
(select sum(unitPrice*quantity) from tproduct) As total
from tproduct
group by productID