Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to use ORDER BY column not in the GROUP BY?

like the title said, here is my code:

SELECT
  material,
  SUM([Amount]) AS [Amount],
  RIGHT(CONVERT(varchar(50), [date_in], 106), 8)
FROM
  [rec_stats]
GROUP BY
  material,
  RIGHT(CONVERT(varchar(50), [date_in], 106), 8)
ORDER BY
  material,date_in

the code wont run because of the date_in, is there anyway to get around this?

like image 495
dreamer Avatar asked Apr 28 '11 11:04

dreamer


People also ask

Can I use ORDER BY without GROUP BY?

Key Differences between GROUP BY and ORDER BY The Group By clause is used to group data based on the same value in a specific column. The ORDER BY clause, on the other hand, sorts the result and shows it in ascending or descending order. It is mandatory to use the aggregate function to use the Group By.

Can we select column which is not part of GROUP BY?

The direct answer is that you can't. You must select either an aggregate or something that you are grouping by.

Can I use column in ORDER BY without specifying in select?

Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table. For a group by clause though you'd need it to be in your select statement. is it possible to give an SELECT statement ...

Does the order of columns matter in a GROUP BY clause?

No, the order doesn't matter for the GROUP BY clause.


1 Answers

Apply another aggregate, so how about;

order by min([date_in])
like image 59
Alex K. Avatar answered Sep 25 '22 10:09

Alex K.