MySQL get start and end date of a quarter by quarter number - mysql

$sq ="
SELECT count(noofrows),QUARTER(Date(last_call)) as quarter,YEAR(last_call)
FROM $dbt.travel_crm
WHERE Date(last_call) BETWEEN '".$_GET['startdate']."'
AND '".$_GET['enddate']."'
GROUP BY QUARTER(Date(last_call))
ORDER BY YEAR(last_call)";
Given only current years rows count, because grouping by quarter, I need to group by (quarter,year), how can I do that?

If you need to group by quarter and year - just do so:
GROUP BY YEAR(last_call), QUARTER(last_call)

Related

sql counting per year and month, show months even when zero

So I'm counting articles per year/month between the start of the year and the current time:
SELECT Year(FROM_UNIXTIME(date)) as year
, Month(FROM_UNIXTIME(date)) as month
, Count(*) as `total`
FROM articles
WHERE date BETWEEN UNIX_TIMESTAMP(DATE('2017-01-01 00:00:00')) AND UNIX_TIMESTAMP(DATE('2017-05-17 12:00:05'))
GROUP
BY Year(FROM_UNIXTIME(date))
, Month(FROM_UNIXTIME(date))
The only issue, is that months that have zero, won't show up.
Is there an easy way around it?
The best solution I can think of is to do an inner join with a lookup table that has months 1-12 in them. Thus verifying there will always be a 12 month result set?
Possibly including a restriction for the current date month can not be surpassed, so you don't actually always get for the whole year.
look here:
Include missing months in Group By query

mySQL query between two dates issue with sum

I have the next table named expenses_partial_payment.
My query is the next, i need the sum of amount_paid as total_paid:
SELECT id_partial_payment, epp.id_billing, epp.id_user, epp.month_of_payment,
COALESCE(SUM(epp.amount_paid),0) AS total_paid,
(be.total - amount_paid) AS pending_total , be.total as total
FROM expenses_partial_payment epp
INNER JOIN BillingExpenses be ON epp.id_billing = be.id_billing
WHERE epp.id_user = 23
but when i add in the clause where
WHERE epp.id_user = 23 AND (Month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01'))
I have an error with the total_piad, the query returns only the last row (300) and don't sum the amount_paid , the correct sum must be 501.00
What can i do to get the correct sum adding the clause where to filter the month_of_payment
(month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01'))
month_of_payment BETWEEN '2015-01-01' AND LAST_DAY('2016-01-01')
last day() returns the last day of the Month not year. So it will not pull up data in the month greater than January.
Try to replace your date with this
STR_TO_DATE('01/01/2015', '%m/%d/%Y')

MySql - order by monthname

I am trying to order mysql query by month name like:
January -- 5
February -- 2
March -- 5
and so on
Here is my query, but its not ordering:
SELECT leave_balance.balance, MonthName(leave_balance.date_added) AS month
FROM leave_balance WHERE leave_balance.staff_id_staff = $iid
GROUP BY month, leave_balance.leave_type_id_leave_type
HAVING leave_balance.leave_type_id_leave_type = $leaveBalTypID
ORDER BY month
Kindly tell me where I am doing wrong
you can specify like
ORDER BY FIELD(MONTH,'January','February','March',...)
SELECT leave_balance.balance, MonthName(leave_balance.date_added) AS month
FROM leave_balance WHERE leave_balance.staff_id_staff = $iid
GROUP BY month, leave_balance.leave_type_id_leave_type
HAVING leave_balance.leave_type_id_leave_type = $leaveBalTypID
ORDER BY FIELD(MONTH,'January','February','March',...,'December');
Just add this at the end of your query statement:
ORDER BY str_to_date(MONTH,'%M')
If column name changes in the future you will not need to worry about it.
try using the built in month function to get the month's number and order by that. For example:
order by month(leave_balance.date_added)
simply order by leave_balance.date_added field. By default it will sort by month.

MySQL, How can I count the amount something happens in a month, and display the sum beside month names?

I need to display 2 columns. First column should have the month name. Second column should show me how many times something was released within each month. e.g:
Month number_of_releases
January 4
March 9
December 2
So far, I have this:
SELECT DISTINCT MONTHNAME(date) AS 'Month',
/*Here is where I need help!*/ AS 'number_of_releases'
FROM table_name;
Without knowing how you are calculating number_of_releases, it's hard to say for certain... but you probably want to group your table by monthname and use a suitable aggregate function to yield the number of releases.
For example, to obtain a count of the number of records within each month:
SELECT MONTHNAME(date) AS Month, COUNT(*) AS number_of_releases
FROM table_name
GROUP BY Month

Group results by period

I have some data which I want to retrieve, but I want to have it grouped by a specific number of seconds. For example if my table looks like this:
| id | user | pass | created |
The created column is INT and holds a timestamp (number of seconds from 1970).
I would want the number of users that are created between last month and the current date, but show them grouped by let's say 7*24*3600 (a week). So if in the range there are 1000 new users, have them show up how many registered each week (100 the first week, 450 the second, 50 the third and 400 the 4th week -- something like this).
I've tried grouping the results by created / 7*24*3600, but that's not working.
How should my query look like?
You need to use integer division div otherwise the result will turn into a real and none of the weeks will resolve to the same value.
SELECT
(created div (7*24*60*60)) as weeknumber
, count(*) as NewUserCount
FROM users
WHERE weeknumber > 1
GROUP BY weeknumber
See: http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html
You've got to keep the integer part only of that division. You can do it with the floor() function.
Have you tried select floor(created/604800) as week_no, count(*) from users group by floor(created/604800) ?
I assume you've got the "select users created in the last month" part sorted out.
Okay here are the possible options you may try:
GROUP BY DAY
select count(*), DATE_FORMAT(created_at,"%Y-%m-%d") as created_day FROM widgets GROUP BY created_day
GROUP BY MONTH
select count(*), DATE_FORMAT(created_at,"%Y-%m") as created_month FROM widgets GROUP BY created_month
GROUP BY YEAR
select count(*), DATE_FORMAT(created_at,"%Y") as created_year FROM widgets GROUP BY created_year