sql-order-by

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

眉间皱痕 提交于 2019-11-28 13:41:42
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. The sort is not guaranteed to be stable. I think the SQL Server documentation has a good explanation of how to achieve a stable sort: To achieve stable results between query requests using OFFSET and FETCH,

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

亡梦爱人 提交于 2019-11-28 13:35:13
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 newly created 'column' I made in the select clause? Thanks in advance! Aliases are only usable in the query

SQL Server: ORDER BY in subquery with UNION

被刻印的时光 ゝ 提交于 2019-11-28 13:23:43
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 Wavelength DESC ) AS Color FROM Strangers The query fails with the error: Msg 104, Level 15, State 1, Line

Query to ORDER BY the number of rows returned from another SELECT

大兔子大兔子 提交于 2019-11-28 13:11:10
I'm trying to wrap my head around SQL and I need some help figuring out how to do the following query in PostgreSQL 9.3. I have a users table, and a friends table that lists user IDs and the user IDs of friends in multiple rows. I would like to query the user table, and ORDER BY the number of mutual friends in common to a user ID. So, the friends table would look like: user_id | friend_user_id 1 | 4 1 | 5 2 | 10 3 | 7 And so on, so user 1 lists 4 and 5 as friends, and user 2 lists 10 as a friend, so I want to sort by the highest count of user 1 in friend_user_id for the result of user_id in

PostgreSQL - order by an array

耗尽温柔 提交于 2019-11-28 12:22:50
I have 2 tables - course that contains id and name of the courses and tagCourse that contains tags for each course. course tagcourse ------------ ---------------- PK id_course PK tag name PK, FK id_course I'd like to write a function that searches courses by given array of tags and returns them ordered by quantity of matching tags. However I don't know how to write it correctly and in an efficient way. Please help me. ie. CREATE OR REPLACE FUNCTION searchByTags(tags varchar[]) RETURNS SETOF..... RETURN QUERY SELECT * FROM course c INNER JOIN tagcourse tc ON c.id_course = tc.id_course WHERE ???

How to re-use result for SELECT, WHERE and ORDER BY clauses?

浪尽此生 提交于 2019-11-28 12:21:21
The following query returns the venues near us (lat: 62.0, lon: 25.0) inside whose radius we fall in ordered by distance: SELECT *, earth_distance(ll_to_earth(62.0, 25.0), ll_to_earth(lat, lon)) AS distance FROM venues WHERE earth_distance(ll_to_earth(62.0, 25.0), ll_to_earth(lat, lon)) <= radius ORDER BY earth_distance(ll_to_earth(62.0, 25.0), ll_to_earth(lat, lon)) Is it possible (and advisable) to re-use the result from earth_distance(ll_to_earth(62.0, 25.0), ll_to_earth(lat, lon)) instead of computing it separately for SELECT, WHERE and ORDER BY clauses? Erwin Brandstetter In the GROUP BY

order by a parameter

梦想的初衷 提交于 2019-11-28 12:03:45
Hi i have a store procedure, where i do a select query. I want order this by an external parameter. I post an minimal example: CREATE PROCEDURE [dbo].[up_missioni_get_data] @order VarChar(100) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here SELECT * from missioni ORDER BY ... END What can i write in order by for do that? thanks You have 2 options, either use a CASE statement, or use dynamic sql This would be an example of the CASE statement DECLARE @Table TABLE( Col1 VARCHAR(10),

UPDATE with ORDER BY

回眸只為那壹抹淺笑 提交于 2019-11-28 12:01:38
Need to "tie" UPDATE with ORDER BY . I'm trying to use cursors, but get the error: cursor "cursupd" doesn't specify a line, SQL state: 24000 Code: BEGIN; DECLARE cursUpd CURSOR FOR SELECT * FROM "table" WHERE "field" = 5760 AND "sequence" >= 0 AND "sequence" < 9 ORDER BY "sequence" DESC; UPDATE "table" SET "sequence" = "sequence" + 2 WHERE CURRENT OF cursUpd; CLOSE cursUpd; COMMIT; How to do it correctly? UPDATE 1 Without cursor, when I do like this: UPDATE "CableLinePoint" AS "t" SET "sequence" = t."sequence" + 2 from ( select max("sequence") "sequence", "id" from "CableLinePoint" where

MySQL order posts by most recent comment OR last posted

被刻印的时光 ゝ 提交于 2019-11-28 11:45:12
How can I sort posts so most recent activity is on top? # Schema not including all info, including FKs CREATE TABLE post( id int unsigned primary key auto_increment, msg text, created datetime )ENGINE=InnoDb; CREATE TABLE comment( id int unsigned primary key auto_increment, post_id int unsigned, msg text, created datetime )ENGINE=InnoDb; I want to order posts by most recent, where a new post is obviously more recent than one previously posted, but an old post that has a recent comment associated with it qualifies as more recent still. 1st attempt # Selecting '*' for simplicity in this example

Why can't i refer to a column alias in the ORDER BY using CASE?

那年仲夏 提交于 2019-11-28 11:06:10
Sorry if this a duplicate, but i haven't found one. Why can't i use my column alias defined in the SELECT from the ORDER BY when i use CASE ? Consider this simple query: SELECT NewValue=CASE WHEN Value IS NULL THEN '<Null-Value>' ELSE Value END FROM dbo.TableA ORDER BY CASE WHEN NewValue='<Null-Value>' THEN 1 ELSE 0 END The result is an error: Invalid column name 'NewValue' Here's a sql-fiddle. (Replace the ORDER BY NewValue with the CASE WHEN... that´'s commented out) I know i can use ORDER BY CASE WHEN Value IS NULL THEN 1 ELSE 0 END like here in this case but actually the query is more