Sort by year SQL data

Asked

Viewed 197 times

0

I have a table with 2 columns (one with date (YYYY,MM,DD) and another with a numeric value). I want the table to be ordered only per year and in case of entries with the same year the "tie-breaker" factor is the numeric value. How can I do?

  • depends, which database are you using? only use a function that extracts the year of the date, but need to know which database... would be for example order by YEAR(coluna), outra_coluna

  • I have a secure table with apolice columns, date and coverage. I want a list of apolices ordered by year and in case of "tie-breaker" sort by the amount of coverage. I tried the command:

  • select apolice from safe order by YEAR(datai), cover asc; But gives error...

  • as I commented, need to know the database, year is a function to sql-server

  • Place the database you are using, if possible insert the tables used to better understand the question :)

  • I have another question: I need a query to return the mark with the highest number of accidents. I have three tables I need to use: vehicle, accident and acid_veic. Each accident has a number, the n_acid and I need to go to the accident tables and vec_acid see which vehicles involved in each accident. In the accident table the number of the crashed vehicle is in the column "safe vehicle" and in the table acid_veic the number of the crashed vehicle is in the column "vehicle". Then I have to group them by tags and count to see which one repeats more times, so the query return me this mark

Show 1 more comment

1 answer

0

Well, in Mysql I would do so (use as a base for another bank). Your query should return all fields through *, and through the Year function return only the year as a field. Note: Year is a Mysql function.

SELECT *, Year(data) AS ano FROM nome_da_sua_tabela ORDER BY ano;
  • gives me syntax error in "year". I don’t know if it helps to say I’m using PUTTY

  • Which database you are using?

  • You managed to solve?

  • Yes, I did! Thank you for your help

Browser other questions tagged

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