sql-order-by

LINQ to SQL does not generate ORDER BY when DISTINCT is used?

陌路散爱 提交于 2019-11-27 22:16:37
The following basic LINQ to SQL statement does not result in the orderby working. As you can see in the T-SQL there is no orderby. Do you know why? LINQ to SQL: var results = (from stats in db.t_harvest_statistics orderby stats.unit_number select stats.unit_number).Distinct().ToList(); Above Results in following TSQL SELECT [Distinct1].[unit_number] AS [unit_number] FROM ( SELECT DISTINCT [Extent1].[unit_number] AS [unit_number] FROM [dbo].[t_harvest_statistics] AS [Extent1] ) AS [Distinct1] That is a limitation of SQL and Relational Algebra of where the ORDER BY is in relation to the DISTINCT

What is the default order of a list returned from a Django filter call?

自古美人都是妖i 提交于 2019-11-27 21:47:11
Short Question What is the default order of a list returned from a Django filter call when connected to a PostgreSQL database? Background By my own admission, I had made a poor assumption at the application layer in that the order in which a list is returned will be constant, that is without using 'order_by'. The list of items I was querying is not in alphabetic order or any other deliberate order. It was thought to remain in the same order as which they were added to the database. This assumption held true for hundreds of queries, but a failure was reported by my application when the order

how does SELECT TOP works when no order by is specified?

我只是一个虾纸丫 提交于 2019-11-27 21:35:51
问题 The msdn documentation says that when we write SELECT TOP(N) ..... ORDER BY [COLUMN] We get top(n) rows that are sorted by column ( asc or desc depending on what we choose) But if we don't specify any order by, msdn says random as Gail Erickson pointed out here. As he points out it should be unspecified rather then random . But as Thomas Lee points out there that When TOP is used in conjunction with the ORDER BY clause, the result set is limited to the first N number of ordered rows;

Orderby() not ordering numbers correctly c#

北战南征 提交于 2019-11-27 21:05:41
问题 I am writing an app for my company and am currently working on the search functionality. When a user searches for an item, I want to display the highest version (which is stored in a database). The problem is, the version is stored as a string instead of int, and when I do an OrderBy(q=>q.Version) on the results, they are returned like 1 10 11 2 3 ... Obviously 2 comes before 10. Is there a way for me to cast the version as an integer or is there a simple IComparer out there? I couldn't find

mysql order by with union doesn't seem to work

跟風遠走 提交于 2019-11-27 20:56:31
问题 Here is my query (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title` LIKE "%only three doors%" OR `joke` LIKE "%only three doors%") ORDER BY `ups` DESC,`downs` ASC) UNION (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title` LIKE "%only%" OR `joke` LIKE "%only%") ORDER BY `ups` DESC,`downs` ASC) UNION (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title` LIKE "%three%" OR `joke` LIKE "%three%") ORDER BY `ups` DESC,`downs` ASC) UNION (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title` LIKE

“Order By” using a parameter for the column name

女生的网名这么多〃 提交于 2019-11-27 20:34:41
We would like to use a parameter in the "Order By" clause of a query or stored procedure created with the Visual Studio DataSet Designer. Example: FROM TableName WHERE (Forename LIKE '%' + @SearchValue + '%') OR (Surname LIKE '%' + @SearchValue + '%') OR (@SearchValue = 'ALL') ORDER BY @OrderByColumn This error is displayed: Variables are only allowed when ordering by an expression referencing a column name. You should be able to do something like this: SELECT * FROM TableName WHERE (Forename LIKE '%' + @SearchValue + '%') OR (Surname LIKE '%' + @SearchValue + '%') OR (@SearchValue = 'ALL')

Varchar to number conversion for sorting

青春壹個敷衍的年華 提交于 2019-11-27 20:04:44
I have a query ordered by column : select * from mytable order by column asc — sort table column type is varchar, so the output is: 1 10 100 11 12 13 How should I sort if I want them to sort by numeric value so the output is: 1 10 11 12 13 100 Use: order by cast(column as unsigned) asc You can use this if you want to treat column as INT only: SELECT * FROM mytable ORDER BY column+0; 1 10 11 12 13 100 Or this if you want to treat column as both INT and VARCHAR SELECT * FROM mytable ORDER BY column+0, column; #this will sort the column by VARCHAR first and then sort it by INT abc xyz 1 10 11 12

ORDER BY “ENUM field” in MYSQL

為{幸葍}努か 提交于 2019-11-27 19:19:38
There is a field 'noticeBy' enum('email','mobile','all','auto','nothing') NOT NULL DEFAULT 'auto'. As it known ordering by ENUM field performs relative to its index. However, how it possible make order by its values? As documented under Sorting : ENUM values are sorted based on their index numbers, which depend on the order in which the enumeration members were listed in the column specification. For example, 'b' sorts before 'a' for ENUM('b', 'a') . The empty string sorts before nonempty strings, and NULL values sort before all other enumeration values. To prevent unexpected results when

ORDER BY date and time BEFORE GROUP BY name in mysql

試著忘記壹切 提交于 2019-11-27 19:14:22
i have a table like this: name date time tom | 2011-07-04 | 01:09:52 tom | 2011-07-04 | 01:09:52 mad | 2011-07-04 | 02:10:53 mad | 2009-06-03 | 00:01:01 i want oldest name first: SELECT * ORDER BY date ASC, time ASC GROUP BY name (->doesn't work!) now it should give me first mad(has earlier date) then tom but with GROUP BY name ORDER BY date ASC, time ASC gives me the newer mad first because it groups before it sorts! again: the problem is that i can't sort by date and time before i group because GROUP BY must be before ORDER BY! Another method: SELECT * FROM ( SELECT * ORDER BY date ASC, time

TSQL - Is it possible to define the sort order?

*爱你&永不变心* 提交于 2019-11-27 18:47:14
Is it possible to define a sort order for the returned results? I would like the sort order to be 'orange' 'apple' 'strawberry' not ascending or descending. I know ORDER BY can do ASC or DESC but is there a DEFINED('orange', 'apple', 'strawberry') type thing? This will be running on SQL Server 2000. It's incredibly clunky, but you can use a CASE statement for ordering: SELECT * FROM Blah ORDER BY CASE MyColumn WHEN 'orange' THEN 1 WHEN 'apple' THEN 2 WHEN 'strawberry' THEN 3 END Alternately, you can create a secondary table which contains the sort field and a sort order. TargetValue SortOrder