I'm searching for a solution to this:
A customer can place a order with a price x at a time y. A customer can have unlimited number of orders.
I want to get the z top-customers with their ordering-amount within a time-frame (e.g.a month).
I'm stuck at the summing and the ordering / filtering to the top z.
Can you help me out? Thanks!
Given an orders table with customer_id, amount and time columns, you should be able to do something like this:
SELECT customer_id, sum(amount) AS total
FROM orders
GROUP BY customer_id
WHERE time BETWEEN start AND end
ORDER BY total DESCENDING
LIMIT 3
This is psuedo code, but I'd use something like:
select sum(order_total), client_id
from orders
where order_date between X and Y
group by client_id
order by sum(order_total)
limit 0, 10
Related
I have a query that essentially should bring the total sales per day but I don't know what I'm doing wrong in the query that brings me all the sales without adding the days.
$ventasdia = DB::select('SELECT DATE_FORMAT(v.fecha_venta,"%d/%m/%Y") as dia, sum(v.monto) as totaldia from ventas v where v.id_estado_venta="2" group by v.fecha_venta order by
day(v.fecha_venta) desc limit 12');
This is what the query brings me:
As can be seen, the days are repeated and it does not add up per day.
You need to GROUP BY dia not by v.fecha_venta.
If you group by v.fetch_venta it groups by the datetime and this is probably different for every row.
I am trying to get monthly sales totals from a MYSQL database and have them summed by month with month name for last X months. The two columns I am targeting are: total_customer_charged and local_time. I almost have my query working correctly, the problem I have is that the order the results come out sometimes changes with each query. Can anyone please give me some hints to get my query correct?
My existing query is:
select date_format(local_time,'%M'),sum(total_customer_charged) FROM ORDERS WHERE local_time BETWEEN curdate() - INTERVAL 3 MONTH AND curdate() group by month(local_time) order by year(local_time),month(local_time)
You should put year into the group-by statement like this:
select
date_format(local_time, '%Y') AS agg_year,
date_format(local_time, '%m') AS agg_month,
sum(total_customer_charged) AS monthly_total_customer_charged
FROM
ORDERS
WHERE
local_time BETWEEN curdate() - INTERVAL 3 MONTH AND curdate()
group by
date_format(local_time, '%Y'),
date_format(local_time, '%m')
order by
agg_year,
agg_month
SELECT MONTHNAME(local_time), SUM(total_customer_charged)
FROM ORDERS
GROUP BY YEAR(local_time), MONTH(local_time)
I'm trying to get the last missing piece in my website unfortunately I need to generate sales report as well. I don't know where to start.
How can i display the records from a specific week, month, year. and total the amount
TABLE: payments.tbl
payment_id amount customer_id product_id trx_id currency payment_date
21 10470 1 15 5F110606611093636 PHP 2015-03-30
NOTE: payment_date structure is DATE
You get specific records with WHERE.
You get a total amount with SUM().
How to deal with dates depends on your DBMS, because different DBMS feature different date functions. Here is an example for MySQL:
select
sum(amount) as total_amount,
currency,
count(distinct customer_id) as number_of_different_customers,
count(distinct product_id) as number_of_different_products
from mytable
where month(payment_date) = month(curdate()) and year(payment_date) = year(curdate())
group by currency;
I group by currency here to get one result record per currency, as it makes no sense to sum amounts of different currencies.
Date functions for MySQL are found here: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html.
I have a product table with product_id and entry_date field (which is varchar and have timestamp value by time() function in php). I want to get result of how many products have been uploaded per week. Please tell me the SQL so that I can get the result of how many products have been uploaded per week.
I also have user_id field.I also need how many products have been uploaded per week per user.
possible duplicate: Show most viewed entries that have been added in the last 7 days
try something like this:
SELECT data-you-want-to-retrieve
FROM `Your-Table`
WHERE entry_date > (curdate() - 604800)
So it sees that the entry date is larger than the date 7 days ago.
SELECT CONCAT(YEAR(entry_date), '_', WEEKOFYEAR(entry_date)) AS week, COUNT(*) Count, user_id
FROM `tbl_products`
GROUP BY week, product_id, user_id
Count of product uploads per week:
SELECT CONCAT(YEAR(entry_date), '_', WEEKOFYEAR(entry_date)) AS week,
COUNT(1) AS prodcnt
FROM products
GROUP BY week
Count of products per week for a particular user:
SELECT CONCAT(YEAR(entry_date), '_', WEEKOFYEAR(entry_date)) AS week,
COUNT(1) AS prodcnt
FROM products
WHERE user_id = <userid here>
GROUP BY week
You'll get results grouped by something like 2012_48, where 48 is the week of the year (goes from 1-53).
I have 50 rows/entrys in my table Orders. I have a column, that holds when the order is claimed at, named claimed_at.
The date in this field are in this format: 2011-10-03 07:07:33
This is in the format (yy/mm/dd time).
I also have a column called price, this price is how much they paid.
I would like to display totals per day.
So for 6 orders from the date 2011-10-03, it should take the 6 order's price value, and plus them together.
So I can display:
2011-10-03 -- Total: 29292 Euros
2011-10-02 -- Total: 222 Euros
2011-09-28 -- Total: 4437 Euros
How can i do this?
You need to use aggregate functionality of MySQL in conjuction with some DATE conversion.
SELECT DATE(claimed_at) AS day
, SUM(price) AS total
FROM Orders
GROUP BY day
Create a new field say day with DATE_FORMAT(claimed_at,'%Y-%m-%d') AS day , sum the price and group by day so you will get the result you want.
Something like
SELECT DATE_FORMAT(claimed_at,'%Y-%m-%d') AS day,SUM(price) as total FROM orders GROUP BY day ORDER BY day DESC
Use grouping like this :
SELECT claimed_at, SUM(price) AS total
FROM Orders
GROUP BY claimed_at
You need to use function to get only date part(not time) in grouping. I dont know which function is used in mysql, in MSSQL it's CONVERT() function.
SELECT TRUNC(claimed_at), SUM(PRICE)
FROM my_orders_table
GROUP BY TRUNC(claimed_at)