I have a table with two fields - a field for "date" entered as day/month/year and a field "rate"
date | rate
24/01/05 | 1.9754
26/01/05 | 1.3723
...
and so on
So, I like to find minimum and maximum values of "rate" for each month of year(s). My query selects only one row
SELECT DISTINCT DATE_FORMAT(date, '%d-%m-%y') as Date, MIN(rate) as r, MAX(rate) as mr FROM rates
This will get the rate values for each month and year
SELECT
YEAR(date) AS thisYear,
MONTH(date) AS thisMonth,
MIN(rate) AS minRate,
MAX(rate) AS maxRate
FROM rates
GROUP BY thisYear ASC, thisMonth ASC
If you need to have the individual date(s) on which the min or max occurs you'll need some additional grouping in there, however the above should suffice for your original question.
To get the minimum and maximum values of "rate" for each month of year, you can modify the query as follows:
SELECT DATE_FORMAT(date, '%m-%Y') as Month_Year, MIN(rate) as Min_Rate, MAX(rate) as Max_Rate
FROM rates
GROUP BY DATE_FORMAT(date, '%m-%Y');
The GROUP BY clause groups the rows in the rates table based on the month and year of the date field. The MIN and MAX functions then calculate the minimum and maximum values of the rate field for each group. The DATE_FORMAT function is used to format the date field into a string with the format %m-%Y, where %m is the month (01-12) and %Y is the year (e.g., 2005).
This query will return a result set with one row for each unique month and year in the rates table, along with the minimum and maximum values of the rate field for that month and year.
Related
I have a table with 4 fields as follows
I want an Sql query to calculate a total salary for each month, where the result will be as follows
Do you just want group by and sum()?
select sum(salary) salary, month
from mytable
group by month
we have a mysql table with the below fields
employeename, shift from date todate
111aaa, B, 2018-09-01 2018-09-30
222bbb, C, 2017-04-01 2030-12-01
how can i write a query like when i give particular employee number and particular date like 2018-09-20 it must show his shift as B as the date range fall under about 1st row date range.
pl. help me.
You could try this;
SELECT * FROM employees WHERE employeename = '111aaa' AND fromdate <= '2018-09-02' AND todate >= '2018-09-02' ORDER BY fromdate DESC LIMIT 1
This selects an employee who's employname is '111aaa' and where a supplied date '2018-09-02' is between the fromdate and todate on the employee record. It sorts the results with the latest fromdate first, and only returns a single result.
I have a table which has product quantity column and the database has multiple entries in a single month.
Date format is (YYYY-MM-DD)
Date Quantity
2016-03-01 1200
2016-03-05 200
2016-04-05 500
2016-04-10 1000
2016-05-05 850
2016-05-10 50
So I want data as:
March (2016-03-01 to 2016-03-31) = 1400
April (2016-04-01 to 2016-04-30) = 1500
May (2016-05-01 to 2016-05-31) = 900
How can I do this?
Use the following query and it will return the result that you want.
' date field ' is the name of the column where date is inserted and Tablename is the name of the Table.
SELECT MONTH(date field) as month , YEAR(date field) as year , SUM(quantity) as
quantity FROM Tablename GROUP BY MONTH( date field )
select MONTH(Date) As dt , SUM(quantity) total from #Table1
GROUP BY MONTH(Date)
You can use DATE_FORMAT to convert the date field into just the year-month format, select the SUM of the quantity field, and then GROUP_BY the year-month field.
SELECT DATE_FORMAT('%Y-%m', `date`) as `ym`, SUM(`quantity`) FROM `table` GROUP BY `ym` ORDER BY `ym` ASC
You can try grouping by the month and year, taking the sum of the quantity field as an aggregate.
SELECT MONTH(Date), YEAR(Date), SUM(quantity)
FROM yourTable
GROUP BY MONTH(Date), YEAR(Date)
If you want the fancy output with actual date ranges, that would be a bit more work to do. Those dates may not exist in your original data set, so it could require a date table. And handling February in a leap year could be a real pain.
Use the following query same as it is it will return what you want
SELECT CONCAT(DATE_FORMAT(date,'%b'),' ',
(SELECT CONCAT('(',YEAR(date),'-',LPAD(MONTH(date),2,0),'-01 - ')
), (SELECT concat(last_day(date),')')
)) dates, SUM(quantity) qty FROM DATES GROUP BY MONTH(date) ORDER BY MONTH(date)
I have form with 4 fields namely(start month, start year, end month, end year)
and MySQL table structure is like id, customer id, amount, month, year
Now, I need to display the rows with matching condition as between the start month and year and end month and year.
I tried this query
select id,customer id,concat(month,'-',year) as d1 from payroll where
empid='$_POST[emp_id]' and (STR_TO_DATE(d1,'%m-%Y') between
STR_TO_DATE('$_POST[fmonth]-$_POST[fyear]','%m-%Y') and
STR_TO_DATE('$_POST[tmonth]-$_POST[tyear]','%m-%Y'))
Please advise....
Assuming the columns year and month and the 4 form fields are integers, this would work:
SELECT id
, customer id
, CONCAT(month, '-', year) AS d1
FROM payroll
WHERE empid = '$_POST[emp_id]'
AND (year, month) >= ( '$_POST[fyear]', '$_POST[fmonth]' )
AND (year, month) <= ( '$_POST[tmonth]', '$_POST[tyear]' )
You should probably take care of those $_POST[] before sending them to the database for security reasons (SQL injection).
A compound index on (empid, year, month) would help performance.
I want to count totals for all mondays, tuesdays, etc. for any given (user selected) date range, StartDate (sDate) through EndDate (eDate). My current query shows all days within range and I'd like it to SUM my table "count" from each day as it is but display total of each day of week as one occurrence. Using image sample, I want my result to be:
Monday - 164
Tuesday - 139
Wednesday - 261
etc.
SQL
SELECT areaname,created, SUM(count) as totals FROM reports WHERE created
between '".$sDate."' AND '".$eDate."' AND area = '".$tArea."' GROUP BY areaname,
created ORDER BY id ASC
html
foreach ($counts as $row) { ?>
<tr><td><?php echo date('l',strtotime($row['reports']['created'])); ?></td>
<?php echo $row[0]['totals']; ?>
}
You can use the DATE_FORMAT() function to get the day of the week from a given date, then you can group by that. The syntax is DATE_FORMAT(date, '%W'). So:
SELECT areaname,
DATE_FORMAT(created, '%W') AS weekday,
SUM(count) AS totals
FROM reports
WHERE created BETWEEN '".$sDate."' AND '".$eDate."'
AND area = '".$tArea."'
GROUP BY areaname, weekday
ORDER BY id
Note that I left in areaname in the GROUP BY, so this will return one result for every combination of areaname/weekday. If you want totals for all areanames, just remove areaname from the GROUP BY clause.