get data for the year within from and to dates - mysql

I have the following query which gets year,Sum data between from and to date.
It works perfectly.
SELECT strftime('%Y',Date), SUM(Amount)
from table where (Date between '2008-08-01' and '2012-07-31')
GROUP BY strftime('%Y',Date)
I am displaying the sum on a bar chart.
Now If I click on the respective year it should show all the transactions till the "To Date" for that year or if the value is smaller than "To date" then the entire year's transaction.
For Eg: In the example above it displays Sum From Date : 2008-08-01 and To Date: 2012-07-31 .
If the user taps on the year 2012 bar column it should show transactions till the date 2012-07-31 and not for the entire year but if the user taps on 2011 it should transaction for the entire year as the TO Date is greater than 2011 year.
Same for the year 2008, it should display data from "2008-08-01" not the entire year and if I tap the bar column of that year it should show the transactions from that particular date till the end of the year and not for the entire year.
I have the query below which returns wrong value .
SELECT strftime('%Y',Date) ,*
from table where (Date between '2011-08-01' and '2012-07-31')
and strftime('%Y',Date)='2012'
GROUP BY strftime('%Y',Date)
This returns only one row for the date 2012-01-01 not all the values for the year 2012 till 2012-07-31.
Basically I would like to change only year in the following query and rest should be same. strftime('%Y',Date)='2012'
I don't know how to fix it or How to go about it w.r.t the above requirement.
Please help me fix it or Any Alternative. Thanks in Advance.

You don't need GROUP BY
SELECT strftime('%Y',Date) ,*
from table where (Date between '2011-08-01' and '2012-07-31')
and strftime('%Y',Date)='2012'

Related

MySQL - get data for time before some quarter AND year

I need to get MySQL results for users records with 'registered' date that happens BEFORE some quarter for some year.
For example to get data (users count, registered before Q3 2018) I do this request:
SELECT count(userid) AS value
FROM users
WHERE QUARTER(registered) <= "3" && YEAR(registered) <= "2018"
However this request give me not expected results (value much lower that it should be, looks like I get results only for Q3 for 2018 year, and without values for previous years with any quarters). If I remove quarter and check just data before Year - this works fine and show correct results for every year that I use.
Why this happens and how to correctly get value with Quarter?
You can use the following solution:
SELECT COUNT(userid) AS value
FROM users
WHERE YEAR(registered) < 2018 OR (
YEAR(registered) = 2018 AND QUARTER(registered) <= 3
)
With your current SELECT you get only all users which registered on third quarter or ealier on every year (2018 and earlier). But you can count all users of the years before 2018 (the quarter doesn't matter in this case) and all users of the current year but in the specified quarter or ealier.

How to sum order total amount by week

I am trying to retrieve last 3 months records. I need to sum order total amount by week. I have made following query.
select CONCAT("Week", WEEK(created_at)) as week_number, sum(total_cost)
from certified_diamond_orders
where created_at > 2016-11-22
and status = "delivered"
group by week("created_at")
But I am only getting one record with this. Infact my table has 2 years entries. Also I was trying to figure out how I can pull week start date and end date to diplay on my chart.
Any suggestions where I am making mistake?
week("created_at") looks like you're trying to determine the week of the string "created_at" rather than the column created_at. Which might explain why you're only getting one row in your result.
The date 2016-11-22 also looks suspiciously like a sum instead of a date (2016 - 11 - 22 = 1983 vs "2016-11-22"
Try this:
SELECT
CONCAT('Week', WEEK(created_at)) AS week_number,
SUM(total_cost)
FROM certified_diamond_orders
WHERE
created_at > '2016-11-22' AND
status = 'delivered'
GROUP BY WEEK(created_at)

Get the last row from every year using mysql query

I need to get the row where the due_date field has the last month in every year.
For eg: if I have 3 entries with due_date field like 2014-5-21,2014-6-21,2014-7-21
I need the last row in year 2014, that will be 2014-7-21, like wise in 2015 and the following years.
Can someone help me out with this.
I tried but nothing worked out
SELECT distinct(year(due_date)) FROM `vw_mortgage_repayment_schedule_org`
where mortgage_id ='AREM-1408614735-VLASFAQ8VI'
and month(due_date) = max(month())
I need all the last rows for the given mortgage of every year eg- 2014,2015,2016 etc
I think if you group by the year of the due_date, that might just about give you what you need, given that we search for the max month in the select, and group by the year. Possibly. Can we have your table structure?
SELECT year(due_date), month(max(due_date)), max(due_date)
FROM `vw_mortgage_repayment_schedule_org`
where mortgage_id ='AREM-1408614735-VLASFAQ8VI'
GROUP BY year(due_date)

Grouping months by quarter over multiple years depending on a dynamic start month

Using MySQL and PHP I am building a JSON array to populate a data table.
For the purposed of my question suppose the data table has the following columns:
Year: 2010,2011,2012,2013...<br/>
Month: 1,2,3,4,5...<br/>
Value: 100, 150, 200 etc...<br/>
The table structure cannot be altered and my solution needs come into the MySQL query
The data can be viewed either monthly, quarterly or yearly. Monthly and yearly is achieved easily through grouping by year and month.
Quarterly data can be grouped by calendar quarter (Jan-Mar, Apr-Jun, Jul-Sep, Oct-Dec) by this group statement:
GROUP BY year, round((month/3)+0.3,0)
So where Jan, Feb and March might all have 100 for their value the summed result is 300, same for other months.
Now my problem comes when I want to group values by a financial quarter, for example a quarter that starts in Feb, or any other quarters.
I have a statement that works for the quarter grouping using two variables that can be accessed via the SQL query, start_year (i.e. 2014) and start_month (i.e. 2)
GROUP BY year, (((round(((((month-(start_month-1))+((year-start_year)*12))-((year-start_year)*12))/3)+0.33,0)/4)+4)-floor(((round(((((month-(start_month, '%m')-1))+((year-start_year)*12))-((year-start_year*12))/3)+0.33,0)/4)+4)))*12
which basically will assign a 0,3,6,9 value to each calendar month for the purposes of grouping.
In the financial year starting February this works fine for quarters 1-3, however breaks for the final quarter as it includes Nov and Dec 2014 data and Jan from 2015.
As a result I get 5 rows of data instead of 4.
This is because of the preceding GROUP by year clause, an important addition as we might want to generate a table that views sequential quarters for multiple years.
So what I am looking for is a way of grouping the years together by offsetting the start month.
So when the year starts in Jan it will group Jan-Dec but if we change that to starting Feb it will group Feb-Jan.
Any ideas, suggestions most welcome!
Regards,
Carl
I solved a similar problem just now (a Moodle report aggregating assignment scores by year and quarter) with something like this:
select year(from_unixtime(s.timemarked)) as year, quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter, count(distinct data1) as "tickets graded" from mdlassignment_submissions s where grade >= 0 group by year, quarter order by year, quarter;
The relevant part for what you're doing is quarter(from_unixtime(s.timemarked)) % 4 + 1 as quarter
As another commenter pointed out, MySQL has a quarter() function, but it doesn't do financial quarters. However, since (as I understand it, at least, based on consulting the relevant wikipedia page) financial quarters are just offset by 1, the % 4 + 1 at the end should convert it.

generate reports in MYSQL between 2 dates grouped by week

I have a staff timesheet table where i have timestamp of when those records are created. I now want to generate the report so that my start date is Tuesday and end date is next Monday, which is 1 week. Now i need to generate all the records grouped by this weeks time but will be next set of tuesday to monday.
This is like normal GROUP BY WEEK(Timestamp) but the WEEK numbers are not the default ones i need to generate the reports in this custom duration. I have a query working for this which groups the record efficiently by Week 1, week 2, week 3 etc.. which is picked from default mysql calendar i guess. How can i change that to generate reports grouped by custom weeks ?
Can you tel me how the following works as how the dates are picked up ?
SELECT WEEK(pw.date) AS Date,DATE_FORMAT(pw.date,'%d-%m-%Y') AS post_date,
SUM(wages) AS amount,SUM(pw.hours) AS hours,SUM(pw.minutes) AS minutes
FROM pos_sessions pw
GROUP BY YEAR(pw.date), WEEK(pw.date) ORDER BY pw.date DESC
I think this Other Solution is what you are looking for...