sql-order-by

How to sort objects in an array inside a json or jsonb value by a property of the objects?

纵然是瞬间 提交于 2019-12-13 15:10:01
问题 I have this pl/pgsql function to aggregate rows from two tables in a jsonb value ( data_table_1 and data_table_2 ). fk_id is a common foreign key id in both tables: DECLARE v_my_variable_1 jsonb; v_my_variable_2 jsonb; v_combined jsonb; BEGIN SELECT json_agg( data_table_1 ) INTO v_my_variable FROM data_table_1 WHERE fk_id = v_id; SELECT json_agg( data_table_2 ) into v_my_variable_2 FROM data_table_2 WHERE fk_id = v_id; SELECT v_my_variable || v_my_variable_2 into v_combined; Now I want to

LINQ: How to dynamically use an ORDER BY in linq but only if a variable is not string.empty or null

亡梦爱人 提交于 2019-12-13 14:47:04
问题 I am using LINQ2SQL and its working pretty well. However depending on the value of the variable type string in C#, I need to use "Order By" in my query or not use an "order by". If the C# string is NOT null, or empty, then I want to "order by" on the contents of the string variable. If the C# string is empty or null, then I don't include an order by. Is it possible to write this kind of query? 回答1: Do like in VVS's answer, but if you want to pass in the column name for ordering you may want

Where is the official documentation for T-SQL's “ORDER BY RAND()” and “ORDER BY NEWID()”?

北慕城南 提交于 2019-12-13 11:03:52
问题 I'm looking for the official T-SQL documentation for "ORDER BY RAND()" and "ORDER BY NEWID()". There are numerous articles describing them, so they must be documented somewhere. I'm looking for a link to an official SQL Server documentation page like this: http://technet.microsoft.com/en-us/library/ms188385.aspx CLARIFICATION: What I'm looking for is the documentation for "order_by_expression" that explains the difference in behavior between a nonnegative integer constant, a function that

mySQL Largest number by group

牧云@^-^@ 提交于 2019-12-13 10:53:34
问题 I have messed around with this code for quite sometime. First thing I have my SQL table setup as char instead of decimal because I don't want the number to always show a decimal value unless it has a decimal value (Is there another way to do this?). I change it to decimal within my code but I believe this is where the problem lies. I am trying to pull the largest fish per user per species but it isn't working out. SELECT * FROM (SELECT * FROM entries ORDER BY CAST(weight AS DECIMAL(9,3)) DESC

How can I order by two columns, if result is equal, consider another column?

我的梦境 提交于 2019-12-13 10:51:31
问题 I have tables called marks, total_marks, and aggregate. I want to sort the results first depending on the aggregate, but when the aggregate in the same column is equal to the next, then consider the student with the highest total. I.e., order by aggregate, but if the last aggregate is equal to the new one, how can I then give the highest rank to the one with higher total? 回答1: With SQL you can sort by multiple columns: SELECT * from marks ORDER BY aggregate DESC, total_marks DESC This will

MySQL ORDER BY or GROUP BY [duplicate]

早过忘川 提交于 2019-12-13 10:07:37
问题 This question already has an answer here : MySQL SELECT and ORDER BY [closed] (1 answer) Closed 6 years ago . In relation to: MySQL ORDER BY Customized I have another question. We have an id_competitor with various scores. id_competitor score 1 WIN 2 50+ 3 90+ 4 90+ 1 50 2 WIN 3 40 4 40+ I would like to use order by but in the following order: id_competitor 2 1 4 3 I dont know how i should do it, with SELECT DISTINCT with ORDER BY or GROUP BY 回答1: based on other answer, I would do something

How to find the top three column totals using SQL

故事扮演 提交于 2019-12-13 08:37:52
问题 Trying to sum all columns in my table and to find the top 3 of them. columns have only a value of 1 or 0 . That's why I am trying to sum the all inputs to compare them with each other. I've stucked with order by code integrated into sum() SELECT (ID) FROM Student ORDER BY SUM(C1), SUMC(C2)...SUM(C10) lIMIT 3 回答1: If I understand correctly, you can use union all to calculate the sum for each column and then order by and limit : select c.* from ((select 'col1', sum(col1) as s from t) union all

Postgresql table with one ID column, sorted index, with duplicate primary key

淺唱寂寞╮ 提交于 2019-12-13 08:34:39
问题 I want to use a PostgreSQL table as a kind of work queue for documents. Each document has an ID and is stored in another, normal table with lots of additional columns. But this question is about creating the table for the work queue. I want to create a table for this queue without OIDs with just one column: The ID of the document as integer. If an ID of a document exists in this work queue table, it means that the document with that ID is dirty and some processing has to be done. The extra

SQL count() over 2 tables by a type

巧了我就是萌 提交于 2019-12-13 07:45:33
问题 I have a question how to make the right sql statement, because I have no Idea how to start ;-( There are the following two tables: Table 1: test id|name|type ------------- 1|FirstUnit|1 2|FirstWeb|2 3|SecondUnit|1 Table 2: types id|type -------- 1|UnitTest 2|WebTest I would like to get the following result: Table result: type|amount ----------- UnitTest|2 WebTest|1 Can anyone helping with giving a hint how to start? Thanks in advance. LStrike 回答1: You can try this query: SELECT t.type , COUNT

mysql force order of results found to match order of IN clause

被刻印的时光 ゝ 提交于 2019-12-13 07:41:53
问题 This question is different to a commonly asked question about ordering the final results by the IN clause. I would like to force the results returned by the query that contains the IN clause, to match the order of the IN clause. This is the original question that I am working from. I'd like to alter the query below so that a row containing progress=2 occurs before progress=4 and progress=7 for each session_id when ordering the formation_page_hits table by datetime . Here is the current query: