简体   繁体   English

SQL:按日期排序,仅给出月份和年份-完整组

[英]SQL: Order by date, only month and year given - Full Group on

I have an SQL database which is setted to full group on mode. 我有一个SQL数据库,它设置为完全组模式。 My goal is to get the amount of rows (ID's) for every month. 我的目标是获取每个月的行数(ID)。 This is why I say Group By Datum . 这就是为什么我说“ Group By Datum Because of the full group mode I cannot simply say Order By created_at . 由于具有完整的组模式,因此我不能简单地说Order By created_at Because I have selected only %m.%Y . 因为我只选择了%m.%Y So I can only work with Datum which is cointaing my month and year. 所以我只能使用Datum来计算我的月份和年份。

I already tried to connect those values like CONCAT('DATE_FORMAT(created_at, '%Y-%m'), "-01 00:00:00") but also this isn't working... Even if I turn it into a UNIX Timestamp it isn't working: UNIX_TIMESTAMP(CONCAT('DATE_FORMAT(created_at, '%Y-%m'), "-01 00:00:00")) 我已经尝试连接那些值,例如CONCAT('DATE_FORMAT(created_at, '%Y-%m'), "-01 00:00:00")但这也无法正常工作,即使我将其变成UNIX时间戳不起作用: UNIX_TIMESTAMP(CONCAT('DATE_FORMAT(created_at, '%Y-%m'), "-01 00:00:00"))

I even tried this one: Order By Year(DATE_FORMAT(created_at, '%Y')), Month DATE_FORMAT(created_at, '%m') But it isn't also working... 我什至尝试了以下方法: Order By Year(DATE_FORMAT(created_at, '%Y')), Month DATE_FORMAT(created_at, '%m')但它也不起作用...

How can I sort my result by month and year without changing the Select values? 如何在不更改Select值的情况下按月和年对结果进行排序? Sofar this is my actual SQL Query. Sofar这是我实际的SQL查询。 Not working either.. I am nearly trying to find a solution since 1 hour... 自1个小时以来,我几乎都在尝试寻找解决方案...

SELECT COUNT(ID) AS Anzahl, DATE_FORMAT(created_at, '%m.%Y') AS Datum FROM leads WHERE created_at >= '2015-01-01' AND created_at <= '2018-01-01' AND shopID = 4184 GROUP BY Datum ORDER BY UNIX_TIMESTAMP(STR_TO_DATE(Datum, '%Y-%m-01 00:00:00'))

I would appreciate any kind of help! 我将不胜感激! And no, I cannot change the Select values or turn off the full_group_mode . 不,我不能更改Select值或关闭full_group_mode

ORDER BY DATE_FORMAT(created_at, '%Y'), date_format(created_at, '%M') desc ORDER BY DATE_FORMAT(created_at,'%Y'),date_format(created_at,'%M')desc

This will sort the results by year ascending then by month descending. 这将按升序排列,然后按降序排列。

Take a look at this SQL Fiddle to see the query using MySql 5.6 看看这个SQL Fiddle ,看看使用MySql 5.6的查询

The following will sort by year and then month in ascending order: 以下内容将按照年份然后是月份的升序排列:

order by substring(datum,4,4), substring(datum,1,2)

sqlfiddle: http://sqlfiddle.com/#!9/16fab4/3 sqlfiddle: http ://sqlfiddle.com/#!9/16fab4/3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM