sql-order-by

LINQ - writing a query with distinct and orderby

故事扮演 提交于 2019-12-05 00:23:45
I'm quite new to LINQ. Suppose that I had the following table: Incident ID DeviceID Time Info 1 1 5/2/2009 d 2 2 5/3/2009 c 3 2 5/4/2009 b 4 1 5/5/2009 a In LINQ, how could I write a query that finds the most recent and distinct (on Device ID) set of incidents? The result I'd like is this: ID DeviceID Time Info 3 2 5/4/2009 b 4 1 5/5/2009 a Do you have to create an IEqualityComparer to do this? You can get the most recent incidents for each device (this is how I understood your question) with: var query = incidents.GroupBy(incident => incident.DeviceID) .Select(g => g.OrderByDescending

OrderBy(“it.” + sort) — Hard coding in LINQ to Entity framework?

旧巷老猫 提交于 2019-12-04 23:51:06
问题 I have been trying to use dynamic LINQ to Entity in my application for specifying the OrderBy attribute at runtime. However when using the code as described in the majority of documentation: var query = context.Customer.OrderBy("Name"); I received the following exception: System.Data.EntitySqlException: 'Name' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced

Is PostgreSQL order fully guaranteed if sorting on a non-unique attribute? [duplicate]

懵懂的女人 提交于 2019-12-04 23:30:08
Possible Duplicate: Why do results from a SQL query not come back in the order I expect? From reading 7.5 Sorting Rows and from issues I've seen with PostgreSQL, my impression is the following, but that section is not fully explicit, so I would be grateful if someone could verify: SELECT * FROM items; has no guaranteed order. SELECT * FROM items ORDER BY published_date ASC; guarantees that two items with different dates come in a given order, but does not guarantee that two items with the same date always come in the same order. SELECT * FROM items ORDER BY published_date ASC, id ASC; always

Determine whether an IQueryable<T> has been ordered or not

蓝咒 提交于 2019-12-04 22:20:46
Is there a way to know if an IQueryable<T> has been ordered (using OrderBy or OrderbyDescending )? So I know whether to call OrderBy or ThenBy on the collection. IQueryable<Contact> contacts = Database.GetContacts(); I tried contacts is IOrderedQueryable<Contact> , but it's always true. Edit : I just changed my example, the previous one wasn't really showing my point. Assume that GetContacts uses Entity Framework and simply returns all the records of a table. Later on, I apply several functions to contacts , I have no knowledge of what those functions do. They can sort or filter the IQueryable

MySQL Orderby a number, Empty Strings (or 0's) Last

拈花ヽ惹草 提交于 2019-12-04 21:37:47
问题 Just asked a question pretty similar to this one... Currently I am doing a very basic OrderBy in my statement. SELECT * FROM tablename WHERE visible=1 ORDER BY position ASC, id DESC The problem with this is that empty string entries for 'position' are treated as 0. Therefore all entries with position as empty string appear before those with 1,2,3,4. eg: '', '', '', 1, 2, 3, 4 or: 0, 0, 0, 1, 2, 3, 4 Is there a way to achieve the following ordering: 1, 2, 3, 4, '', '', ''. or: 1, 2, 3, 4, 0, 0

BLOB data returned in MySQL using AES_DECRYPT with ORDER clause

吃可爱长大的小学妹 提交于 2019-12-04 21:35:31
I'm creating a system in which users can store messages via PHP with a MySQL database, and I am using the MySQL AES_ENCRYPT function to encrypt the contents of these messages. Here is my posts table: CREATE TABLE IF NOT EXISTS `posts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user` int(11) DEFAULT NULL, `group` int(11) DEFAULT NULL, `body` varbinary(1000) NOT NULL, `ip` varchar(45) NOT NULL, `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `replyto` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `replyto` (`replyto`), KEY `user` (`user`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT

Return records distinct on one column but order by another column

醉酒当歌 提交于 2019-12-04 19:44:37
I am building a Rails 3 app with a pretty standard message model. I would like to return the most recently created message records for each unique conversation_id. It seems like a fairly simple task, but I have not been able to code or find a working solution. Admittedly, I am not super SQL savvy either (as I have gotten by with mainly Active Record queries thus far). Here is what I'm trying to accomplish. Sample messages table: | id | sender_id | receiver_id | conversation_id | subject | body | created_at | | 1 | * | * | 1 | * | * | 16:01 | | 2 | * | * | 2 | * | * | 17:03 | | 3 | * | * | 1 |

Distinct Records with joins and order

不打扰是莪最后的温柔 提交于 2019-12-04 19:24:24
问题 I have a simple relationship between User and Donations in that a user has many donations, and a donation belongs to a user. What I'd like to do is get a list of users, ordered by the most recent donations. Here's what I'm trying: First I want to get the total number of uniq users, which is working as expected: > User.joins(:donations).order('donations.created_at').uniq.count (3.2ms) SELECT DISTINCT COUNT(DISTINCT "users"."id") FROM "users" INNER JOIN "donations" ON "donations"."user_id" =

Set Order By to ignore punctuation on a per-column basis

丶灬走出姿态 提交于 2019-12-04 19:22:56
Is it possible to order the results of a PostgreSQL query by a title field that contains characters like [](),; etc but do so ignoring these punctuation characters and sorting only by the text characters? I've read articles on changing the database collation or locale but have not found any clear instructions on how to do this on an existing database an on a per-column basis. Is this even possible? If you want to have this ordering in one particular query you can ORDER BY regexp_replace(title, '[^a-zA-Z]', '', 'g') It will delete all non A-Z from sting and order by resulting field. Erwin

Order against two columns at the same time (intersecting)

ぃ、小莉子 提交于 2019-12-04 18:34:15
I have a table with the fields CommonName and FirstName . Only either field has data, never both. Is there a way to order rows in an intersecting manner on SQL Server? Example: CommonName FirstName Bern Wade Ashley Boris Ayana I want records ordered like this: CommonName FirstName Ashley Ayana Bern Boris Wade Is this possible, and if so, how? Use a CASE statement to select the value for that row and ORDER BY that. ORDER BY CommonName + FirstName , with appropriate ISNULL(<column>, '') if they are nullable. ORDER BY CASE WHEN CommonName is null THEN FirstName ELSE CommonName END order by