sql-order-by

SQL how to make null values come last when sorting ascending

北城以北 提交于 2019-11-26 03:47:21
问题 I have a SQL table with a datetime field. The field in question can be null. I have a query and I want the results sorted ascendingly by the datetime field, however I want rows where the datetime field is null at the end of the list, not at the beginning. Is there a simple way to accomplish that? 回答1: select MyDate from MyTable order by case when MyDate is null then 1 else 0 end, MyDate 回答2: (A "bit" late, but this hasn't been mentioned at all) You didn't specify your DBMS. In standard SQL

Use own IComparer<T> with Linq OrderBy

佐手、 提交于 2019-11-26 03:28:41
问题 I have a generic List<MyClass> where MyClass has a property InvoiceNumber which contains values such as: 200906/1 200906/2 .. 200906/10 200906/11 200906/12 My list is bound to a BindingList<T> which supports sorting with linq: protected override void ApplySortCore( PropertyDescriptor property, ListSortDirection direction) { _sortProperty = property; _sortDirection = direction; var items = this.Items; switch (direction) { case ListSortDirection.Ascending: items = items.OrderByDescending(x =>

Using union and order by clause in mysql

回眸只為那壹抹淺笑 提交于 2019-11-26 03:28:13
问题 I want to use order by with union in mysql query. I am fetching different types of record based on different criteria from a table based on distance for a search on my site. The first select query returns data related to the exact place search . The 2nd select query returns data related to distance within 5 kms from the place searched. The 3rd select query returns data related to distance within 5-15 kms from the place searched. Then i m using union to merge all results and show on a page

PostgreSQL DISTINCT ON with different ORDER BY

大城市里の小女人 提交于 2019-11-26 03:28:09
I want to run this query: SELECT DISTINCT ON (address_id) purchases.address_id, purchases.* FROM purchases WHERE purchases.product_id = 1 ORDER BY purchases.purchased_at DESC But I get this error: PG::Error: ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions Adding address_id as first ORDER BY expression silences the error, but I really don't want to add sorting over address_id . Is it possible to do without ordering by address_id ? Documentation says: DISTINCT ON ( expression [, ...] ) keeps only the first row of each set of rows where the given expressions evaluate

What is MySQL row order for “SELECT * FROM table_name;”?

余生长醉 提交于 2019-11-26 02:57:44
问题 Assume that the following query is issued to a MySQL database: SELECT * FROM table_name; Note that no ORDER BY clause is given. My question is: Does MySQL give any guarantees to which order the result set rows will be given? More specifically, can I assume that the rows will be returned in insertion order?, that is the same order in which the rows were inserted into the table. 回答1: No, there are no guarantees. Unless you specify an order using an ORDER BY clause, the order is totally

C# Sort and OrderBy comparison

折月煮酒 提交于 2019-11-26 02:55:20
问题 I can sort a list using Sort or OrderBy. Which one is faster? Are both working on same algorithm? List<Person> persons = new List<Person>(); persons.Add(new Person(\"P005\", \"Janson\")); persons.Add(new Person(\"P002\", \"Aravind\")); persons.Add(new Person(\"P007\", \"Kazhal\")); 1. persons.Sort((p1,p2)=>string.Compare(p1.Name,p2.Name,true)); 2. var query = persons.OrderBy(n => n.Name, new NameComparer()); class NameComparer : IComparer<string> { public int Compare(string x,string y) {

The order of a SQL Select statement without Order By clause

给你一囗甜甜゛ 提交于 2019-11-26 02:35:26
问题 As I know, from the relational database theory, a select statement without an order by clause should be considered has not particular order. But actually in SQL Server and Oracle (I\'ve tested on those 2 platforms), if I query from a table without an order by clause multiple times, I always get the results in the same order. Does this behavior can be relied on? Anyone can help to explain a little? 回答1: No, that behavior cannot be relied on. The order is determined by the way the query planner

SQL Query - Using Order By in UNION

筅森魡賤 提交于 2019-11-26 02:19:36
问题 How can one programmatically sort a union query when pulling data from two tables? For example, SELECT table1.field1 FROM table1 ORDER BY table1.field1 UNION SELECT table2.field1 FROM table2 ORDER BY table2.field1 Throws an exception Note: this is being attempted on MS Access Jet database engine 回答1: Sometimes you need to have the ORDER BY in each of the sections that need to be combined with UNION . In this case SELECT * FROM ( SELECT table1.field1 FROM table1 ORDER BY table1.field1 ) DUMMY

MySQL “Group By” and “Order By”

随声附和 提交于 2019-11-26 02:17:51
问题 I want to be able to select a bunch of rows from a table of e-mails and group them by the from sender. My query looks like this: SELECT `timestamp`, `fromEmail`, `subject` FROM `incomingEmails` GROUP BY LOWER(`fromEmail`) ORDER BY `timestamp` DESC The query almost works as I want it — it selects records grouped by e-mail. The problem is that the subject and timestamp don\'t correspond to the most recent record for a particular e-mail address. For example, it might return: fromEmail: john

Default row ordering for select query in oracle

怎甘沉沦 提交于 2019-11-26 01:50:37
问题 In Oracle, what is the the default ordering of rows for a select query if no \"order by\" clause is specified. Is it the order in which the rows were inserted there is no default ordering at all none of the above. 回答1: According to Tom Kyte: "Unless and until you add "order by" to a query, you cannot say ANYTHING about the order of the rows returned. Well, short of 'you cannot rely on the order of the rows being returned'." See this question at asktom.com. As for ROWNUM, it doesn't physically