Counting total per day with my result in SQL - php

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)

Related

How to select a records by specific month / year

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.

Month wise sum of column mysql

Hello i am using php and mysql.I have one table with one column as recdatetime as datetime datatype .
If i search record between two months i want sum of quantity column of same table.
If i have following record.
quantity recdatetime
**44** `2014-01-01 16:53:06`
**14** ` 2014-01-21 16:53:06`
**10** `2013-12-21 16:53:06 `
**17** `2013-12-22 16:53:06 `
**29** `2013-11-20 16:53:06`
If i search between November 2013 and January 2014 i want output in following manner.
November December January
29 27 58
I want Mysql query for above output.
Start with this:
SELECT SUM(quantity) AS s, DATE_FORMAT(recdatetime, '%M') AS m
FROM table_name
GROUP BY DATE_FORMAT(recdatetime, '%Y-%m')
You can use month(recdatetime), sum(quantity) and group by month(recdatetime).
Select year(recdatetime), month(recdatetime), sum(quantity) as total from x
group by year(recdatetime),month(recdatetime);
You will need the year or it will also sum up months for different years. You normaly do not want this. And add a where condition to only get the months you want. Hope you can do that on your own.
Switching rows and columns could be done in the output.
You can try like this:
select sum(quantity) as sumQty,recdatetime FROM table_name group by Month(recdatetime

selecting the data between two date

I am trying to select the price between two date (star date and end date). In my database i have fixed price like this:
2013-05-01 to 2013-05-31 price: 300
2013-06-01 to 2013-06-30 price: 200
2013-07-01 to 2013-07-01 price: 250
I user selects any date between these 3 i am able to generate result by doing
sdate>='2013-05-01'
AND edate <='2013-05-31'`.
But if a user selects date 2013-05-28 to 2013-06-03 then it should two days price (300) for may month and 3 days price (200) from the month june.
How can i achieve this?
try this
select price from tablename where sdate >= '2013-05-01' and edate <= '2013-05-31'
if user enter date for two different months then your select query will return two prices. assume select price from tb_demo where sdate>='2013-06-01' and edate<='2013-07-31' this will return two prices which is stored into resultset.
After that by using while loop you can easy print those price.If u want to print month name also then use array and print that name.
Please Before positing Use search...
SQL query to select dates between two dates

MySQL: Find top-3-customers with within a time frame

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

How to split records per hour in order to display them as a chart?

I have an SQL table like this : sales(product,timestamp)
I want to display a chart using Open Flash Chart but i don't know how to get the total sales per hour within the last 12 hours. ( the timestamp column is the sale date )
By example i will end up with an array like this : array(12,5,8,6,10,35,7,23,4,5,2,16) every number is the total sales in each hour.
Note: i want to use php or only mysql for this.
Thanks
SELECT HOUR(timestamp),COUNT(*)
FROM sales
WHERE timestamp >= DATE_SUB(NOW(),INTERVAL 12 HOUR)
GROUP BY HOUR(timestamp)
The SQL is
SELECT HOUR(timestamp), COUNT(product)
FROM sales
ORDER BY HOUR(timestamp)
Loop over the result to get it into an array.
EDIT: Applying requested where condition for unix timestamp
SELECT HOUR(timestamp), COUNT(product)
FROM sales
WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(NOW(),INTERVAL 12 HOUR))
ORDER BY HOUR(timestamp)
Some pseudo code:
foreach timestamp
use date('G',timestamp) to get hour
increment array value using the hour as key
Something along those lines. Watch out for timezones.

Categories