order-by

How do I order by multiple columns in a SELECT query?

I have a table of records like below int_record_id int_category_id str_name int_order bit_active 1 1 test1 2 1 2 1 test2 1 1 3 2 test3 1 1 1 3 te...

Is order by clause allowed in a subquery

Is there any reason why or why not you should do an 'order by' in a subquery? ...

How to specify an Order or Sort using the C# driver for MongoDB?

I'm trying to figure out how to sort a collection of documents server side by telling the C# driver what the sort order is, but it appears not to support that construct yet. Is it possible to do this any other way? ...

Order By on a modified Entity Framework object

I'm getting an Entity Framework object collection from my Products table called oProducts. I am looping through the collection and setting the Quantity field from a separate function. Before I write out a display of the objects in HTML, I want to sort them by the Quantity. So, I am using LINQ to create a new collection and try to orde...

Two level group by, order by

what is the linq equivalent of the following: SELECT Mytable.A, MyTable.B, SUM(MyTable.C) FROM MyTable GROUP BY Mytable.A, MyTable.B ORDER BY Mytable.A, MyTable.B This is trivial in SQL, but seems to be very difficult in LINQ. What am I missing? ...

Best practice question for MySQL: order by id or date?

This is kind of a noobish question, but it's one that I've never been given a straight answer on. Suppose I have a DB table with the following fields and values: | id | date_added | balance | +------------------------------------+ | 1 | 2009-12-01 19:43:22 | 1237.50 | | 2 | 2010-01-12 03:19:54 | 473.00 | | 3 | 2010-01-12 0...

MySql Query: ordering by two fields in two tables

Hi everybody, I have two tables storing members data members: id, field1, field2 and field3... members_extra: memberId, someExtraField1 and someExtraField2 members_extra.memberId is a Foreign Key referencing members.id 'members_extra' may or may not have related rows for rows in 'members' let's say: I have 1000 members in 'members...

Django order_by a related _set

I have a model for our customers and a model for their purchases/orders. In our backend/web admin, we want to be able to sort the customer list by their most recent order. Here is basically what our models look like class Customer(models.Model): username = models.CharField(max_length=32) first_name = models.CharField(max_length...

performing a SELECT TOP query with an ORDER BY - should I use an index or a 'sorted view' to increase performance

This query (part of a sproc) will be executed quite a lot: SELECT TOP (@numRecords) BlogPost.postId,BlogPost.creationDate, BlogPost.header,BlogPost.markedupContentAbstract FROM dbo.BlogPost ORDER BY BlogPost.creationDate DESC Should I put an index on the 'creationDate' field in the BlogPost table? Should I have a view wh...

mysql optimization - display 10 most recent records, but also identify duplicate rows

I am new to mysql and I have been pulling my hair out about this problem for days. I need to improve/optimize this query so that it runs faster - right now its taking over 5 seconds. Here is the query: SELECT SQL_NO_CACHE COUNT(*) as multiple, a.*,b.* FROM announcements as a INNER JOIN stores as s ON a.username=s.username ...

Order resultset based on WHERE IN clause data

Considering this MySQL query: SELECT someColumns FROM someTable WHERE someColumn IN ( value1, value2, value3 ) ... how can I guarantee that the rowset comes out ordered in the exact order of the values given to the IN () clause? I presume this is not guaranteed without giving it an ORDER BY clause, is it? PS.: The values to the...

How to find the first item according to a specific ordering using LINQ in O(n)?

Suppose I have a list of items (e.g., Posts) and I want to find the first item according to some non-trivial ordering (e.g., PublishDate and then CommentsCount as a tie-breaker). The natural way to do this with LINQ is like this: posts.OrderBy(post => post.PublishDate).ThenBy(post => post.CommentsCount).First() However, the micro-opti...

How to manipulate an array similar to an ORDER BY clause in SQL?

My data has been placed into an array, but unfortunately not in the order I want it in... String[][] databaseToArray = { //{"Name", "Channel", "Description", "Amount", "isReady"}, {"John", "Nick", "likes", "2", "yes" }, {"Drew", "MTV", "dislikes", "4", "no" }, {"Fred", "CNN", "okay", ...

mysql order by count

I have a table with store IDs and the city where the store is located. I want to list all the stores starting with the stores that are in the city where there is the most stores. TABLE ID CITY 1 NYC 2 BOS 3 BOS 4 NYC 5 NYC The output I want is the following since I have the most stores in NYC, I want all the N...

Linq OrderBy breaks with navigation property being null.

Working with four tables. Users -> has basic user info including a userid and a departmentid (int) Groups -> basic group info including a groupid GroupsMembers -> table that has the relationship between a group and it's members, many to many relationship, so groupid and userid are the columns Departments -> basic department info includi...

Setting the default orderby for an entity in the mapping file?

Is it possible to set the default order-by column to use in the NHibernate mapping file? Unless explicitly defined I want to order all specific entities by the Name column automatically. I've seen that it can be done on collections but thats not what I'm after in this case. ...

I want to use NAnt's foreach to iterate files in a folder, how to force alphabetic iteration?

I have an NAnt task "ship" to package my current .sql scripts into a build, then name the build with an incrementing int {######} and copy it to a build folder. I have another NAnt task which executes those build scripts. They must execute in order, but in my last attempt, they were not. Can I "force" NAnt to work alphabetically? Tha...

How can I order by Greek Letters and Normal Letters in SQL Server?

I have a mix of greek and normal words and now the order of these is that the normal words comes first and then all the greek words. I want them to get mixed in the order so beta in greek is the same as B. How can I make this ORDER BY in SQL Server? ...

Problem joining tables where joined table needs to be ordered before grouping

I have a scenario, which is seemingly simple on paper, but I'm having trouble getting to work as desired in practice. I have two tables (only relevant columns presented): | Thread +---------- | ThreadID | Post +---------- | PostID | ThreadID | Posted (Datetime) Now, what I want to do, is to join Thread and Post, grouping by ThreadI...

c# Subsonic 2.1 : Order by case

Hi All, I would like to know if its possible to order by a specific ID in subsonic 2.1. Same as regular sql query: ORDER BY CASE WHEN ID = 5 then 1 WHEN ID = 10 then 2 WHEN ID = 1 then 3 WHEN ID = then 4 else 5 END I hope someone can help me, I want this functionality for country specific content. Kind regards, Mark ...