sql-order-by

PostgreSQL: sort an array of elements using some sorting condition

Deadly 提交于 2019-12-12 14:02:53
问题 Suppose you need to sort an array of numranges by, say, descending left boundary. Is the following approach the simplest: unnest the array into a table, sort the table, array_agg it back into an array. How would that look in code? Here is my non-working attempt: DO $$ DECLARE x numrange[] := '{"[0, 3]", "[0, 1]", "[3, 5]", "[3, 8]"}'; BEGIN x := ( WITH x AS ( SELECT xrow FROM unnest(x) AS xrow ) SELECT array_agg(xrow) FROM x ORDER BY lower(xrow) DESC ); RAISE NOTICE '%', x; END; $$; 回答1: You

Files in directory sort by fileName ascending

断了今生、忘了曾经 提交于 2019-12-12 13:26:16
问题 I am having a list of files from a directory and I want to sort it out by filename. This is the main code: var localPath = this.Server.MapPath("~/Content/Img/" + type + "/"); var directory = new DirectoryInfo(localPath); isDirectory = directory.Exists; if (isDirectory) { foreach (FileInfo f in directory.GetFiles()) { Picture picture = new Picture(); picture.ImagePath = path; picture.CreationDate = f.CreationTime; picture.FileName = f.Name; listPictures.Add(picture); } } here is the class

OrderBy clause is resulting different result sets when order column having same data

爷,独闯天下 提交于 2019-12-12 13:21:47
问题 We have a stored proc to return set of records based on Page Number and Page Size. Sorting is being done by a column " CreateDateTime ". If value of CreatedDateTime is same for all the records, it is giving the results sets in different orders. The behavior is inconsistent. Some Portion of Code: SET @FirstRec = ( @PageNo - 1 ) * @PageSize SET @LastRec = ( @PageNo *@PageSize + 1 ) SELECT * FROM ( select ROW_NUMBER() OVER (ORDER BY CreatedDateTime) AS rowNumber,EMPID From Employee ) as KeyList

Preserve the order of distinct inside string_agg

一世执手 提交于 2019-12-12 13:19:34
问题 My SQL function: with recursive locpais as ( select l.id, l.nome, l.tipo tid, lp.pai from loc l left join locpai lp on lp.loc = l.id where l.id = 12554 union select l.id, l.nome, l.tipo tid, lp.pai from loc l left join locpai lp on lp.loc = l.id join locpais p on (l.id = p.pai) ) select * from locpais gives me 12554 | PARNA Pico da Neblina | 9 | 1564 12554 | PARNA Pico da Neblina | 9 | 1547 1547 | São Gabriel da Cachoeira | 8 | 1400 1564 | Santa Isabel do Rio Negro | 8 | 1400 1400 | RIO NEGRO

UNION and ORDER BY issue in MySQL

点点圈 提交于 2019-12-12 12:43:33
问题 You should see what I'm trying to do here but it's not working $getquery = "SELECT * FROM highscore WHERE score >= '$score' ORDER BY score ASC LIMIT 6 UNION SELECT * FROM highscore WHERE score < '$score' ORDER BY score DESC LIMIT 5"; mysql_error() returns: "improper usage of ORDER BY and UNION". 回答1: Try: $getquery = "(SELECT * FROM highscore WHERE score >= '$score' ORDER BY score ASC LIMIT 6) UNION ALL -- guaranteed to be beneficial in this case as Johan commented (SELECT * FROM highscore

MySQL: How to GROUP BY a field to retrieve the rows with ORDER BY another field?

怎甘沉沦 提交于 2019-12-12 12:33:31
问题 assume following data: Data: id | date | name | grade --------+---------------+-----------+--------------- 1 | 2010/12/03 | Mike | 12 2 | 2010/12/04 | Jenny | 12 3 | 2010/12/04 | Ronald | 15 4 | 2010/12/03 | Yeni | 11 i want to know who has the best grade in each day, something like this: Desired Result: id | date | name | grade --------+---------------+-----------+--------------- 1 | 2010/12/03 | Mike | 12 3 | 2010/12/04 | Ronald | 15 i thought query should look like this: SELECT name FROM

Linq: How to OrderBy the SUM of one table and the COUNT of another

五迷三道 提交于 2019-12-12 12:32:49
问题 As per a previous question, I've got the following LINQ expression. Events.Where(Function(e) e.EventDate >= Date.Today) _ .OrderByDescending(Function(e) (((e.EventVotes.Sum(Function(s) s.Vote)) * 2) + (e.Comments.Count))) _ .Skip(0) _ .Take(5) Which converts to the following SQL -- Region Parameters DECLARE @p0 DateTime2 = '2011-01-17 00:00:00.0000000' DECLARE @p1 Int = 2 DECLARE @p2 Int = 0 DECLARE @p3 Int = 5 -- EndRegion SELECT [t3].[ID], [t3].[UserID], [t3].[RegionID], [t3].[LocationID],

Rails 3 ORDER BY FIELD and last

青春壹個敷衍的年華 提交于 2019-12-12 12:25:54
问题 Hello I have an issue with Rails 3.2 and ordering. When wanting to order a collection by a field, when calling .last ActiveRecord behaves weirdly... >> User.order("FIELD(id, '1')") User Load (0.4ms) SELECT `users`.* FROM `users` ORDER BY FIELD(id, '1') => [] >> User.order("FIELD(id, '1')").first User Load (0.4ms) SELECT `users`.* FROM `users` ORDER BY FIELD(id, '1') LIMIT 1 => nil >> User.order("FIELD(id, '1')").last User Load (0.3ms) SELECT `users`.* FROM `users` ORDER BY FIELD(id DESC, '1')

SQL Server MERGE statement and ORDER BY clause

自古美人都是妖i 提交于 2019-12-12 11:34:25
问题 I would like to write a MERGE statement to pick TOP 10 rows from a large table by using ORDER BY clause and update it’s one of the column values. MERGE statement allows me to pick TOP 10 rows but I could not put ORDER BY clause anywhere. MERGE TOP(10) StudentAllocation AS SA USING (SELECT @sub_id AS subId) AS TSA ON SA.sub_id = TSA.subId WHEN MATCHED THEN UPDATE SET SA.exam_batch = 1); 回答1: You can use a table expression as both the source and target for the MERGE . WITH SA AS ( SELECT TOP(10

Symfony2 createQuery order by field

不羁的心 提交于 2019-12-12 11:23:33
问题 Hi i have this query writen in phpmyadmin and it works gr8. SELECT u.* FROM users AS u WHERE u.id = 14469 OR u.id = 685 ORDER BY u.id, field(u.id, 14469, 685) But i need to write it in symfony2. How it will looks like? Because this is throwing me an error: $query=$this->_em->createQuery("SELECT u FROM UserBundle:User u WHERE u.id = 14469 OR u.id = 685 ORDER BY u.id, field(u.id, 14469, 685) "); An exception has been thrown during the rendering of a template ("[Syntax Error] line 0, col 122: