2 Counts Statements in MySQL [duplicate] - mysql

This question already has answers here:
Is it possible to GROUP BY multiple columns using MySQL?
(7 answers)
Closed last month.
I am trying to see the number of products sold to different companies by month and year.
For example:
I have a dataset where I have a long list of products. I would like to see two different columns where the first column shows a count of all those products and a second column that shows the company that was sold to by month and year
That is my query:
SELECT MONTHNAME(date) AS Month, COUNT(*), company
FROM database.products
WHERE YEAR(date) = '2022'
GROUP BY MONTH(date)
WITH ROLLUP);
Expected Result:
Year
Month
Products
Company
2022
January
654
walmart
2022
January
895
Winco
2022
February
562
Ross
2022
February
456
Best Buy

Just add the rest of the fields...?
SELECT YEAR(date) AS Year, MONTHNAME(date) AS Month, COUNT(*), company
FROM database.products
WHERE YEAR(date) = '2022'
GROUP BY YEAR(date), MONTH(date), company
WITH ROLLUP

Related

2 where conditions from 1 column SQL [duplicate]

This question already has answers here:
How can I get multiple counts with one SQL query?
(12 answers)
Closed 2 months ago.
I am trying to see 2 conditional columns using the WHERE clause from 1 column.
For example:
I have a dataset where I have a long list of calls. I would like to see two different columns where the first column shows a count of all those calls who are equal or longer that 0 seconds and a second column that shows a count of all those calls that are equal or longer to 120 seconds.
That is my first query:
SELECT distinct year(date) Year, monthname(date) as Month, count(calls) Leads
FROM database.calls
where duration >= 120
and year(date) = 2022
group by month(date)
order by max(date);
second query:
(The only difference it's on the 3rd line where duration is equal to 0)
SELECT distinct year(date) Year, monthname(date) as Month, count(calls) Leads
FROM database.calls
where duration >= 0
and year(date) = 2022
group by month(date)
order by max(date);
Expected Result:
Year
Month
Calls(0sec)
Calls(120sec)
2022
January
654
521
2022
February
895
465
2022
March
562
321
For obtaining conditional sums you can use conditional aggregation: specify a condition inside your aggregate function, so that it aggregates only the eligible values.
SELECT YEAR(date) AS Year_,
MONTHNAME(date) AS Month_,
SUM(IF(duration > 0, 1, 0)) AS Calls0Sec,
SUM(IF(duration > 120, 1, 0)) AS Calls120Sec
FROM database.calls
WHERE YEAR(date) = 2022
GROUP BY YEAR(date), MONTHNAME(date)
ORDER BY MAX(date);
Note: you need to add every selected and non-aggregated field inside the GROUP BY clause, otherwise you can expect either DBMS errors or wrong data.

Query to combine two tables group by month

I have tried to connect two tables by join and group them to get the count. But unfortunately, these two tables don't have any common value to join (Or I have misunderstood the solutions).
select date_format(check_in.date,'%M') as Month, count(check_in.Id) as checkInCount
from check_in
group by month(check_in.date);
Month
checkInCount
July
1
October
2
This is the first table.
select date_format(reservation.date,'%M') as Month, count(reservation.id) as reserveCount
from reservation
group by month(reservation.date);
Month
reserveCount
July
3
October
5
This is the second table.
I want to show these two tables in one table.
Month
checkInCount
reserveCount
July
1
3
October
2
5
Thank you for trying this and sorry if this is too easy.
You will need to join the result by month from your two subqueries.
This query assume all the month (July, August, September...) present in your subqueries monthCheckInStat, monthCheckOutStat, even if the count is 0
SELECT monthCheckInStat.Month, monthCheckInStat.checkInCount, monthCheckOutStat.reserveCount
FROM
(
select date_format(check_in.date,'%M') as Month, count(check_in.Id) as checkInCount
from check_in
group by month(check_in.date)
) monthCheckInStat
INNER JOIN
(
select date_format(reservation.date,'%M') as Month, count(reservation.id) as reserveCount
from reservation
group by month(reservation.date)
) monthCheckOutStat
ON monthCheckInStat.Month = monthCheckOutStat.Month;

How to Aggregate Weighted averages fields at different levels of granularity?

I am calculating weighted formula for a field as sum(revenue)/ Sum(qty) and this is as per the below query. Now I will be creating a view that would store these results as I shown in code below.
My question is, if I select this w_revenue out of the view and want to see per year, how will I aggregate to show it per year? See desired outputs.
select month,item, sum(revenue)/ Sum(qty) as w_revenue,
year
from my_revenue_table
group by month, year,item;
create view xyz as
select month,item, sum(revenue)/ Sum(qty) as w_revenue,
year
from my_revenue_table
group by month, year,item;
select w_revenue
from xyz.
How do I do this so as to aggregate this per year?
Year Month Item Revenue Qty Sum(revenue)/Sum(Qty)
2019 Mar A 10 2 5
2019 Mar B 30 3 10
2019 Feb C 50 1 50
2019 Feb D 20 2 10
Expected value if I see per year:
Year Sum(revenue)/Sum(Qty)
2019 13.75 (10+30+50+20)/(2+3+1+2)
group on by year then create another view as
create view abc as
select sum(revenue)/ Sum(qty) as w_revenue, year
from my_revenue_table
group by year;
and call
select * from xyz union all
select null, null, a.* from abc a
to combine them, if I understood you correctly.
or revenue per year repeating at every row as
select x.*,
(select w_revenue
from abc
where year = x.year) as w_revenue_year
from xyz x
You cannot do the aggregation you want from the view. You don't have enough information.
If you change the view to something like this:
create view xyz as
select year, month, item,
sum(revenue)/ Sum(qty) as w_revenue,
sum(qty) as total_qty
from my_revenue_table
group by month, year, item;
Then you can aggregate the results as:
select year, sum(w_revenue * total_qty) / sum(total_qty)
from xyz
group by year;
EDIT:
Or just modify the view to have the information you want:
create view xyz as
select year, month, item,
sum(revenue)/ Sum(qty) as w_revenue,
sum(qty) as total_qty,
(sum(sum(revenue)) over (partition by year, item) /
sum(sum(qty)) over (partition by year, item)
) as w_revenue_year
from my_revenue_table
group by month, year, item;

MySQL Query for current year sales by month and previous years sales by month

How can I get the net sales for last year by month also?
I am trying to report current month sales info with the related increase/decrease percentages.
select date_format(sdate,'%M-%Y') as sdate,
sum(netsales) as 'netsales',
from repsoft.daily
where scode=1234 and year(sdate)=year('2016-06-01')
group by month(sdate)
order by month(sdate);
Why don't you group it by year as well?
select date_format(sdate,'%M-%Y') as sdate,
sum(netsales) as 'netsales',
from repsoft.daily
where scode=1234 and year(sdate)=year('2016-06-01')
group by year(sdate), month(sdate)
order by year(sdate), month(sdate);

mysql - getting the last 10 unique months

I want to get the last 10 unique months where there have been events, stored as event_date as a mysql date. I am using coldfusion so could group the result but can't get that working as I would like.
This is what I have so far.
SELECT MONTH(event_date) AS month, YEAR(event_date) AS year, event_date from events
so ideally if there were no posts in August would be output as
september 2010
july 2010
june 2010
may
etc etc
Thanks.
R.
SELECT DISTINCT MONTH(event_date) AS month, YEAR(event_date) AS year from events ORDER BY year DESC, month DESC LIMIT 10;