sql-order-by

Conditional SQL ORDER BY ASC/DESC for alpha columns

半世苍凉 提交于 2019-11-27 14:23:11
Writing a stored procedure in MS SQL Server 2008 R2, I want to avoid using DSQL... I would like the sort method (ASC or DESC) to be conditional. Now, with a numeric column I would simply use a case statement and negate the value to emulate ASC or DESC... That is: ... ORDER BY CASE @OrderAscOrDesc WHEN 0 THEN [NumericColumn] ELSE -[NumericColumn] END ASC What is an appropriate method for doing this with an alpha column? EDIT: I thought of a clever way but it seems terribly inefficient... I could insert my ordered alpha column into a temp table with an autonumber then sort by the autonumber

Dynamic Order in JDBI SQL Object Queries

半世苍凉 提交于 2019-11-27 14:06:33
How do you do ordering with SQL Object Queries in JDBI? I want to do something like: @SqlQuery( "SELECT * FROM users " + "WHERE something = :something " + "ORDER BY :orderBy :orderDir" ) List<User> getUsers( @Bind("something") Integer something , @BindOrderBy("orderBy") String orderBy , @BindOrderDir("orderDir") String orderDir ); or @SqlQuery( "SELECT * FROM users " + "WHERE something = :something " + "ORDER BY :orderBy :orderDir" ) List<User> getUsers( @Bind("something") Integer something , @Bind("orderBy") OrderBy orderBy , @Bind("orderDir") OrderDir orderDir ); I've recently been exploring

ORDER BY alphabet first then follow by number

限于喜欢 提交于 2019-11-27 13:43:11
I looking for some tweak in mysql ordering , I normally select record from table and then order the record by Name(varchar) ASC but the number is always come first here some example of my question ( note. mysql sort the record with 0-9 first ) SELECT name FROM list ORDER BY name ASC record returned: 1 star 2 star 9 slice Ape Age Beg Bell Fish Zoo What i want is the alphabet order come first then follow by number Desired output Ape Age Beg Bell Fish Zoo 1 star 2 star 9 slice Use the following ORDER BY clause: ORDER BY IF(name RLIKE '^[a-z]', 1, 2), name Salil Ref this SELECT name FROM list

ORDER BY ASC with Nulls at the Bottom

℡╲_俬逩灬. 提交于 2019-11-27 13:26:25
I'm writing an SQL query that connects a schools table to a districts table. Simple One-To-Many relationship where each school is attached to one district. My query is as follows: SELECT schools.id AS schoolid, schools.name AS school, districts.id AS districtid, districts.name AS district FROM sms_schools AS schools LEFT JOIN sms_districts AS districts ON schools.districtid = districts.id WHERE 1 = 1 ORDER BY districts.name, schools.name The reason I did a left join is because not every school is attached to a district. For example one school may be home schooled that may contain all students

SQL ORDER BY multiple columns [duplicate]

心不动则不痛 提交于 2019-11-27 12:55:52
问题 This question already has an answer here: SQL multiple column ordering 5 answers I want to sort my products table by two columns: prod_price and prod_name . SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_price, prod_name; How is the sorting done here? I think it happens first by prod_price and then by prod_name . Also, how is the above query different from this one: SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_name; My products table is as follows:

How do you keep the order using SELECT WHERE IN()?

眉间皱痕 提交于 2019-11-27 12:23:30
问题 Is there a way to keep the order when using SELECT WHERE IN()? For example, using the following query: SELECT id FROM data_table WHERE id IN(56,55,54,1,7); The results will come back using the default order by id. 1,7,54,55,56 When I want to keep the order used in the IN: 56,55,54,1,7 Is there a quick way to do this in mySQL or will I be forced to order it after in code. Thanks :) 回答1: Use FIND_IN_SET: ORDER BY FIND_IN_SET(id, '56,55,54,1,7') 回答2: You can also use FIELD: ORDER BY FIELD(id,

What does “ORDER BY (SELECT NULL)” mean?

拜拜、爱过 提交于 2019-11-27 12:18:46
The following SQL is from Itzik Ben-Gan that is used to generate a numbers table. What does the order by (select null) part mean? Thanks. DECLARE @number_of_numbers INT; SELECT @number_of_numbers = 100000; WITH a AS ( SELECT 1 AS i UNION ALL SELECT 1 ), b AS ( SELECT 1 AS i FROM a AS x , a AS y ), c AS ( SELECT 1 AS i FROM b AS x , b AS y ), d AS ( SELECT 1 AS i FROM c AS x , c AS y ), e AS ( SELECT 1 AS i FROM d AS x , d AS y ), f AS ( SELECT 1 AS i FROM e AS x , e AS y ), numbers AS ( SELECT TOP ( @number_of_numbers ) ROW_NUMBER() OVER ( ORDER BY ( SELECT NULL ) ) AS number FROM f ) SELECT *

SQL Server 2008: Ordering by datetime is too slow

两盒软妹~` 提交于 2019-11-27 12:11:38
问题 My table (SQL Server 2008) has 1 million+ records, when I try to order records by datetime, it takes 1 second, but when I order by ID (int), it only takes about 0.1 second. Is there any way to improve the efficiency? (I already added the datetime column to the index) 回答1: Ordering by id probably uses a clustered index scan while ordering by datetime uses either sorting or index lookup. Both these methods are more slow than a clustered index scan. If your table is clustered by id , basically

how to output a standings table on the fly from a mysql table of football [soccer] results?

[亡魂溺海] 提交于 2019-11-27 11:59:08
问题 I have been trying to find something about this topic and I can't seem to find anything, there were a few questions on here but they didn't work for my particular project. I asked a similar question about updating the table but its not going to work for what I actually want here is the list of result. -------------------------------------------------------- |id | hometeam |goalsfor|goalsagainst| awayteam | -------------------------------------------------------- | 1 |Inter Milan | 3 | 1 | FC

Alphanumeric sorting with PostgreSQL

放肆的年华 提交于 2019-11-27 11:56:27
In the database, I have various alpha-numeric strings in the following format: 10_asdaasda 100_inkskabsjd 11_kancaascjas 45_aksndsialcn 22_dsdaskjca 100_skdnascbka I want them to essentially be sorted by the number in front of the string and then the string name itself, but of course, characters are compared one by one and so the result of Order by name produces: 10_asdaasda 100_inkskabsjd 100_skdnascbka 11_kancaascjas 22_dsdaskjca 45_aksndsialcn instead of the order I'd prefer: 10_asdaasda 11_kancaascjas 22_dsdaskjca 45_aksndsialcn 100_inkskabsjd 100_skdnascbka Honestly, I would be fine if