-2

I am facing one issue I cant handle yet.

Here's the deal: I am working on a program which should monitor employees working hours. So far, I created a SQL Server table called TablicaSQL with 4 columns:

Id, Ime (Name), Datum (date), BrojSati (WorkingHours)

It saves data according to the time of saving.

Example: if I enter Kristijan (name) worked on 2017-11-03 4 hours, but tomorrow if I save that Kristijan worked on 2017-11-01 4 hours, it will show which data has been saved first, which in this case is 2017-11-03.

So my question is: How can I sort my data according the column Datum (date), NOT by the date of saving the data.

Also, I am not looking for query which says something like this:

SELECT * 
FROM..
ORDER BY...ASC/DESC

I need some kind of "permanetly asc/desc query".

Here is the screenshot of my table

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Post the tables and data as text [READ THIS](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) – Juan Carlos Oropeza Nov 29 '17 at 13:30
  • You can put an index on your table which has Datum as your clusterd index with sort order asc or desc. Then it will permantly sorted this way. – SqlKindaGuy Nov 29 '17 at 13:35
  • 2
    @plaidDK Data order isnt guaranted. Check here https://stackoverflow.com/questions/28126252/do-clustered-index-on-a-column-guarantees-returning-sorted-rows-according-to-tha – Juan Carlos Oropeza Nov 29 '17 at 13:37
  • @JuanCarlosOropeza Thanks for clarifying – SqlKindaGuy Nov 29 '17 at 13:43
  • Hello Kristijan, You can create a SQL view as follows which has an additional column with ROW_NUMBER() which can provide a sorting (but that is not guaranteed) Please test and check with huge data to use for productive requirements create view MyDatesV as select *, row_number() over (order by InsertDate) as SortNum from MyDates – Eralper Nov 29 '17 at 13:44
  • @Eralper View doesnt have order neither. if you query the View you also need add `ORDER BY` – Juan Carlos Oropeza Nov 29 '17 at 13:45
  • The Row_Number() provides a virtual(!) sorting of course not guarenteed as ORDER BY clause – Eralper Nov 30 '17 at 08:18

1 Answers1

4

There isn't a permanent order on database table. They are unorder data set. The data isn't order by the data of creation. Is just returned in the order is storage. But that can change if db engine optimizer find a better way to read the data. Multiple Partition, Clusters, etc.

If you want the data return in a specific order YOU MUST include ORDER BY

Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118