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
Related
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 retrieved data from my table by summing result and show in table.I want to show sum result record between date='2014-01' and date='2014-04' in 2014-04 column name.And then,I want to retrieve other sum result in 2014-05 to current date.
How can I do that?
SELECT MONTH(regist_timestamp) AS Month, YEAR(regist_timestamp) AS Year,
count(id) AS numberOfVisits FROM ash_members WHERE
DATE(regist_timestamp) < CURDATE() GROUP BY
DATE_FORMAT(`regist_timestamp`,'%Y/%m' )
Format the date on SELECT then GROUP BY. Try with -
SELECT MONTH(regist_timestamp) AS Month, YEAR(regist_timestamp) AS Year,
count(id) AS numberOfVisits, DATE_FORMAT(`regist_timestamp`,'%Y/%m' ) formatted_date
FROM ash_members
WHERE DATE(regist_timestamp) < CURDATE()
GROUP BY formatted_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 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.
I have a field on my table which is called X, and I store unixtime(php -> time()).
My question is:
How can I list all the months from my DB, something like: 6.2009 8.2009 etc..
And my second question is:
How can I make a query to list all the informations based on a month and year on the same field X(i store unixtime), I know this doesn't work, but mabe you can understand it better: where date = '06.2009'
Any solutions?
List all months:
SELECT DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(X)) FROM MyTable;
Return all rows with a given year & month:
SELECT * FROM MyTable
WHERE EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(X)) = 200906;
Try this
SELECT FROM_UNIXTIME(X,'%m.%Y') FROM TABLE GROUP BY FROM_UNIXTIME(X,'%m.%Y')
SELECT * FROM TABLE WHERE FROM_UNIXTIME(X,'%m.%Y')='06.2009'
Bye
To answer your first question, you would do this with grouping:
SELECT FROM_UNIXTIME(timestamp_column, '%c.%Y') AS month, some_other_column FROM table_name GROUP BY month;
As for your second question, this depends on what you're trying to do. For example:
SELECT AVG(payment), SUM(*), FROM_UNIXTIME(timestamp_column, '%c.%Y') AS month, some_other_column FROM table_name WHERE timestamp_column BETWEEN UNIX_TIMESTAMP(200906) AND UNIX_TIMESTAMP(200907) - 1 GROUP BY month;
Would return the average of the payments and the number (sum) of rows for each group.
To get information ungrouped from a specific timeframe, reduce the query like so:
SELECT payment, some_other_column FROM table_name WHERE timestamp_column BETWEEN UNIX_TIMESTAMP(200906) AND UNIX_TIMESTAMP(200907) - 1;