mysql need to show all months data for the selected year - php

I got stuck with my query. I need to show monthly (current + previous) records.
Let's take below example for the year 2014:-
I have 7 records in the month 03(March)
I have 2 records in the month 04(April)
So total records in the year 2014 is 9.
Now we come to the year 2015:-
I need to show all records in Jan(No new records added in the database), This should be 9.
My Query shows 0.
I need to show all records in Feb(5 new records added in the database), This should be 14.
My query shows 5.
Query works fine when I am checking for month 03 and year 2014, showing all 7 records. but not for all conditions.
I am calling the same query for fetching all months(01-12) data one by one.
SELECT count(pkID) as TRecords
FROM students
WHERE (fkCategoryID ='56'
OR fkSecondaryCategoryID ='56'
OR fkOptionalCategoryID ='56')
AND MONTH(`DateAdded`) <='1'
AND YEAR(`DateAdded`) <='2015';

AND MONTH(`DateAdded`) <='1'
Think about this. You won't find a month smaller than 1. Year and month belong together; don't look at them separately.
SELECT COUNT(pkID) as TRecords
FROM students
WHERE (fkCategoryID = 56 OR fkSecondaryCategoryID = 56 OR fkOptionalCategoryID = 56)
AND DATE_FORMAT(DateAdded, '%Y%m') <= '201501';

In you where clause, you are doing <= (less than or equal to). I expect you want just = (equal to) for your year and month. Certainly your year should only be = (equal to).

Related

sorting dates by year then month in php / sql

Thanks for reading my question.
my mysql database is for my record shop.
I have a table called records with 700 records in it
My fields are artist, title, release_date etc.
The release_date is varchar and entered and stored as mm/yyyy
i.e. 01/2003 = jan 2003
i.e. zz/1997 = during 1997 - some records dont have a month
I want to be able to sort then display the records in order of year, then month in my results. i.e. so I can show the visitor a list of the top 10 most recent records. If a record starts with zz, then it would be last in order of the list for that year.
an example sort results would be thus:
11/2001
06/2001
zz/2001
09/2000
01/2000
zz/2000
Any help appreciated!
Many thanks
Matt.
In MySQL, you can do:
order by right(release_date, 4) desc,
left(release_date, 2) + 0 desc;
A note on the second key. This converts the month to a number. The value zz will be converted to 0 because there are no leading digits.

how to get records b/w two dates that incremented by 1 month in mysql

Assume i have apply on 2015-01-28, then i will clear first month due on 2015-02-28 then last_paid_date will be updated to 2015-02-28, now i didn't clear due to last one month for 2015-03-28 and also today date is 2015-04-28,now i want to show two records for last one month and current month..i am stuggle with "now i want to show two records for last one month and current month"..but i think i can be solve by last paid date.i am so confuse bro .. but entry record is only one.the last paid date only be updated while clear due..
Here is my code, but i not sure this right or wrong in php query exection.any one can tell that.
here last_paid_date updated on every month updation if updated..
mysql_query("
select * from esc_electricitybill
where status='access'
&& (((DATE_ADD(last_paid_date,INTERVAL 1 MONTH))>='$cur_date')
&& ((DATE_ADD(last_paid_date,INTERVAL 1 MONTH))<='$aft_five_date'))
&& service_type='Electricity Bill'
") or die (mysql_error());
I am not sure what are you asking but this is the way how to select records between a date (x) and one month (x+1 month)
SELECT
*
FROM
esc_electricitybill
WHERE
last_paid_date BETWEEN $cur_date AND DATE_ADD(OrderDate, INTERVAL 1 MONTH)

How to get year just before current year from MySQL table

I need a record whose year is just before the current year. My table schema and data is like this
aca_id|aca_start_date | aca_end_date
------|---------------|--------------
1 |2013-04-01 |2014-03-31
3 |2015-04-01 |2016-03-31
7 |2014-04-01 |2015-03-31
I want to fetch the record depending on the current year condition is - if current date year is 2014 then fetch the 1st record because its aca_start_date is 2013 which just before 2014. If current date year is 2015 then fetch the record 2nd record. I cannot sort the table on aca_start_date because table is sorted on aca_id in descending order for other purpose.
Need guidance how can I do this job in MySQL query or if this is possible to do by using php
Here is one way:
select t.*
from table t
where year(aca_start_date) = year(now()) - 1
limit 1;

Mysql BETWEEN clause being ignored. No syntax error

im trying to show items booked between two separate date columns (start and end dates). i want to search between the dates but the query seems to ignore any dates that land between the selected dates.
SELECT
bookings.booking_id,
bookings.booking_id,
bookings.booked_from,
bookings.booked_from,
bookings.booked_till,
item_set_computer.item_id,
item_set_computer.name,
item_set_computer.booking_groups_id
FROM
bookings,
item_set_computer
WHERE
item_set_computer.item_id = bookings.item_id
AND
item_set_computer.booking_groups_id = 3
AND
booked_from
BETWEEN "2010-04-13" AND "2010-04-20"
AND
booked_till
BETWEEN "2010-04-13" AND "2010-04-20"
for instance, I have an item booked from 13th to 15th. date1 is 2010-04-13 and date2 is 2010-04-15. User searches for booked items from 14th to 16th. It returns no results. Why is it ignoring database dates that drop between dates selected by the user? The columns are set as DATE in the database and have been correctly entered.
You said "User searches for booked items from 14th to 16th."
That means your query will be
AND
booked_from
BETWEEN "2010-04-14" AND "2010-04-16"
AND
booked_till
BETWEEN "2010-04-14" AND "2010-04-16"
The first AND clause will obviously be false (since your 4/13 start date is NOT indeed between 4/14 and 4/16)
The correct logic for you (assuming you want the FULL booking interval) is
AND booked_from >= "2010-04-14"
AND booked_till <= "2010-04-16"
Or if you want partial interval
AND booked_from >= "2010-04-16" -- means we booked before the end of interval
AND booked_till <= "2010-04-14" -- means we left after the start
are there recordsets with the same id in both table which falls under the clause? also make sure that with WHERE col BETWEEN a AND b, a has to be smaller than b to work.
what gets returned if you comment out the last 6 lines?

MySQL AVG(COUNT(*) - Orders By day of week query?

This query has baffled me... I've searched the web work over a day now and I have tried numerous things.
I want to get the avg number of orders for every day of the week from my db. I can pull the total # with COUNT just fine. But I just can't figure out how to get the AVG of COUNT on a GROUP BY. I've tried subqueries... functions... everything... nothing works... maybe someone can throw me a bone.
Here is the query I started with below. I know AVG(COUNT(*)) won't work but I'll leave it at that because it shows what I want to do.
SELECT
AVG(COUNT(*)) AS avgorders,
SUM(total) AS ordertotal,
DAYNAME(STR_TO_DATE(order_time,'%m/%d/%Y %H:%i')) AS day
FROM data
GROUP BY day
ORDER BY DAYOFWEEK(STR_TO_DATE(order_time,'%m/%d/%Y %H:%i')) ASC
To get the average you don't need the grand totals for each day, you need multiple daily totals for each day.
Day | Count
__________________
Monday 5
Tuesday 4
Monday 6
Tuesday 3
... ...
Then you can average those numbers. I.e (5+6)/2 for Monday.
Something like this should work:
SELECT day_of_week, AVG(order_count) average_order FROM
(
SELECT DAYNAME(order_date) day_of_week,
DAYOFWEEK(order_date) day_num,
TO_DAYS(order_date) date,
count(*) order_count
FROM data
GROUP BY date
) temp
GROUP BY day_of_week
ORDER BY day_num
UPDATE: I was originally wrong. Group the inner SELECT by the actual date to get the correct daily totals. For instance, you need to get how many orders happened Monday (2/1/10) and Monday (2/8/10) separately. Then average those totals by the day of the week.
This will do, assuming that order_time is date or datetime field ( everyone would be hapier this way ;) ). Of course there is some approximation, because oldest order can be in Friday and newest in Monday, so amount of every day of week isn't equal, but creating separate variable for every day of week will be pain in the ass. Anyway I hope it will be helpful for now.
SET #total_weeks = (
SELECT
TIMESTAMPDIFF(
WEEK,
MIN(order_time),
MAX(order_time)
)
FROM data
);
SELECT
DAYNAME(order_time) AS day_of_week,
( COUNT(*) / #total_weeks ) AS avgorders,
COUNT(*) AS total_orders
FROM
data
GROUP BY
DAYOFWEEK(order_time)
I know this is old, but i was searching for a similar solution hoping to find something someone else had used. In hopes of not doing a sub query, i came up with the below and would love any feed back!
SELECT dayofweek(`timestamp`) as 'Day',count(`OrderID`)/count(DISTINCT day(`timestamp`)) as 'Average' FROM `Data` GROUP BY dayofweek(`timestamp`)
The idea is to divide the total orders on a given day of the week, by the total number of "Mondays" or whatever day it is. What this does not account for would be any days that had zero orders would not lower the average. That may or may not be desired depending on the application.
What you are asking doesn't make sense to me... AVG is an aggregate function and so is COUNT. What's wrong with the query above but just use: COUNT(*) AS avgorders?
Lets say you had 3 rows for day1, 2 rows for day2, 5 rows for day3, and 9 rows for day4... do you want to get back a single row result that tells you:
avgorders = (3 + 2 + 2 + 5 + 9) / 5 = 21 / 5 = 4.2
ordertotal = (3 + 2 + 2 + 5 + 9) = 21
I don't think you can get that in a single query, and you'd be better off doing the second round of aggregation in a server side language like PHP operating on the results of the first aggregation.

Categories