I NEED HELP ORDERING A QUERY

Asked

Viewed 60 times

-5

I’m doing a query where I’m returning the last record of the day(which in case has the days but there are several time records in a day, and I’m picking the last of them in a sub select), the problem is that I can’t sort in the end, it gets shuffled, I’ve tried order by and it doesn’t work. Follow the query:

NOTE: I want to sort the column 'Createdon'

    SELECT
  rl.WipOrderNo,
  CAST(rl.CreatedOn AS smalldatetime) AS CreatedOn,
  rl.RegularHours
FROM resource_labor rl

WHERE CAST(rl.CreatedOn AS date) = CAST(rl.CreatedOn AS date)
AND rl.WipOrderNo = rl.WipOrderNo

GROUP BY CAST(rl.CreatedOn AS smalldatetime),
         rl.WipOrderNo,
         rl.RegularHours

Resultado da QUERY

  • you want to order by what?

  • sort the Createdon column in ascending

  • what kind of field?

  • datetime, is in sql server

1 answer

0


I believe you can do something like:

  GROUP BY  CONVERT(date, CreatedOn) , 
           rl.WipOrderNo,
           rl.RegularHours

   ORDER BY  CONVERT(date, CreatedOn) desc
  • gave the following error: Message 8120, Level 16, State 1, Line 19 Column 'resource_labor.Createdon' is invalid in the select list because it is not contained in either an Aggregate Function or the GROUP BY clause. Error on query 3rd line

  • It is possible that when you group by date, for the same date there are different Wiporderno, for example. That way, select doesn’t understand which one to show.

  • look at this link https://stackoverflow.com/questions/13999817/reason-for-column-is-invalid-in-the-select-list-because-it-is-not-contained-in-e The first reply explains this kind of error right.

  • I managed to solve the problem! Thank you very much, helped a lot with that group by and order by, and with the post indicated! Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.