Order By before a Group By [duplicate] - mysql

This question already has answers here:
GROUP_CONCAT ORDER BY
(5 answers)
Closed 8 years ago.
I need to pull some data on a couple thousand records, but I'm having trouble sorting the results.
SELECT IdCode, GROUP_CONCAT(' ',CONCAT(MONTHNAME(ei.StartDatetime),' ',YEAR(ei.StartDatetime)))
FROM exclusion_import_data eid JOIN exclusion_import ei ON ei.ImportId = eid.ImportId
WHERE ( IdCode IN ( '84277', '253533' ))
GROUP BY IdCode
ORDER BY RecordId ASC;
Basically I'm trying to see which months specific records appeared in our system.
253533 May 2013, November 2013, December 2013, January 2014, February
2014, March 2014, April 2014, May 2014, June 2014, August 2014,
October 2013, September 2013, June 2013, July 2013, May 2013, August
2013 84277 September 2013, April 2014, May 2013, May 2014, June 2014,
May 2013, March 2014, June 2013, February 2014, January 2014, July
2013, December 2013, November 2013, August 2013, October 2013, August
2014
The above query returns the correct months, separated by the idCodes, but the months are all out of order.
How can I sort the records before grouping them?

Use the order by option in the group_concat():
SELECT IdCode,
GROUP_CONCAT(' ', CONCAT(MONTHNAME(ei.StartDatetime),' ',YEAR(ei.StartDatetime))
ORDER BY ei.StartDatetime)
FROM exclusion_import_data eid JOIN
exclusion_import ei
ON ei.ImportId = eid.ImportId
WHERE ( IdCode IN ( '84277', '253533' ))
GROUP BY IdCode
ORDER BY RecordId ASC;

You can specify an ordering within the GROUP_CONCAT function, for example:
GROUP_CONCAT(DATE_FORMAT(ei.StartDatetime,'%M %Y') ORDER BY ei.StartDatetime)
^^^^^^^^^^^^^^^^^^^^^^^^^
(My preference would be to get the month and year with a single function call, and one reference to the column, rather than three function calls and two references to the same column. I've demonstrated that above as well.)
Ref: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

Related

filter multiple rows in sql

I have table like
Company Year
--------------
AxisBank 2018
AxisBank 2019
AxisBank 2020
ICICIBank 2018
ICICIBank 2019
ICICIBank 2020
Now I want to filter only Axis Bank and year less than 2020. So that output should be like below
Company Year
--------------
AxisBank 2020
ICICIBank 2018
ICICIBank 2019
ICICIBank 2020
Can someone please help me with Query
You can use:
where company <> 'AxisBank' or year = 2020
You can also express this as:
where not (company = 'AxisBank' and year < 2020)
You can use full Query like below:
SELECT * FROM tableName
WHERE Company != 'AxisBank' OR (Company = 'AxisBank' AND Year >= 2020)
So it will filter out, Company AxisBank and Year is less than 2020.

MySQL query. Creating a table displaying information by month and week

please tell me how the query should be compiled for the table with the following lines: January 2019, first week of January 2019, second week of January 2019 ... fifth week of January 2019, February 2019, first week of February, 2019 second week of February 2019 .... Fifth week of December 2019. The amount of goods sold will be displayed in columns. That is, in the line of the month - the number of sales for the whole month, in the line of the week - the number of sold only for this week.

How can I sort date perfectly

I was trying to sort the data in-order by date but it doesn't sort perfectly. I don't know if there's an error in the code.
Here's some of the data in character in my database:
March 5, 2016
March 17, 2016
August 9, 2017
April 2, 2016
July 5,2018
January 15, 2019
The query I used is:
SELECT * FROM reporting ORDER BY date ASC
Result are:
April 2, 2016
August 9, 2017
January 15, 2019
July 5,2018
March 5, 2016
March 17, 2016
Expected Result:
March 5, 2016
March 17, 2016
April 2, 2016
August 9, 2017
July 5,2018
January 15, 2019
use str_to_date() to convert your date field from string to datetime
SELECT * FROM reporting ORDER BY str_to_date(`date`,'%M %d, %Y') ASC
use date format
ORDER BY DATE_FORMAT(date, "%F-%d-%Y") as date ASC
You may try this,
For SQL Server:
convert( varchar, [date], 120) will convert your date in yyyy-MM-dd format.
select * from reporting order by convert( varchar, [date], 120) asc
For MySQL:
select * from reporting order by DATE_FORMAT(date, "%Y-%m-%e") asc

How to sort month in calendar order using mysql query

I have a query like this
SELECT Value,month,Year FROM `rainfall_past` WHERE Year = '2006' and District= 'Colombo'
this will return the following results
Value month Year
103.4 April 2006
270.6 August 2006
51.9 December 2006
156.9 February 2006
126.9 January 2006
96.8 July 2006
183.1 June 2006
266.6 March 2006
193.1 May 2006
524.7 November 2006
619.9 October 2006
129 September 2006
the problem is that as u can see the months are not in the calendar order. how can query this in calendar months order. i don't want to add additional column to the table or write a script in my source code to sort this. is there any way i can do this in my query only. thanks guys for ur support
It seems like month is a text field. If this is the case, then you can use FIELD function in the ORDER BY clause:
SELECT Value, month, Year
FROM `rainfall_past`
WHERE Year = '2006' and District= 'Colombo'
ORDER BY Year,
FIELD(month, 'January', 'February', ..., 'December')
If, on the other hand, month is deduced from a datetime field, you can use MONTH(date).

MySQL Show Events in Logical Order

looking to organize the following dates (assuming today is August 24, 2012):
December 1, 2012
November 1, 2012
June 1, 2012
June 30, 2012
In the following way:
November 1, 2012
December 1, 2012
June 30, 2012
June 1, 2012
This is such a way that it shows the events that have NOT yet happened first, and from soonest to furthest away, then show past events, from closest to farthest.
You can assume the table structure is:
ID name event_date
1 Test 1351742400 # All dates are Unix Time
2 Test2 1354338000
SELECT ID, name, event_date
FROM yourtable
ORDER BY event_date < UNIX_TIMESTAMP(), ABS(UNIX_TIMESTAMP() - event_date)
See it working online: sqlfiddle