Entity Framework Include OrderBy random generates duplicate data

As one can sort it out by reading AakashM answer and Nicolae Dascalu answer, it strongly seems Linq OrderBy requires a stable ranking function, which NewID/Guid.NewGuid is not. So we have to use another random generator that would be stable inside a single query. To achieve this, before each querying, use a .Net Random generator … Read more

Returning query results in predefined order

I didn’t think this was possible, but found a blog entry here that seems to do the type of thing you’re after: SELECT id FROM table WHERE id in (7,2,5,9,8) ORDER BY FIND_IN_SET(id,”7,2,5,9,8″); will give different results to SELECT id FROM table WHERE id in (7,2,5,9,8) ORDER BY FIND_IN_SET(id,”8,2,5,9,7″); FIND_IN_SET returns the position of id … Read more

SQL Order By Count

You need to aggregate the data first, this can be done using the GROUP BY clause: SELECT Group, COUNT(*) FROM table GROUP BY Group ORDER BY COUNT(*) DESC The DESC keyword allows you to show the highest count first, ORDER BY by default orders in ascending order which would show the lowest count first.

PostgreSQL ORDER BY issue – natural sort

Since Postgres 9.6, it is possible to specify a collation which will sort columns with numbers naturally. https://www.postgresql.org/docs/10/collation.html — First create a collation with numeric sorting CREATE COLLATION numeric (provider = icu, locale=”en@colNumeric=yes”); — Alter table to use the collation ALTER TABLE “employees” ALTER COLUMN “em_code” type TEXT COLLATE numeric; Now just query as you … Read more