I want to get the last 10 unique months where there have been events, stored as event_date as a mysql date. I am using coldfusion so could group the result but can't get that working as I would like.
This is what I have so far.
SELECT MONTH(event_date) AS month, YEAR(event_date) AS year, event_date from events
so ideally if there were no posts in August would be output as
september 2010
july 2010
june 2010
may
etc etc
Thanks.
R.
SELECT DISTINCT MONTH(event_date) AS month, YEAR(event_date) AS year from events ORDER BY year DESC, month DESC LIMIT 10;
Related
how can I count last 7 days data from SQL and group them by day/date (excluding today).
I should be able to use the result as $resultday1, $resultday2, $resultday3 etc.
If there was 10 total SQL entries in day 1 (yesterday) $resultday1 should show "10".
and the days should be last 7 only, and today/current day should not consider.
The following PHP SQL script shows the total count only
SELECT COUNT(1) FROM orders WHERE username='jondoe'
database is a list of referrals made by a registered user in previous days.
a single table contains all user's referral details, table name "orders" as per above example.
This is the exact query as you want
SELECT
COUNT(*), DATE(order_date) order_date
FROM
orders
WHERE
order_date < CURDATE()
AND order_date > DATE_SUB(NOW(), INTERVAL 7 DAY)
GROUP BY order_date
ORDER BY order_date DESC;
I'm working to write a MySQL query that outputs the number of new users created by week.
My user table is:
id | created_at
My query:
SELECT YEAR(created_at) AS Year, DATE_FORMAT(created_at, '%b %e') AS Week, COUNT(*) AS total
FROM users
GROUP BY Year, Week;
The problem:
Years: I would like the most recent year to be at the top, currently the oldest year is at the top of the output.
Weeks: The week column is not sorted based on the calendar. For example, the last records shows: 2019 | May 9 | 100
Where I'd like the year and week sorted.
I think you'll find the function YEARWEEK() helpful, not only for the grouping, but also for the ordering.
Also, your use of DATE_FORMAT() doesn't look right, because you're outputing the %e, which is the day of the month, yet you're grouping by week?
SELECT DATE_FORMAT(created_at, '%Y %b %v') AS date, COUNT(*) AS total
FROM users
GROUP BY YEARWEEK(created_at)
ORDER BY YEARWEEK(created_at) DESC;
so I have a table "records" like this:
name month year
Rafael 9 2018
Rafael 5 2018
Rafael 10 2017
And I want to get my last records (Rafael, 9, 2018). I thought about summing the month and the year and getting the max of that sum like this:
select * from records, max(month + year) as max_date
But doesn't seem to be right. Thanks for help
Using ORDER BY clause, you can get the highest year and month combo. Try the following:
SELECT *
FROM records
ORDER BY year DESC, month DESC
LIMIT 1
Do you mean the output of the follwing?
select *
from records
order by year desc, month desc
limit 1
In general, it would be more useful to use one DATE or DATETIME column type for this purpose where you can extract year and month if you want.
Use concat if you want to concat max month and max year
Select name ,concat (concat( max(month), '-'),max(year)) from records
Group by name
but if you want just year wise max year date information then use below
Select * from records
order by year desc
limit 1
https://www.db-fiddle.com/f/sqQz1WEEAukoWEWkbxBYxe/0
name month year
Rafael 9 2018
I have a table with faktura_kroner (decimal 9,2), and faktura_dato (BIGINT 11, which is a unix timestamp).
I am trying to get the sum of faktura_kroner per month so I can use it in a chart, but am struggling a bit to find the correct sql query. Also it is unique by year. So October 2016 should not be grouped with October 2017 as an example.
Any help appreciated..
SELECT sum(faktura_kroner) as sum_faktura,
MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month,
YEAR(FROM_UNIXTIME(faktura_dato)) as year
FROM
faktura
WHERE
user_id = 1
AND
virksomhet_id = 1
GROUP BY YEAR(FROM_UNIXTIME(faktura_dato)) DESC,
MONTH(FROM_UNIXTIME(faktura_dato)) ASC, faktura_dato DESC;
You can try below query -
SELECT sum(faktura_kroner) as sum_faktura
,MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month
,YEAR(FROM_UNIXTIME(faktura_dato)) as year
FROM faktura
WHERE user_id = 1
AND
virksomhet_id = 1
GROUP BY MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month
,YEAR(FROM_UNIXTIME(faktura_dato)) as year
I am working on a PHP application that displays events. It generates a select box that allows visitors to choose a month for which events should be displayed.
My problem is in regard to the select box, which should display not only months on which an event starts or ends, but also months during which an event is still taking place (even if it started months before and ends months later).
Here is what I've got at the moment, it's pretty basic and only returns months on which events start.
SELECT
DISTINCT DATE_FORMAT(start,'%M %Y') as prettydate,
DATE_FORMAT(start,'%m%Y') as monthyear
FROM
`events`
WHERE
start >= NOW()
ORDER BY
start ASC;
How can I also get February & July but also March, April, May & June 2014 in the result if the only event I've got in 2014 starts in February and finishes in July for example?
Thanks in advance for your help!
Assuming you don't have any events that span more than 1 year, and using the following start/end dates:
('2012-01-01','2012-01-31'),
('2012-03-01','2012-03-31'),
('2012-05-01','2012-06-01'),
('2012-07-01','2012-07-31'),
('2012-09-01','2012-10-31'),
('2012-11-01','2012-11-30');
this query:
SELECT DISTINCT
DISTINCT DATE_FORMAT(e.start + INTERVAL ids.id MONTH,'%M %Y') as prettydate,
DATE_FORMAT(e.start + INTERVAL ids.id MONTH,'%m%Y') as monthyear
FROM
(
SELECT 0 AS id
UNION SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
) ids,
events e
WHERE
EXTRACT(YEAR_MONTH FROM e.start + INTERVAL ids.id MONTH) <=
EXTRACT(YEAR_MONTH FROM e.end)
ORDER BY
e.start + INTERVAL ids.id MONTH
will return:
prettydate monthyear
January 2012 012012
March 2012 032012
May 2012 052012
June 2012 062012
July 2012 072012
September 2012 092012
October 2012 102012
November 2012 112012
See http://sqlfiddle.com/#!2/12b08/3 for a working example.
Select start and finish date for each event. Once you have finish date you can calculate which months are in between of event beginning and ending.
Try using http://php.net/manual/en/function.date.php
With that or your own formatting you can check if Starting date month and year are same as finish month and year. If they are not, simply fill out the list with, let's say some predefined array of months.
$months = array(1 => "Jan", 2 => "Feb", ... , 12 => "Dec");
then you can do something like
From there you can calculate the number of months in between and simply iterate through the $months array.