i have one table with two columns as shown in picture
at the execution of page user enter the start and end date.
now what i need to calculate is total amount.
for example, if user put
start date: 18-jan-2010
end date: 23-jan-2010
then he has 2 dates in first options (20$) and 3 dates in second options (26$) so total will be (56$).
first i want to calculate in sql
1. how many days in first range (if any this depends on supplied dates from user)
2. how many days in 2nd range (if any this depends on supplied dates from user)
3. how many days in 3rd range (if any this depends on supplied dates from user)
then
we will multiply days with Amount in php for each range.
at the end i want to get grand total also in php.
Maybe:
SELECT SUM(amount) FROM your_table WHERE date BETWEEN start_date AND end_date GROUP BY user
SELECT 'In Range' AS SumType
SUM(amount)
FROM <table>
WHERE date BETWEEN start_date AND end_date
UNION
SELECT 'Outside Range' AS SumType
SUM(amount)
FROM <table>
WHERE date < start_date OR date > end_date
UNION
SELECT 'Grand Total' AS SumType
SUM(amount)
FROM <table>
Related
This is the output of a query on a temporary table created in a complex stored procedure in MySQL :
The goal now is to have the average duration per day like we have here, but for each week by month
So I want to take every month and divide it into 4 set of days with the average duration per day.
Therefore no matter how many days there are in the month, I will have 4 values for it.
How can I do that ?
Note : If it is easier I can do it with php, since I will use the data with this language.
You have a query that groups per day, e.g.:
select
the_date as day,
sec_to_time(avg(timestampdiff(second, start_time, end_time))) as duration
from ...
group by the_date;
You want days 1-7, 8-14, 15-21, 22-end per month instead. Use CASE WHEN to build the groups.
select
year(the_date) as year,
month(the_date) as month,
case
when day(the_date) <= 7 then '01-07'
when day(the_date) <= 14 then '08-14'
when day(the_date) <= 21 then '15-21'
else '22-end'
end as day_range,
sec_to_time(avg(timestampdiff(second, start_time, end_time))) as duration
from ...
group by year, month, day_range
order by year, month, day_range;
This is little confusing me how to select dates from table on continuous basis.
Suppose i have absent_report table and have entries like this,
Sno Code date
1 101 01-01-2014
2 101 02-01-2014
3 101 03-01-2014
4 101 05-01-2014
5 101 06-01-2014
6 101 07-01-2014
I only want to select continues date from a date. like first three dates not fourth one and so on.
Example Like I have date from which I have to compare is Like 31-12-2013 now from here next continues dates like 01-01-2014,02-01-2014,03-01-2014 only, No matter next records like i have dates in that table 05-01-2014,06-01-2014,07-01-2014. I just want first continuous dates.Hope this clear more what I want.
Use the BETWEEN syntax
SELECT * FROM table WHERE date BETWEEN '2014-01-01' AND '2014-01-03';
You can check if either the next row has the following day or the previous row has the previous day. To do this, I used DATE_ADD() as described in this answer. However, I am not sure if this would be better than implementing the logic in PHP alltogether.
SELECT sno, code, date FROM table AS current
WHERE
DATE(DATE_ADD(date, INTERVAL -1 DAY)) = (SELECT date FROM table AS previous WHERE previous.date < current.date ORDER BY date DESC LIMIT 1)
OR
DATE(DATE_ADD(date, INTERVAL +1 DAY)) = (SELECT date FROM table AS next WHERE next.date > current.date ORDER BY date ASC LIMIT 1)
ORDER BY date ASC
See it work here.
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 a mysql database in which i have two datetime fields, one is start_date another one is end_date. So lets say scorer1 have a start_date and end_date | similarly scorer2 have a start_date and end_date.
Now how can i get the average days taken by all the scorers?
You can select the difference between the dates with mysql:
SELECT SUM(DATEDIFF(end_date, start_date)) as difference_in_days, COUNT(id) as total_rows FROM table
This will give you the total days difference, and the total rows it applies to.
Then once you've pulled that from the DB, use some PHP to work out the average:
$average = $row['difference_in_days'] / $row['total_rows'];
i have one table with two columns as shown in picture
table columns names are (MAXDATE,AMOUNT).
if you see we have
first date range (from current date to 20-jan-2010)
second date range from 20-jan-2010 to 30-jan-2010
3rd range is from 20-jan-2010 to 31-jan-2010.
at the execution of page user enter the start and end date.
for example, if user put
start date: 18-jan-2010
end date: 23-jan-2010
then he has 2 dates in first options and 3 dates in second options.
what i want to calculate in sql
1. how many days in first range (if any this depends on supplied dates from user)
2. how many days in 2nd range (if any this depends on supplied dates from user)
3. how many days in 3rd range (if any this depends on supplied dates from user)
Thanks
Here is an example how to calculate days.
http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html
You can do all of this in MySQL:
SELECT DATEDIFF(NOW(), max_date) as days;
SELECT DATEDIFF(date2, date1) FROM
(SELECT max_date as date1 FROM table1 LIMIT 1) as s1
JOIN
(SELECT max_date as date2 FROM table1 LIMIT 1 OFFSET 1) as s2;
//etc.