sql-order-by

What is the purpose of Order By 1 in SQL select statement?

﹥>﹥吖頭↗ 提交于 2019-11-29 21:09:44
I'm reading through some old code at work, and have noticed that there are several views with an order by 1 clause. What does this accomplish? Example: Create view v_payment_summary AS SELECT A.PAYMENT_DATE, (SELECT SUM(paymentamount) FROM payment B WHERE PAYMENT_DATE = B.PAYMENT_DATE and SOME CONDITION) AS SUM_X, (SELECT SUM(paymentamount) FROM payment B WHERE PAYMENT_DATE = B.PAYMENT_DATE and SOME OTHER CONDITION) AS SUM_Y FROM payment A ORDER BY 1; This: ORDER BY 1 ...is known as an "Ordinal" - the number stands for the column based on the number of columns defined in the SELECT clause. In

MySQL order by relevance

强颜欢笑 提交于 2019-11-29 18:38:30
问题 I have a search form which searches a site content table to pull back appropriate results. I want to search the title and content fields and pull back results in order of relevance. Giving highest priority to the title. Say we had a table (tblContent) of intID | strTitle | txtContent 1 | Smith John | Lorem Ipsum 2 | Lorem Ipsum | Lorem John Smith Ipsum 3 | John Smith | Lorem Ipsum Lorem Ipsum 4 | Lorem Ipsum | Lorem Ipsum Lorem Ipsum 5 | Lorem Ipsum | Lorem Ipsum Smith John And you were

How to use CASE function in ORDER BY?

扶醉桌前 提交于 2019-11-29 16:36:27
My friend asked a question a few times ago. Also there is a answer under that and it is good, but not for my case. The idea of that solution is joining the current table to itself. That seems expensive and not effective for me, Because in reality there is four join on these tables ( votes , favorites , comments , viewed ) in my query. Now I want to know, how can I do that using CASE function? Something like this: ... ORDER BY Type, CASE WHEN AcceptedAnswerId = Id THEN 1 ELSE 0, timestamp Or is there any better solution? To be more readable, I paste those examples here: I have a table like this

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

风流意气都作罢 提交于 2019-11-29 16:31:20
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 test4 3 1 i want to select this record in a such a way that it should be sorted in the order of both int_category_id and int_order so the result should be like below int_record_id int_category_id str_name int_order bit_active 2 1 test2 1 1 1 1 test1 2 1 3 2 test3 1 1 4 3 test4 3 1 Does any one have an idea about its sql query, i have tried a lot i'm not getting the result correct. can any one show me the exact sql query for this. select * from your

mysql order by serialized data?

不羁岁月 提交于 2019-11-29 15:52:18
I need to query a single field and order it by serialized data, is that even possible? my table fields are: ********************************************* | meta_id | user_id | meta_key | meta_value | ********************************************* my query looks like this SELECT user_id FROM $wpdb->usermeta WHERE meta_key='ba_ur' which works fine but here things start to make no scene to me meta_value holds a serialized data for example a:2:{s:4:"data";s:9:"text text";s:6:"number";s:2:"22";} which when unserialized gives: array ( 'data' => 'text text', 'number' => '22', ) and i need to order by

How to Select and Order By columns not in Groupy By SQL statement - Oracle

天大地大妈咪最大 提交于 2019-11-29 15:43:24
问题 I have the following statement: SELECT IMPORTID,Region,RefObligor,SUM(NOTIONAL) AS SUM_NOTIONAL From Positions Where ID = :importID GROUP BY IMPORTID, Region,RefObligor Order BY IMPORTID, Region,RefObligor There exists some extra columns in table Positions that I want as output for "display data" but I don't want in the group by statement. These are Site, Desk Final output would have the following columns: IMPORTID,Region,Site,Desk,RefObligor,SUM(NOTIONAL) AS SUM_NOTIONAL Ideally I'd want the

Sorting a MySQL query with ORDER BY or with PHP sort functions

可紊 提交于 2019-11-29 15:42:23
I have a query that I want to sort alphabetically, but the trick is that I want the sorting to treat two columns equally. For instance, if the first row of first_col equals apple and the second row of second_col equals aardvark I want the value in the second row of second_col to be listed before the value in the first row of first_col . A value (not NULL or '' ) will always exist in every row of second_col , but the value in first_col can be '' . Hopefully I have explained this good enough. I don't care if I have to use MySQL or PHP for this, but once sorted, the array is read through and

Different behaviour in “order by” clause: Oracle vs. PostgreSQL

穿精又带淫゛_ 提交于 2019-11-29 15:23:30
I have the following table (created and populated them in Oracle and PostgreSQL): > create table foo (a varchar(10)); I populated them with values, and order by clause is behaving differently in PostgreSQL and Oracle (I don't think versions are relevant to this question): Oracle: > select a, length(a) from foo order by a; A LENGTH(A) ---------- ---------- .1 2 01 2 1 1 1#0 3 1#1 3 1.0 3 1.1 3 10 2 11 2 9 rows selected. I get what I expect. .1 before 01 , since . is before 0 in ascii table. However, in PostgreSQL I have: => select a, length(a) from foo order by a; a | length -----+-------- 01 |

MySQL order by (str to int)

戏子无情 提交于 2019-11-29 13:59:55
SELECT `01` FROM perf WHERE year = '2013' order by CAST(`01` AS INT) LIMIT 3 Column 01 has numeric values as varchar. I need to order top 3 of '01' as integer. Why doesn't this query working? Table like this; +----------------------+ | name | 01 | 02 | year| +----------------------+ |name1 | 90 |*** |2013 | +----------------------+ |name2 | 93 | 55 |2013 | +----------------------+ |name3 |*** | 78 |2013 | +----------------------+ Query should order by 01 (dismiss * ) and give names and values. MySQL doesn't permit you to CAST('01' AS INT) . It expects instead a SIGNED or UNSIGNED . SELECT `01`

Which row's fields are returned when Grouping with MySQL?

佐手、 提交于 2019-11-29 13:59:45
I have a MySQL table with the fields id and string . id s are unique. string s are varchars and are non-unique. I perform the following query: SELECT id, string, COUNT( * ) AS frequency FROM table GROUP BY string ORDER BY frequency DESC, id ASC Questions Assume the table contains three rows with identical string values, and id s 1, 2, and 3. Which id is going to be returned ( 1, 2, or 3 )? Which id is this query going to ORDER BY ( Same as is returned? ... see question 1 )? Can you control which id is returned / used for ordering? eg. Return the largest id , or the first id from a GROUP. What