How to reset the sales order after day end as per below example 01/11/2015
sales order number is 0001,0002,0003 to 1000 02/11/2015 sales order number is 0001,0002,0003 to 1100 03/11/2015 sales order number is 0001,0002,0003 to 1500.
Related
I have a table transactions like this
date
amount
2020-02-26
1000
2020-02-26
1500
2021-01-11
200
I want to select the sum of all transactions per month. The result should look something like...
month
sum
2020-02
2500
2021-01
200
This is what I've got so far
select sum(amount) sum, MONTH(date) month from transactions group by month;
However this groups by month ignoring year (the values returned for month are single integers). I need to group by each individual month (return a different row for Jan 2020 vs Jan 2021)
here is one way:
select sum(amount) sum, extract(YEAR_MONTH FROM `date`) month
from transactions
group by month;
I have 2 tables. One contains expenses, the other contains earnings.
my_costs:
my_earnings:
From these two tables, I want to get data:
Grouping a period by month and year
The amount of expenses in the period
Average expenses per day in the period
Amount of earnings in the period
I am using the following query:
SELECT DATE_FORMAT(my_costs.DATE, "%M-%Y") AS Period, SUM(my_costs.sum) AS Costs,
ROUND(SUM(my_costs.sum)/DAY(LAST_DAY(my_costs.date)), 0) AS Average,
SUM(my_earnings.sum)
FROM my_costs
LEFT JOIN my_earnings ON DATE_FORMAT(my_costs.DATE, "%M-%Y") = DATE_FORMAT(my_earnings.date, "%M-%Y")
GROUP BY DATE_FORMAT(my_costs.DATE, "%M-%Y")
ORDER BY Period DESC
This request successfully gives me the period, costs and earnings. But the amount of earnings shown is incorrect. The numbers are much higher than expected, should be no more than 10,000 per month
Aggregating after joins can be inaccurate, because rows are duplicated or lost. I would recommend using union all and then aggregating:
SELECT DATE_FORMAT(ce.DATE, '%M-%M') AS Period,
SUM(ce.costs) AS Costs, SUM(ce.Expenses) as expenses,
ROUND(SUM(ce.sum)/DAY(LAST_DAY(ce.date)), 0) AS daily_Average
FROM ((SELECT date, sum as costs, 0 as expenses
FROM my_costs
) UNION ALL
(SELECT date, 0, sum
FROM my_earnings
)
) ce
GROUP BY period
ORDER BY MIN(ce.DATE) DESC;
Note that I changed the ORDER BY so the results are in time order.
I want to get the first 4 rows with the highest value in a certain date range of a table in a mysql database. So I can do it with this code line, for example:
SELECT MONTH(date) as month, amount from sales where date >='2014-01-01' AND date <='2014-12-31' ORDER BY amount DESC LIMIT 4
But I want to add a new column with the percentage of each value, I tried with this line:
SELECT MONTH(date) as month, amount, round(amount*100/sum(amount) ,1) as 'percent' from sales where date >='2014-01-01' AND date <='2014-12-31' ORDER BY `amount` DESC LIMIT 4
But it doesn't work. How can I get it? I'd like some help.
Here's one way of doing it. Providing snippets of raw data also helps. Creating an sql fiddle helps even more!!
select month, amount, round(amount*100/total,1) as percent
from
(
select MONTH(date) as month, amount
from sales
where date >='2014-01-01' AND date <='2014-12-31'
ORDER BY amount DESC LIMIT 4
) c JOIN (
select sum(amount) as total
from sales
where date >='2014-01-01' AND date <='2014-12-31'
) t
I have a sales table which has one row for each sales tranaction. This table has date of sale and customer id as well.
I am looking for a way to select all those customers who have total spending in the specified range with in a specified date range. For example, get all customers who spent between 100 and 1000, between 2016-07-01 and 2016-08-15. This then has to become part of a larger query.
This query
select
customer_id,
sum(sale_amount)
from
sales_receipt
where
DATE(sales_receipt.sale_date) BETWEEN '2016-07-01' AND '2016-08-29'
group by
customer_id;
gives me all customers and their total spending in the specified date range but I need only those customers for whom sum(sale_amount) is between 100 and 1000.
Can any one help.
Try to use
select customer_id, sum(sale_amount) from sales_receipt where
DATE(sales_receipt.sale_date) BETWEEN '2016-07-01' AND '2016-08-29'
group by customer_id having sum(sale_amount)>=100 and sum(sale_amount)<=100
You'd use the HAVING clause here because you want to filter on the aggregated result:
SELECT
customer_id,
SUM(sale_amount) AS total_amount
FROM sales_receipt
WHERE DATE(sales_receipt.sale_date) BETWEEN '2016-07-01' AND '2016-08-29'
HAVING total_amount BETWEEN 100 AND 1000
GROUP BY customer_id;
I want to create two queries for my table which has fields name,surname and amount paid,the first query should select the day,month and the amount paid,the second query should select a month,year in that year and the total amount paid in that month,lets say john paid on 2013-05-01, on 2013-05-03,while peter paid on 2013-04-08, i want the first query to output
month and day amount
05-01 200
05-03 400
04-08 50
and the second query should output:
month and year total
2013-05 600
2013-04 50
I know I can use the sum aggregate function to select the total but the tricky part is how to select the day and the month in the format above,
first query will be
SELECT DATE_FORMAT(date, "%m-%d") AS 'month and day',price as amount FROM `tablename`
and second query will be
SELECT DATE_FORMAT(date, "%Y-%m") AS 'month and year' , SUM(price) AS total FROM `tablename` GROUP BY YEAR(date), MONTH(date)