sql-order-by

Oracle Insert Select with order by

被刻印的时光 ゝ 提交于 2019-11-27 07:56:37
问题 I am working on a plsql procedure where i am using an insert-select statement. I need to insert into the table in ordered manner. but the order by i used in the select sql is not working. is there any specific way in oracle to insert rows in orderly fashion? 回答1: The typical use case for an ordered insert is in order to co-locate particular value in the same blocks (effectively reducing the clustering factor on indexes on columns by which you have ordered the data). This generally requires a

MS-Access -> SELECT AS + ORDER BY = error

我只是一个虾纸丫 提交于 2019-11-27 07:54:27
问题 I'm trying to make a query to retrieve the region which got the most sales for sweet products. 'grupo_produto' is the product type, and 'regiao' is the region. So I got this query: SELECT TOP 1 r.nm_regiao, (SELECT COUNT(*) FROM Dw_Empresa WHERE grupo_produto='1' AND cod_regiao = d.cod_regiao) as total FROM Dw_Empresa d INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao ORDER BY total DESC Then when i run the query, MS-Access asks for the "total" parameter. Why it doesn't consider the

Is SQL order by clause guaranteed to be stable ( by Standards)

感情迁移 提交于 2019-11-27 07:48:02
问题 I am using following query to query DB which have order by on two columns. SELECT a,b,c from Table1 Order By a asc, b asc; My question is, Is the sorting guaranteed to be stable (by Standards) or not. Though it doesn't make any sense for it to be, not be stable, But i ask this because I read on net that The standard does not prevent the use of a stable sort, but it also does not require it. 回答1: The sort is not guaranteed to be stable. I think the SQL Server documentation has a good

Hibernate order by with nulls last

核能气质少年 提交于 2019-11-27 07:48:01
Hibernate used with PostgreSQL DB while ordering desc by a column puts null values higher than not null ones. SQL99 standard offers keyword "NULLS LAST" to declare that null values should be put lower than not nulls. Can "NULLS LAST" behaviour be achieved using Hibernate's Criteria API? Pascal Thivent Given that HHH-465 is not fixed and is not going to get fixed in a near future for the reasons given by Steve Ebersole, your best option would be to use the CustomNullsFirstInterceptor attached to the issue either globally or specifically to alter the SQL statement. I'm posting it below for the

SQL Server: ORDER BY in subquery with UNION

拜拜、爱过 提交于 2019-11-27 07:47:02
问题 i have two queries being combined with a UNION ALL 1 : --Query 1 SELECT Flavor, Color FROM Friends --Query 2 SELECT Flavor, (SELECT TOP 1 Color FROM Rainbows WHERE Rainbows.StrangerID = Strangers.StrangerID ORDER BY Wavelength DESC ) AS Color FROM Strangers Both of which, of course, work fine separately, but when combined with a UNION ALL : SELECT Flavor, Color FROM Friends UNION ALL SELECT Flavor, (SELECT TOP 1 Color FROM Rainbows WHERE Rainbows.StrangerID = Strangers.StrangerID ORDER BY

Getting row number for query

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 07:31:10
I have a query which will return one row. Is there any way I can find the row index of the row I'm querying when the table is sorted? I've tried rowid but got #582 when I was expecting row #7. Eg: CategoryID Name I9GDS720K4 CatA LPQTOR25XR CatB EOQ215FT5_ CatC K2OCS31WTM CatD JV5FIYY4XC CatE --> C_L7761O2U CatF <-- I want this row (#5) OU3XC6T19K CatG L9YKCYAYMG CatH XKWMQ7HREG CatI I've tried rowid with unexpected results: SELECT rowid FROM Categories WHERE CategoryID = 'C_L7761O2U ORDER BY Name EDIT: I've also tried J Cooper's suggestion (below), but the row numbers just aren't right. using

mysql query order by multiple items

狂风中的少年 提交于 2019-11-27 07:09:19
is is possible to order by multiple rows? I want my users to be sorted by last_activity, but at the same time, i want the users with pictures to appear before the ones without Something like this: SELECT some_cols FROM `prefix_users` WHERE (some conditions) ORDER BY last_activity, pic_set DESC; ihorko SELECT some_cols FROM prefix_users WHERE (some conditions) ORDER BY pic_set DESC, last_activity; Sort by picture and then by activity: SELECT some_cols FROM `prefix_users` WHERE (some conditions) ORDER BY pic_set, last_activity DESC; Pankaj Yadav SELECT id, user_id, video_name FROM sa_created

SQLite Order By Date1530019888000

谁都会走 提交于 2019-11-27 07:02:39
Every record in my SQLite database contains a field which contains a Date stored as a string in the format 'yyyy-MM-dd HH:mm:ss' . Is it possible to query the database to get the record which contains the most recent date please? paritosh you can do it like this SELECT * FROM Table ORDER BY date(dateColumn) DESC Limit 1 For me I had my query this way to solve my problem select * from Table order by datetime(datetimeColumn) DESC LIMIT 1 Since I was storing it as datetime not date column You need to convert it to unix timestamp, and then compare them: SELECT * FROM data ORDER BY strftime('%s',

Order row selection by multiple columns

こ雲淡風輕ζ 提交于 2019-11-27 07:01:08
问题 I have a database id | parentid | name 1 | 0 | CatOne 2 | 0 | CatTwo 3 | 0 | CatThree 4 | 1 | SubCatOne 5 | 1 | SubCatOne2 6 | 3 | SubCatThree How I can select this cats Order By id , parentid ? That is CatOne 1 --SubCatOne 4 --SubCatOne2 5 CatTwo 2 CatThree 3 --SubCatThree 6 回答1: This should do it... with exception of a double dash "--" prefix to the name... SELECT t1.name, t1.id FROM Table1 t1 ORDER BY case when t1.parentID = 0 then t1.ID else t1.ParentID end, case when t1.parentID = 0 then

SQL Order By Count

僤鯓⒐⒋嵵緔 提交于 2019-11-27 06:57:12
If I have a table and data like this: ID | Name | Group 1 Apple A 2 Boy A 3 Cat B 4 Dog C 5 Elep C 6 Fish C and I wish to order it according to the total of Group from smallest to largest value, such as : A - 2 records , B - 1 record , C - 3 records , so it will become: 3 Cat B 1 Apple A 2 Boy A 4 Dog C 5 Elep C 6 Fish C I tried $sql = "SELECT ID,Name FROM table ORDER BY COUNT(Group)"; but it just returns one result for me. Are there any hints? Thank you. You need to aggregate the data first, this can be done using the GROUP BY clause: SELECT Group, COUNT(*) FROM table GROUP BY Group ORDER BY