I am having a table in MYSQL in which among other fields i am having two fields one for Month (Varchar) and year (int). Months field is used to store the name of the month and year is used to store the year of the entered data. I am confused that how can use the where class to display only the records of max month and year.
For Example
10 records are there for month January 2013
50 records are there for month February 2013
.
.
.
100 records are there for the month of November 2014 --- (LAST ENTRY)
Now
HERE I want to display only the November 2014 records. My code is written in such a way that i can not use the select query, I have to do it using WHERE CLAUSE
SELECT *
FROM tablename
WHERE
(year, month) = (SELECT year, month
FROM tablename
ORDER BY
STR_TO_DATE(CONCAT_WS(' ', '01', month, year), '%d %M %Y') DESC
LIMIT 1)
select count(*), year, month
from your_table
group by year, month
order by year,
case when month = 'January' then 1
when month = 'February' then 2
when month = 'March' then 3
when month = 'April' then 4
when month = 'May' then 5
when month = 'June' then 6
when month = 'July' then 7
when month = 'August' then 8
when month = 'September' then 9
when month = 'October' then 10
when month = 'November' then 11
when month = 'December' then 12
end desc
limit 1
Try This
select Count(*),month,year from tablename
group by STR_TO_DATE(CONCAT_WS(' ', '01', month, year), '%d %M %Y') order by
STR_TO_DATE(CONCAT_WS(' ', '01', month, year), '%d %M %Y')
Related
For each month (except January) I would like to find the difference in quantity from the previous month.
I have a table:
Month
Qty
January
4
February
3
March
9
April
3
May
7
and I would like to return:
Month
Difference
February
-1
March
6
April
-6
May
4
I'm using an older version of MySQL, so I can't use LEAD/LAG for this.
SELECT t1.`Month`, COALESCE(t1.Qty - t2.Qty, 'Unknown') Difference
FROM table t1
LEFT JOIN table t2
ON STR_TO_DATE(CONCAT(t1.`Month`, ' 01 2021'), '%M %d %Y')
= STR_TO_DATE(CONCAT(t2.`Month`, ' 01 2021'), '%M %d %Y') + INTERVAL 1 MONTH
You can use a correlated subquery to get the previous value. If the first column is really ordered, then you can use:
select t.*, (qty - prev_qty) as difference
from (select t.*,
(select t2.qty
from t t2
where t2.month < t.month
order by t2.month desc
limit 1
) as prev_qty
from t
) t
where prev_qty is not null;
If you are really storing months as a string name, then you need to convert to something orderable:
select t.*, (qty - prev_qty) as difference
from (select t.*,
(select t2.qty
from t t2
where str_to_date(concat(t2.month, ' 1 2000', '%M %d %Y') < str_to_date(concat(t.month, ' 1 2000', '%M %d %Y')
order by str_to_date(concat(t2.month, ' 1 2000', '%M %d %Y') desc
limit 1
) as prev_qty
from t
) t
where prev_qty is not null;
I need to get the total sum of the mark of previous 3 months. that is if I look on April I would get the sum of Jan, Feb, Mar. Similarly, When I look on Jan it displays the total of previous year Oct, Nov, Dec. I use the below query
SELECT empid,
name,
sum(total) AS total
FROM formresult
WHERE (MONTH(date) = MONTH(now())
OR MONTH(date) = MONTH(now()- INTERVAL 1 MONTH)
OR MONTH(date) = MONTH(now()- INTERVAL 2 MONTH))
AND YEAR(date) = YEAR(now())
GROUP BY empid
But when using this code I got 3-month result but when I look on Jan it does not display previous year result. How could it possible. Please Help me
Something like the following should do what you need.
SELECT empid, name, SUM(total) AS total
FROM formresult
WHERE date BETWEEN
DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 3 MONTH), '%Y-%m-01') AND
LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH))
GROUP BY empid
See DATE_FORMAT(), DATE_SUB(), and LAST_DAY() for more information.
So there are a few issues with the date math that's being done here. First it looks like you are getting the current month and previous two months instead of the previous three months. This is getting you the current month MONTH(date) = MONTH(now()). Also you can't assume it's the current year, if you run this on January you'd expect to get October, November and December of the previous year however it will return for the current year.
I want to query row to view registered record based on previous quarterly month, for example:
current month / year: Jan 2018
the query should show all records by registered month which is: Oct 2017, Jul 2017, Apr 2017 and so on.
I use below query but only can select rows for last 3 month.
SELECT name, date, amount, agreement, bank
FROM `account`
WHERE (YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 3 MONTH) AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 3 MONTH))
You can do arithmetic like this:
select a.*
from account a
where mod(month(date), 3) = mod(month(current_date), 3);
You seem to understand how to handle the year component, but something like:
and date >= curdate() - interval 1 year
Try This One, Manually
select *
from account
Where current_date >= dateadd(year, -1, Getdate())
And Month(current_date) In (
Month(Getdate()),
(Case When Month(Getdate())+3 > 12 Then Month(Getdate())+3-12 Else Month(Getdate())+3 End),
(Case When Month(Getdate())+6 > 12 Then Month(Getdate())+6-12 Else Month(Getdate())+6 End),
(Case When Month(Getdate())+9 > 12 Then Month(Getdate())+9-12 Else Month(Getdate())+9 End)
)
I have a table with columns day, month, year, total_payments. And I have to calculate total_payments according to days, months, years.
How can I calculate values?
I am thinking code like :
select
month, year, sum(total_payments)
from
webassignment.subscription_stats
group by
day;
select
month, year, sum(total_payments)
from
webassignment.subscription_stats
group by
month;
select
month, year, sum(total_payments)
from
webassignment.subscription_stats
group by
year;
but it will not return the correct answers. And I want to calculate total_payments daywise, monthwise, yearwise. Please help me to find values.
Sample input :
Day Month Year Total_payments
10 01 2008 10
10 01 2008 20
11 02 2008 10
10 03 2010 10
Output:
Daywise :
day month year total_payments
-----------------------------
10 01 2008 30
11 02 2008 10
10 03 2010 10
Same for month and yearwise
You can get a summary with Totals by Year, Month and Day using GROUP BY WITH ROLLUP:
SELECT
`Year`,
`Month`,
`Day`,
SUM(`Total_payments`) as `Totals`
FROM `webassignment`.`subscription_stats`
GROUP BY `Year`,`Month`,`Day` WITH ROLLUP;
If you want individual queries for Year, Month and Day:
By Year:
SELECT
`Year`,
SUM(`Total_payments`) as `Totals`
FROM `webassignment`.`subscription_stats`
GROUP BY `Year`
ORDER BY `Year;
By Month:
SELECT
`Year`,
`Month`,
SUM(`Total_payments`) as `Totals`
FROM `webassignment`.`subscription_stats`
WHERE `Year` = 2017
GROUP BY `Year`,`Month`
ORDER BY `Year`,`Month`;
By Day:
SELECT
`Year`,
`Month`,
`Day`,
SUM(`Total_payments`) as `Totals`
FROM `webassignment`.`subscription_stats`
GROUP BY `Year`,`Month`,`Day`
ORDER BY `Year`,`Month`,`Day`;
A way could be the use on an union for show the different result based on different group by level
select day, month,year,sum(total_payments)
from webassignment.subscription_stats group by day, month,year
union
select null, month,year,sum(total_payments)
from webassignment.subscription_stats group by month,year
union
select null, null ,year,sum(total_payments)
from webassignment.subscription_stats group by year
order by year, month, day
for the sample you provided should be enough
select day, month,year,sum(total_payments)
from webassignment.subscription_stats group by day, month,year
order by day, month,year
I have one table (but at many locations):
DATE STUFF
-------------------
2011-12-01 DATA
2011-12-02 DATA
2011-12-03 DATA
...
2011-12-31 DATA
2012-01-01 DATA
2012-01-02 DATA
My table covers multiple years from 2005 to 2012. I want to get AGGREGATE Function values, i.e., SUM/AVG/MAX/MIN, for each month within each year. Easy:
GROUP BY DATE_FORMAT(DATE, '%Y'), DATE_FORMAT(DATE, '%m')
I want go do the same for 3-month time periods within those years... this works for all but one:
GROUP BY DATE_FORMAT(DATE, '%Y'), DATE_FORMAT(DATE, '%m') IN (12, 01, 02)
My other time periods work, because they are in the same year (03, 04, 05), (06, 07, 08), and (09, 10, 11)... but the GROUP BY above is grouping December of 2012 with January/February of 2012. My time period has to be December 2011 and January/February 2012, December 2010 and January/February 2011... ...
I want to keep this generic, so I don't have to update with date spans, in order to put the code in a stored procedure for multiple locations.
I've tried to join the table to it self by shifting the year ahead by one if MONTH 12. This yielded undesirable results.
GROUP BY floor((month(DATE) + year(DATE) * 12 -1 + %month_shift%) / %month_group_period%)
where %month_group_period% is three (3) in your example
and %month_shift% is one (1) to obtain december, january, february together, and so on
EDIT: this works for 5 month period too (if you want)
I'm assuming a little bit here about what you really want to do with DATE_FORMAT(DATE, '%m') IN (12, 01, 02), but:
SELECT IF(DATE_FORMAT(DATE, '%m') = 12, DATE_FORMAT(DATE, '%Y') + 1, DATE_FORMAT(DATE, '%Y')) AS yr,
CASE DATE_FORMAT(DATE, '%m')
WHEN 12 THEN 1
WHEN 1 THEN 1
WHEN 2 THEN 1
WHEN 3 THEN 2
WHEN 4 THEN 2
WHEN 5 THEN 2
WHEN 6 THEN 3
WHEN 7 THEN 3
WHEN 8 THEN 3
WHEN 9 THEN 4
WHEN 10 THEN 4
WHEN 11 THEN 4
END AS qtr
FROM ...
GROUP BY IF(DATE_FORMAT(DATE, '%m') = 12, DATE_FORMAT(DATE, '%Y') + 1, DATE_FORMAT(DATE, '%Y')),
CASE DATE_FORMAT(DATE, '%m')
WHEN 12 THEN 1
WHEN 1 THEN 1
WHEN 2 THEN 1
WHEN 3 THEN 2
WHEN 4 THEN 2
WHEN 5 THEN 2
WHEN 6 THEN 3
WHEN 7 THEN 3
WHEN 8 THEN 3
WHEN 9 THEN 4
WHEN 10 THEN 4
WHEN 11 THEN 4
END
I don't think you want to use GROUP BY how you are trying to do it. You would probably want to write your queries like this
SELECT MONTH(date) as `month`, SUM(stuff) as `sum`
FROM table
WHERE YEAR(date) IN ('2010', '2011', 2012')
GROUP BY `month`
The GROUP BY clause is not where you should be filtering/formatting your data.
Of course, you could use whatever aggregate function you want instead of SUM. Also you can omit the WHERE clause for all years, or modify the years to be included as needed.
If you want year and month, you could simply modify the query like this:
SELECT YEAR(date) as `year`, MONTH(date) as `month`, SUM(stuff) as `sum`
FROM table
WHERE `year` IN ('2010', '2011', 2012')
GROUP BY `year`, `month`
You need to subtract one month from the date for the group by:
GROUP BY DATE_FORMAT(date_add(DATE, interval 1 month), '%Y'),
DATE_FORMAT(date_add(date, interval 1 month), '%m') IN (12, 01, 02)
You will probably also have to modify the select.
select DATE_FORMAT(date_add(date_add(DATE, interval 1 month), interval -1 month), '%Y'),
DATE_FORMAT(date_add(date_add(date, interval 1 month), interval -1 month), '%m')
I would try something like this:
GROUP BY DATE_FORMAT(IF(MONTH(`DATE`)=12,DATE_ADD(`DATE`,INTERVAL 1 YEAR),`DATE`),'%Y')
, DATE_FORMAT(`DATE`,'%m')
Basically, if month is 12, then add 1 year to it.
Actually, I would tend to do it something like this:
GROUP BY DATE_FORMAT(DATE_ADD(`DATE`,INTERVAL IF(MONTH(`DATE`)=12,1,0) YEAR),'%Y')
, CASE WHEN MONTH(`DATE`) IN (12,1,2) THEN 1
WHEN MONTH(`DATE`) IN (3,4,5) THEN 2
WHEN MONTH(`DATE`) IN (6,7,8) THEN 3
WHEN MONTH(`DATE`) IN (9,10,11) THEN 4
END