Hello i am using php and mysql.I have one table with one column as recdatetime as datetime datatype .
If i search record between two months i want sum of quantity column of same table.
If i have following record.
quantity recdatetime
**44** `2014-01-01 16:53:06`
**14** ` 2014-01-21 16:53:06`
**10** `2013-12-21 16:53:06 `
**17** `2013-12-22 16:53:06 `
**29** `2013-11-20 16:53:06`
If i search between November 2013 and January 2014 i want output in following manner.
November December January
29 27 58
I want Mysql query for above output.
Start with this:
SELECT SUM(quantity) AS s, DATE_FORMAT(recdatetime, '%M') AS m
FROM table_name
GROUP BY DATE_FORMAT(recdatetime, '%Y-%m')
You can use month(recdatetime), sum(quantity) and group by month(recdatetime).
Select year(recdatetime), month(recdatetime), sum(quantity) as total from x
group by year(recdatetime),month(recdatetime);
You will need the year or it will also sum up months for different years. You normaly do not want this. And add a where condition to only get the months you want. Hope you can do that on your own.
Switching rows and columns could be done in the output.
You can try like this:
select sum(quantity) as sumQty,recdatetime FROM table_name group by Month(recdatetime
Related
Hey guys i want to create one tree based data in my html view from mysql.
I have one table salary_slip there 2 columns pay_month and pay_year.
data is like this:
pay_month pay_year
February 2014
march 2014
January 2015
February 2015
April 2015
may 2015
June 2015
January 2016
march 2016
December 2016
So now i want to create tree view for the same.so, if user will click on 2016(pay_year) onclick this all data in pay_month in front of 2016 should be display in tree format and same for the all.
My current query is:
select
PAY_MONTH,
PAY_YEAR
from
india_salary_slip_details
where
EMPLOYEE_ID = 34
group by
pay_year
By this i got my data:
but i can only see each year's first month only.
please provide me with better query so i can get my result.
image for the same:current data from my query
A major part of your problem comes from the use of GROUP BY, which is used for, well, grouping.
MySQL has different handling of GROUP BY than other DBMS' that require all non-aggregate fields to be listed in the GROUP BY clause. MySQL give you enough rope with which to hang yourself (so to speak), which is why you didn't get an error for what is clearly not an intended use of GROUP BY.
Depending on how your front end is handling the data, you have a few options.
Option 1) Have it load (ordered) and use the output loop to determine the change in year.
SQL Query:
SELECT
PAY_MONTH,
PAY_YEAR
FROM
india_salary_slip_details
WHERE
EMPLOYEE_ID = 34
ORDER BY
pay_year,
pay_month -- we want it ordered by year, then month, though this will be alphabetical due to the data type
PHP Code:
<?php
$year = NULL;
foreach ($result as $row) { // Assuming your result is stored as $result
if ($row['PAY_YEAR'] !== $year) {
// Print the year in the left column so to speak, perhaps create a sublist, etc
// Set the year
$year = $row['PAY_YEAR'];
}
// Add month
// Do something with $row['PAY_MONTH'], perhaps add it to a sublist, again, depends on front end handling.
}
Option 2) Have it load the years, then the next level asynchronously.
SQL Query to get years:
SELECT DISTINCT
PAY_YEAR
FROM
india_salary_slip_details
WHERE
EMPLOYEE_ID = 34
ORDER BY
PAY_YEAR
SQL Query to get months given a year:
SELECT
PAY_MONTH
FROM
india_salary_slip_details
WHERE
EMPLOYEE_ID = 34
AND PAY_YEAR = (Whatever the year is)
ORDER BY
PAY_MONTH
The problem is your group by, you just want to order by year :
SELECT PAY_MONTH, PAY_YEAR, FROM india_salary_slip_details WHERE EMPLOYEE_ID = 34
ORDER BY PAY_YEAR
I inserted join date for every employee in a table like March 9, 2014, 10:50:29
After the completion of month ,I need to get the data like
march 9 => 20,
march 10 =>30, .....
I tried with group by statements along with count(*) , but i didn't got my required output. please help me...Thanks in advance..
I suppose that you are using datetime field for storing the DateOfJoining.
Use this query, (Please replace table-name and fields as per yours)
SELECT DAY(joining_date) AS jdate, COUNT(*) AS jtotal
FROM your_table
WHERE MONTH(joining_date) = 3
GROUP BY jdate;
Hope it works.
SELECT DAY(joining_date) AS jdate, COUNT(*) AS jtotal
FROM your_table
WHERE MONTH(joining_date)=3 AND YEAR(joining_date)=2014
GROUP BY DAY(joining_date);
should work
Ok, i have a database in which i have all my users registered with their datetime which stores in my MYSQL datetime field as 2012-03-31 12:13:42 now i want to fetch COUNT of users according to year and month.
As i want to present a chart data for that purpose, i want total customers added in January, in feb, march and so on.... in particular year.
i.e it has to be year specific that in 2010 Jan lets say 52 Customers in 2010 Feb lets say 72 Customers.
Note: The query i want to execute is using PHP and Mysql.
You can use the MySQL date functions
SELECT YEAR(date_field) as stat_year,
MONTH(date_field) as stat_month,
COUNT(*) as stat_count
FROM `table`
GROUP BY stat_year,stat_month
If you only want the results for a specific year/month, you can add conditions to the WHERE clause (and remove the GROUP BY) like this
SELECT COUNT(*) as stat_count
FROM `table`
WHERE YEAR(date_field) = 2012
AND MONTH(date_field) = 3
There are different SQL that will do the trick, here is one ...
SELECT COUNT(dt), DATE_FORMAT(dt, '%Y-%b') AS df FROM foo GROUP BY df;
where foo is your table name, and dt is the datetime column. Note that this will apply the function DATE_FORMAT on each row. Also, you can change the format as desired, e.g. use %m for numeric month, %M for full month name, etc. Add in where clause to filter as needed. The following SQL is almost the same ...
SELECT COUNT(dt), YEAR(dt) AS y, MONTH(dt) AS m FROM foo GROUP BY y, m;
I have 50 rows/entrys in my table Orders. I have a column, that holds when the order is claimed at, named claimed_at.
The date in this field are in this format: 2011-10-03 07:07:33
This is in the format (yy/mm/dd time).
I also have a column called price, this price is how much they paid.
I would like to display totals per day.
So for 6 orders from the date 2011-10-03, it should take the 6 order's price value, and plus them together.
So I can display:
2011-10-03 -- Total: 29292 Euros
2011-10-02 -- Total: 222 Euros
2011-09-28 -- Total: 4437 Euros
How can i do this?
You need to use aggregate functionality of MySQL in conjuction with some DATE conversion.
SELECT DATE(claimed_at) AS day
, SUM(price) AS total
FROM Orders
GROUP BY day
Create a new field say day with DATE_FORMAT(claimed_at,'%Y-%m-%d') AS day , sum the price and group by day so you will get the result you want.
Something like
SELECT DATE_FORMAT(claimed_at,'%Y-%m-%d') AS day,SUM(price) as total FROM orders GROUP BY day ORDER BY day DESC
Use grouping like this :
SELECT claimed_at, SUM(price) AS total
FROM Orders
GROUP BY claimed_at
You need to use function to get only date part(not time) in grouping. I dont know which function is used in mysql, in MSSQL it's CONVERT() function.
SELECT TRUNC(claimed_at), SUM(PRICE)
FROM my_orders_table
GROUP BY TRUNC(claimed_at)
Im am selecting various things from a table. The problem is I only want to select them if they belong to the current year and the next year.
My table looks like this
Title Text Date
The date is formated like this 0000-00-00 and is in the format "date"
So the question is how can i select only items from only this year and the next?
Example: the year is 2012, I have items in my table that is old and i dont want them to show - I only want to select items from at the first 2012 1 January and last in this case 31 Dec 2013 current year 2012 + 1 year.
Thanks a lot for any help!
SELECT
*
FROM
table
WHERE
YEAR(date) = YEAR(CURDATE())
OR
YEAR(date) = YEAR(CURDATE()) + 1
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
SELECT
*
FROM
table
WHERE
date BETWEEN CONCAT(YEAR(CURDATE()),'-01-01') AND CONCAT(YEAR(CURDATE())+1,'-12-31')
As ugly as it looks, it allows your query to use index on date field.
Better idea is to create limiting dates in external PHP script, so that the query can use query cache.
If you only want to show items, that are no older than two years, you can do this:
SELECT
*
FROM
table
WHERE
date >= CURDATE() - INTERVAL 2 YEAR;