How get earnings per month MYSQL Query - mysql

I have two tables, one table called "tbl_materiales", and another called "tbl_pedidos".. In the table called tbl_materiales" I have information about all my products, Like Description, and the most important "Price"...
In my table "tbl_pedidos", i register all information of products that the user register in the website.
For example:
tbl_materiales:
IdProduct Description Price
5 Product one 8
6 Product three 10
7 Product four 15
tbl_pedidos
IdProduct Quantity Month
5 10 January
6 5 January
7 2 February
So, I want to know all the earnings PER month...
I want to have this: The column earnings is the multiplication of tbl_pedidos.Quantity * tbl_materiales.Price, obviously it depends of the price of the product, and the quantity sold out.
Month Earnings
January 130
February 30
Now, I have this, but it doesn't bring me the correct information...
SELECT tbl_pedidos.Mes
, (SUM(tbl_pedidos.Cantidad) * tbl_materiales.Precio) as Total
FROM tbl_pedidos
INNER JOIN tbl_materiales
ON tbl_pedidos.IdMaterial = tbl_materiales.IdMaterial
GROUP BY tbl_pedidos.Mes
ORDER BY tbl_pedidos.Fecha;

SELECT tbl_pedidos.Mes , SUM(tbl_pedidos.Cantidad*tbl_materiales.Precio) as Total
FROM tbl_pedidos
INNER JOIN tbl_materiales
ON tbl_pedidos.IdMaterial = tbl_materiales.IdMaterial
GROUP BY tbl_pedidos.Mes
ORDER BY tbl_pedidos.Fecha;
Check http://sqlfiddle.com/#!9/de665b/1

The query can be like :
SELECT tbl_p.Month
,sum(as tbl_m.Price*TP.Quantity) AS Earnings
FROM tbl_materiales AS tbl_m
JOIN tbl_pedidos AS tbl_p
ON tbl_m.IdProduct = tbl_p.IdProduct
GROUP
BY tbl_p.Month;

In this case I have used Where instead of Join, maybe de next sentence resolve your problem:
select TP.Month,sum(TM.Price*TP.Quantity) as Earnings
from TBL_Pedidos TBP,TBL_Materiales TM
where TP.IdProduct = TM.Id_Product
group by TP.Month
Group by is the solution

Related

Group by inside inner join 1055

So, i have 2 tables one of them is a control list of holidays (1 for holiday and 0 for not holiday) and the day of the week on that date it kinda looks like this:
calendar_date
Day_of_week
Holiday_flg
2016-01-02
Monday
1
2016-02-03
Friday
0
2016-02-01
Monday
1
And a second table with some restaurant info that looks like this:
Visit date: refers to the day the visitors arrive at the restaurant
Reserve visitors: refers to the day the visitors created a reservation on the restaurant (not used in this problem, just part of the main table)
ID
Visit_date
reserve_datetime
reserve_visitors
1023044
2016-02-01
2016-01-01
5
1041331
2016-01-01
2016-01-01
2
1023044
2016-01-01
2016-01-01
4
I used this query to join both tables together by date
SELECT * from restaurants_visitors
inner join date_info
on restaurants_visitors.visit_date= date_info.calendar_date
where holiday_flg=1
limit 10
;
which returns both tables joined by the date
I want to group them by restaurant ID, so i can get the highest average number of vistors on holidays, and the percentage of growth of the amount of visitors week over week for the last 4 weeks
So far i have tried something like this
SELECT * FROM restaurants_visitors as rv
inner join date_info as df
on rv.visit_date= df.calendar_date
where df.holiday_flg=1
group by rv.id
limit 10
;
But i keep getting this error code:
Error Code: 1055. Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database.restaurants_visitors.visit_date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 0.024 sec
Expected Output
List of top 5 more visited restaurants in holidays( Table with id of restaurant and avg visitors on holidays)in dsc order
ID
Average_visitorsin_holidays
1023044
4.5
1041331
2
List of percentage of growth of the amount of visitors week over week for the last four weeks of the data?(Table with percentage of growth over weeks)
week
percentage of growth over last week
4
unknow result
3
unknow result
Which day of the week there are usually more visitors on average in restaurants.
(single row with day of the week with more visitors)
Day of the week
AVG visitor per day of week
Monday
4.5
1041331
1
Ok, the main problem is that you're selecting ALL the columns but only defining rv.id as the group identifier.
You want to start with something like this:
SELECT
rv.id,
df.calendar_date,
sum(rv.reserve_visitors) as total_visitors
FROM
restaurants_visitors as rv
inner join
date_info as df on rv.visit_date= df.calendar_date
WHERE
df.holiday_flg=1
GROUP BY
rv.id,
df.calendar_date
This will tell you the total visitors per restaurant per holiday. With that in hand, we can find the average visitors per restaurant on holidays:
WITH
holiday_visitors as (
SELECT
rv.id,
df.calendar_date,
sum(rv.reserve_visitors) as total_visitors
FROM
restaurants_visitors as rv
inner join
date_info as df on rv.visit_date= df.calendar_date
WHERE
df.holiday_flg=1
GROUP BY
rv.id,
df.calendar_date
)
SELECT
id,
avg(total_visitors) as avg_holiday_visitors
FROM
holiday_visitors
GROUP BY
id
ORDER BY
avg_holiday_visitors desc
This will tell you which restaurants had the most traffic on average on holidays. How you limit it to the top 5 depends on the database you are using.

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;

I would like to count the number of users who made multiple purchases grouped by month

So what i'm trying to do here, is that i am trying to count the number of repeat users (users who made more than one order) in a period of time, let it be month day or year, the case here is months
i'm currently running mysql mariadb and i'm pretty much a beginner in mysql, i've tried multiple subqueries but all have failed till now
This is what i have tried so far ..
This returns all the number of users with no ordering count condition
Since people are asking for sample data, here is what the data is looking like at the moment:
Order_Creation_Date - User_ID - Order_ID
2019-01-01 123 1
2019-01-01 123 2
2019-01-01 231 3
2019-01-01 231 4
This is the query i am using to get the result but it keeps on returning total number of users within the month
select month(o.created_at)month,
year(o.created_at)year,
count(distinct o.user_uuid) from orders o
group by month(o.created_at)
having count(*)>1
and this returns the number of users as 1 ..
select month(o.created_at)month,
year(o.created_at)year,
(select count(distinct ord.user_uuid) from orders ord
where ord.user_uuid = o.user_uuid
group by ord.user_uuid
having count(*)>1) from orders o
group by month(o.created_at)
Expected result will be from the sample data above
Month Count of repeat users
1 2
If you want the number of users that make more than one purchase in January, then do two levels of aggregations: one by user and month and the other by month:
select yyyy, mm, sum( num_orders > 1) as num_repeat_users
from (select year(o.created) as yyyy, month(o.created) as mm,
o.user_uuid, count(*) as num_orders
from orders o
group by yyyy, mm, o.user_uuid
) o
group by yyyy, mm;
I think you should try something like this which will return USer_ID list Month and Year wise who ordered more that once for the period-
SELECT
[user_uuid],
MONTH(o.created_at) month,
YEAR(o.created_at) year,
COUNT(o.user_uuid)
FROM orders o
GROUP BY
MONTH(o.created_at),YEAR(o.created_at)
HAVING COUNT(*) > 1;
For more, if you are looking for the count that how many users placed more that one order, you can just place the above query as a sub query and make a count on column 'user_uuid'

how to write a Query in Mysql

I have 2 tables.ms_expese and track_expense.Using this table generate a fact table
I want the expense_name in ms_expense,expense_amount from track_expense.
I want to get the sum of expense_amount for a particular expense_name based on date.The date in the order of 1,2...12 as month id
SELECT DATE_Format(a.date,'%b') as month_id,b.expense_name AS expense_type, sum(a.expense_amount) AS expense_amount FROM ms_expense b JOIN track_expense a on a.`expense_id`=b.`expense_id` group by DATE_Format(a.date,'%b')
how to put the month id in the order of 1,2,..12 and my date format y-m-d
I get the month in apr,aug and so on but i need jan as 1,feb as 2
I have 25 expenses(expense name).In this query i got the total expense amount of first expense only.I want the total expense of all expenses in every month
CREATE TABLE fact AS
(<your select query>)
Your select query can be in the following form
SELECT MONTH(date)as month_id,expense_name,sum(expense_amount)
FROM ms_expense JOIN track_expense using (expense_id)
group by expense_name,MONTH(date)

Get sold items, ordered by purchases, in the last week

I have 2 Tables.
The first table stores the products.
id I Product Name
1 I Apple
2 I Bread
3 I Butter
... Many more
The second table stores the overall purchases with a date
product_id I purchasedoverall I date
1 I 2 I 1.2.14
1 I 10 I 3.2.14
3 I 3 I 4.2.14
... Many more
Attention: In the example, at 1.2.14 2 apples where sold, at 3.2.14 8 more apples, so there are 10 stored in purchasedoverall.
What I want from the Database is:
Give me the products purchased in the last week, ordered by how many items where bought IN THE LAST WEEK.
Until now, I do it this (bad) way:
I get all products
SELECT * FROM products
Store them in an array
I iterate over the array to get the purchased count in the last week
SELECT MAX(c.purchasedoverall)-MIN(c.purchasedoverall) as purchased
FROM products as a, puchased as c
WHERE c.product_id = {product-id from the array}
AND c.date >= NOW() - INTERVAL 1 WEEK
ORDER BY c.date
I combine the arrays an sort them
My question: Can I do this with one MySQL-Query?
Assuming pr.name is unique:
SELECT pr.name,
COALESCE(MAX(pu.purchasedoverall)-MIN(pu.purchasedoverall),0) purchased
FROM products pr
LEFT JOIN purchased pu
ON pu.product_id = pr.id
AND pu.date > CURDATE() - INTERVAL 1 WEEK
GROUP BY pr.name
ORDER BY purchased DESC, pr.name
But as pointed out in the comments you should really be storing data at the resolution that you need it, not performing complex queries to enhance the resolution..
Whatever the difficulties of obtaining the data structure and the code associated, it is almost certainly much easier to refactor this than build a system atop a flawed design.
SELECT a.id, a.product_name, MAX(c.purchasedoverall)-MIN(c.purchasedoverall) as total_purchased
FROM products a
left join puchased c
ON c.product_id=a.id
AND c.date >= NOW() - INTERVAL 1 WEEK
group by a.id
ORDER BY total_purchased;