I am trying to get data from table, as shown on the code. Instead of ThisYear and LastYear; I want to display Year number depending on current year. I want to try use something like "Year(curdate())" and "Year(curdate()) - 1"; and column headers will be 2017 and 2016 instead of ThisYear and LastYear.
SELECT Months AS Months,
SUM(if(Years = Year(curdate()) AS ThisYear,
SUM(if(Years = Year(curdate())-1, in_amount, null)) AS LastYear,
FIELD(Months, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December') AS `FIELD(Months, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')` FROM ao_vw_revenue GROUP BY Months ORDER BY FIELD(Months, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
Any help? Thanks.
Related
Hi i have the following mysql data
INSERT INTO `monthly` (`id`, `year`, `stat_id`, `cat_id`, `January`, `February`, `March`, `April`, `May`, `June`, `July`, `August`, `September`, `October`, `November`, `December`) VALUES
(1, '2017', '12', '25', '1', '3', '1', '1', '3', '4', '4', '2', '4', '', '', ''),
and i would like it to be convert to be like this
INSERT INTO `monthlydata` (`id`, `year`, `monthName`, `stat_id`, `cat_id`, `data`) VALUES
(1, '2017', 'January', '12', '25', '1'),
(2, '2017', 'February', '12', '25', '3'),
(3, '2017', 'March', '12', '25', '1'),
(4, '2017', 'April', '12', '25', '1'),
(5, '2017', 'May', '12', '25', '3'),
(6, '2017', 'June', '12', '25', '4'),
(7, '2017', 'July', '12', '25', '4'),
(8, '2017', 'August', '12', '25', '2'),
(9, '2017', 'September', '12', '25', '4'),
(10, '2017', 'October', '12', '25', ''),
(11, '2017', 'November', '12', '25', ''),
(12, '2017', 'December', '12', '25', ''),
is there an easier way to do this using mysql/php
You need to UNPIVOT your data. MySQL doesn't have a built in function to do that so you'll need to use multiple queries.
INSERT INTO `monthlydata` (`id`, `year`, `monthName`, `stat_id`, `cat_id`, `data`) VALUES
SELECT id, year, 'January', stat_id, cat_id, January
FROM monthly WHERE monthName = 'January'
UNION ALL
SELECT id, year, 'February', stat_id, cat_id, February
FROM monthly WHERE monthName = 'February'
UNION ALL
SELECT id, year, 'March', stat_id, cat_id, March
FROM monthly WHERE monthName = 'March'
.....
ID column here might cause issues. Depending on how you have defined it. If it is auto generated then you can remove it from the INSERT and let it be auto generated. Since you'll have rows for all months with same ID, you need to handle that scenario.
trying to get max value of months column in a single row
for example i want to get max value from jan~ dec from first column which is currently 5 is the max value
preferably do it in laravel
See the PHP max function
To get the month name(s) (remember more than one month could have the maximum value), you'll need to implement a loop.
$max_day = max(
$record->jan,
$record->feb,
$record->mar,
$record->apr,
$record->may,
$record->jun,
$record->jul,
$record->aug,
$record->sep,
$record->oct,
$record->nov,
$record->dec
);
$columns = [
'jan' => 'January',
'feb' => 'February',
'mar' => 'March',
'apr' => 'April',
'may' => 'May',
'jun' => 'June',
'jul' => 'July',
'aug' => 'August',
'sep' => 'September',
'oct' => 'October',
'nov' => 'November',
'dec' => 'December',
];
$max_months = [];
foreach ($columns as $column_name => $month_name) {
if ($record->{$column_name} === $max_day) {
$max_months[] = $month_name;
}
}
// $max_months e.g. ['January', 'February']
i have a table collaborator that contains:
the columns : hiring_date and release_date ( date type)
i would like to calculate the total number of collaborators per month of a year
using hiring_date and release_date
I guess you are asking for something like this but please, try to give more information when asking
SELECT COUNT(*) FROM collaborator WHERE hiring_date >= date AND release_date<= date
You can use "GROUP BY" for this.
e.g., To calculate the total number of collaborators per month using hiring_date :
SELECT MONTH(hiring_date),COUNT(*) FROM `collaborator`
GROUP BY MONTH(hiring_date);
To calculate the total number of collaborators per month of a year using release_date, use :
SELECT MONTH(release_date),COUNT(*) FROM `collaborator `
GROUP BY MONTH(release_date)
WHERE YEAR(release_date) = <year>;
this code is to get number of hiring collaborators on 2016 per month
SELECT
SUM(IF(month = '1', total, 0)) AS 'Jan',
SUM(IF(month = '2', total, 0)) AS 'Feb',
SUM(IF(month = '3', total, 0)) AS 'Mar',
SUM(IF(month = '4', total, 0)) AS 'Apr',
SUM(IF(month = '5', total, 0)) AS 'May',
SUM(IF(month = '6', total, 0)) AS 'Jun',
SUM(IF(month = '7', total, 0)) AS 'Jul',
SUM(IF(month = '8', total, 0)) AS 'Aug',
SUM(IF(month = '9', total, 0)) AS 'Sep',
SUM(IF(month = '10', total, 0)) AS 'Oct',
SUM(IF(month = '11', total, 0)) AS 'Nov',
SUM(IF(month = '12', total, 0)) AS 'Dec'
FROM (
SELECT month(DATE_ENTREE) AS month , count(DATE_ENTREE) as total
FROM collaborateur where YEAR(DATE_ENTREE) =2016
GROUP BY month(DATE_ENTREE)) as e
and this code is the number of release per month on 2016
SELECT
SUM(IF(month = '1', total, 0)) AS 'Jan',
SUM(IF(month = '2', total, 0)) AS 'Feb',
SUM(IF(month = '3', total, 0)) AS 'Mar',
SUM(IF(month = '4', total, 0)) AS 'Apr',
SUM(IF(month = '5', total, 0)) AS 'May',
SUM(IF(month = '6', total, 0)) AS 'Jun',
SUM(IF(month = '7', total, 0)) AS 'Jul',
SUM(IF(month = '8', total, 0)) AS 'Aug',
SUM(IF(month = '9', total, 0)) AS 'Sep',
SUM(IF(month = '10', total, 0)) AS 'Oct',
SUM(IF(month = '11', total, 0)) AS 'Nov',
SUM(IF(month = '12', total, 0)) AS 'Dec'
FROM (
SELECT month(DATEE_SORTIE_COLLABORATEUR) AS month , count(DATEE_SORTIE_COLLABORATEUR) as total
FROM collaborateur where YEAR(DATEE_SORTIE_COLLABORATEUR) =2016
GROUP BY month(DATEE_SORTIE_COLLABORATEUR)) as s
my objective is to calculate total number of collaborator per month
hope this can help you to understand what i mean
I have 2 tables. Table1 and Table2.
Table1 data:
Id org_id start_date end_date month
'1', '46', '2015-01-01', '2015-01-31', 'January'
'2', '46', '2015-02-01', '2015-02-28', 'February'
'3', '46', '2015-03-01', '2015-03-31', 'March'
'4', '46', '2015-04-01', '2015-04-30', 'April'
'5', '46', '2015-05-01', '2015-05-31', 'May'
'6', '46', '2015-06-01', '2015-06-30', 'June'
'7', '46', '2015-07-01', '2015-07-31', 'July'
'8', '46', '2015-08-01', '2015-08-31', 'August'
'9', '46', '2015-09-01', '2015-09-30', 'September'
'10', '46', '2015-10-01', '2015-10-31', 'October'
'11', '46', '2015-11-01', '2015-11-30', 'November'
'12', '46', '2015-12-01', '2015-12-31', 'December'
Table2 data:
Id org_id from_date emp_id
'48', '46', '2015-06-09' 1
'49', '46', '2015-06-09' 1
'50', '46', '2015-06-01' 2
'51', '46', '2015-05-20' 1
'56', '46', '2015-07-07' 2
This is my query:
select t1.month,count(t2.emp_id) as count
from Table1 t1
left outer join Table2 t2 on t2.from_date between t1.start_date and t1.end_date
where t2.org_id=46 group by t1.month
Output i am getting is:
month count
'July', '1'
'June', '3'
'May', '1'
Output i am expecting is:
month count
'January', '0'
'February', '0'
'March', '0'
'April', '0'
'May', '1'
'June', '3'
'July', '1'
'August', '0'
'September', '0'
'October', '0'
'November', '0'
'December', '0'
I have used left outer join. But all records from left table is not getting fetched.
Any help!!
You have to put the part of your wherestatament to the on clase:
select t1.month,count(t2.emp_id) as count
from Table1 t1
left outer join Table2 t2 on t2.from_date between t1.start_date and t1.end_date
and t2.org_id=46 group by t1.month
If you do not do it you have a inner join
try this..
SELECT t1.month,COUNT(t2.emp_id) AS COUNT
FROM Table1 t1
LEFT JOIN Table2 t2 ON t2.from_date BETWEEN t1.start_date AND t1.end_date
AND t2.org_id=46 GROUP BY t1.month
I've this pivot query:
SELECT `fonte`,
MAX(IF(month= '1', sell, NULL)) AS Gen,
MAX(IF(month= '2', sell, NULL)) AS Feb,
MAX(IF(month= '3', sell, NULL)) AS Mar,
MAX(IF(month= '4', sell, NULL)) AS Apr,
MAX(IF(month= '5', sell, NULL)) AS Mag,
MAX(IF(month= '6', sell, NULL)) AS Giu,
MAX(IF(month= '7', sell, NULL)) AS Lug,
MAX(IF(month= '8', sell, NULL)) AS Ago,
MAX(IF(month= '9', sell, NULL)) AS Sett,
MAX(IF(month= '10', sell, NULL)) AS Ott,
MAX(IF(month= '11', sell, NULL)) AS Nov,
MAX(IF(month= '12', sell, NULL)) AS Dic,
FROM `pdl_dati` group by fonte
How can I insert the "total" for month='1' and month='2' and month='3' and month='4' and month='5' and month='6' and month='7' and month='8' and month='9' and month='10' and month='11' and month='12'?
Thanks
If you want a final row that has a total for each month you can use GROUP BY WITH ROLLUP:
SELECT `fonte`,
MAX(IF(month= '1', sell, NULL)) AS Gen, -- do you want this to be sum?
MAX(IF(month= '2', sell, NULL)) AS Feb,
MAX(IF(month= '3', sell, NULL)) AS Mar,
MAX(IF(month= '4', sell, NULL)) AS Apr,
MAX(IF(month= '5', sell, NULL)) AS Mag,
MAX(IF(month= '6', sell, NULL)) AS Giu,
MAX(IF(month= '7', sell, NULL)) AS Lug,
MAX(IF(month= '8', sell, NULL)) AS Ago,
MAX(IF(month= '9', sell, NULL)) AS Sett,
MAX(IF(month= '10', sell, NULL)) AS Ott,
MAX(IF(month= '11', sell, NULL)) AS Nov,
MAX(IF(month= '12', sell, NULL)) AS Dic
FROM `pdl_dati`
group by fonte with ROLLUP;
As a side note, if you are attempting to get the total for each fonte by month, I would think you would want to use sum instead of max. See Demo