Mysql Ordering by date - mysql

I have a date field in my table called date1. if i use the following query.
select * from schedule order by date1 asc
it gives the result like as jan 2011 comes before december 2010. but i need the december 2011 as the first row of the result.

Change your query to
SELECT * FROM schedule ORDER BY date1 DESC
This should do the trick.
James

select * from schedule order by date1 desc

Related

How to group by date not with time

My query return like below
date count
November 19,2019,11:58 PM 2
November 19,2019,11:59 PM 2
November 20,2019,12:02 AM 2
Is there any way to do like
November 19,2019 4
November 20,2019 2
follow this query:
I get this result when I execute the query
select count(*) from listings group by date(created)
Seems your date is not a standard mysql date time format. We need to format it first using str_to_date function.
select cast(str_to_date(dateC, '%M %d,%Y,%h:%i') as date), count(1) from Test
group by cast(str_to_date(dateC, '%M %d,%Y,%h:%i') as date)
see dbfiddle.

SQL: MAX of summing the values of 2 columns

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

MYSQL - Can't group a summed column

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

Get Weekly SUM from MySQL

I want to get a list of mysql results for each week beginning at July 2015, showing the SUM or new users from my table user GROUPED BY Week. Is this possible?
So as result:
CW25/15: 100
CW26/15: 70
CW27/15: 180
....
How to do?
Try this:
SELECT CONCAT('CW',WEEK(date_col),'/',YEAR(date_col)) as week,
COUNT(*) as count
FROM table_name
GROUP BY YEAR(date_col),WEEK(date_col)
ORDER BY date_col
You can do it like this(You didn't post your table structures so you will have to adjust it) :
SELECT concat('CW',week(DateColumn),'/',year(DateColumn)) as weekDate,
count(*) as cnt
FROM YourTable
GROUP BY concat('CW',week(DateColumn),'/',year(DateColumn))
ORDER BY year(DateColumn),week(DateColumn)

Get Latest Event from mysql

I have events table. I want latest event which event type='appointment' and group on type and instruction_id. Problem is event_date come first '2013-12-02' instead of '2013-12-05'. More information required give comment I explain in detail.
My Expected output :
ID INSTRUCTION_ID TYPE COMMENT EVENT_DATE
3 2 appointment at home December, 05 2013 00:00:00+0000
10 1 appointment at home November, 22 2013 00:00:00+0000
5 3 appointment office September, 17 2013 00:00:00+0000
For more information check SQL fiddle:
try below query:
select *
from
(SELECT *
FROM EVENTS
WHERE EVENTS.type='appointment'
ORDER BY EVENTS.event_date DESC) EVENTS
GROUP BY EVENTS.type,EVENTS.instruction_id;
Query 2:
select *
from
(SELECT *
FROM EVENTS
WHERE EVENTS.type='appointment'
ORDER BY EVENTS.event_date DESC) EVENTS
GROUP BY EVENTS.type;
If you want the latest of only "appointment type" grouping by event type:
SELECT max(EVENT.event_date) -- use other fields
FROM EVENTS
WHERE EVENTS.type='appointment'
GROUP BY EVENTS.instruction_id;
Because if you filter by one specific type you will only get the max date of that type. If you want to get the max date of each type
SELECT max(EVENT.event_date) -- use other fields
FROM EVENTS
GROUP BY EVENTS.type, EVENTS.instruction_id
EDIT: if you add the rest of the fields is working as you expected. Anyway, I paste you the query tested:
SELECT ID, INSTRUCTION_ID, TYPE, COMMENT, max(EVENT_DATE) EVENT_DATE
FROM EVENTS
WHERE EVENTS.type='appointment'
GROUP BY EVENTS.instruction_id
ORDER BY EVENT_DATE;