sql-order-by

T-Sql - Order By on Alphanumeric

☆樱花仙子☆ 提交于 2019-12-06 05:44:07
i have a list of alphanumeric tokens, say '1a', '1b', '02', '03', '10', '11', etc... Now, what's the best way to do an order by on this list of tokens? I am getting '1a', '1b', '10', '11', '02', '03', but i need it to be '1a', '1b', '02', '03', '10', '11' UPDATE ok, i am doing this after the suggestion but it's not working. declare @tokens table(token varchar(20)); insert into @tokens select '1a' select '1b' select '02' select '10' select * from @tokens order by case when ISNUMERIC(token) = 1 then right('0000000000'+token+'0',10) else right('0000000000'+token,10) end I am getting the response

PostgreSQL: order by column, with specific NON-NULL value LAST

孤者浪人 提交于 2019-12-06 04:59:23
When I discovered NULLS LAST , I kinda hoped it could be generalised to 'X LAST' in a CASE statement in the ORDER BY portion of a query. Not so, it would seem. I'm trying to sort a table by two columns (easy), but get the output in a specific order (easy), with one specific value of one column to appear last (got it done... ugly). Let's say that the columns are zone and status (don't blame me for naming a column zone - I didn't name them). status only takes 2 values ('U' and 'S'), whereas zone can take any of about 100 values. One subset of zone 's values is (in pseudo-regexp) IN[0-7]Z , and

Building a snapshot table from audit records

这一生的挚爱 提交于 2019-12-06 04:08:33
I have a Customer table with the following structure. CustomerId Name Address Phone 1 Joe 123 Main NULL I also have an Audit table that tracks changes to the Customer table. Id Entity EntityId Field OldValue NewValue Type AuditDate 1 Customer 1 Name NULL Joe Add 2016-01-01 2 Customer 1 Phone NULL 567-54-3332 Add 2016-01-01 3 Customer 1 Address NULL 456 Centre Add 2016-01-01 4 Customer 1 Address 456 Centre 123 Main Edit 2016-01-02 5 Customer 1 Phone 567-54-3332 843-43-1230 Edit 2016-01-03 6 Customer 1 Phone 843-43-1230 NULL Delete 2016-01-04 I have a CustomerHistory reporting table that will be

MySQL ORDER BY AVG() DESC not working when certain columns are included

好久不见. 提交于 2019-12-06 04:08:07
I'm doing a query to return all the rows in table1, along with their average rating from table2: SELECT `table1`.`description`, AVG( `table2`.`rating` ) AS avg_rating FROM `table1` LEFT JOIN `table2` ON ( `table2`.`botid` = `table1`.`id` ) GROUP BY `table1`.`id` ORDER BY avg_rating DESC The problem is that even though I specify DESC , the results are being returned ASC : +-------------+------------+ | description | avg_rating | +-------------+------------+ | test2 | 1.0000 | | test3 | 3.0000 | | test4 | 3.0000 | | saasdf | 4.0000 | +-------------+------------+ Why isn't MySQL honoring ORDER BY

mysql custom order by with mixed data types

萝らか妹 提交于 2019-12-06 03:59:33
问题 In the following mysql query I'm using a custom order by statement so I can display various sizes in a specific order instead of alphabetical: select distinct size from product p left join productsizes ps on p.productcode = ps.size_prodcode order by field(size, 'XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL') In cases where some products also have numeric sizes, how do I write the order by so that it places the numeric sizes in an ascending order along with the custom order? An example of the

T-SQL ORDER BY number and letters mixed in string

浪子不回头ぞ 提交于 2019-12-06 03:34:46
I have a range of unique strings which contain numbers and sometimes numbers and a letter, a sample of which reads: 1X 2X 2Y 12X 20 21 The number/s always precede the letter. What is the ORDER BY (T-SQL) clause solution to produce a list which would give me the order as demonstrated above? I tried using LEN( fieldName ), fieldname - which would work but for the 20 and 21. I have tried expressing the strings as an integer but the CAST fails in the conversion process. I'm stealing my details from Here . declare @t table(s varchar(25)) insert @t select '122345684XT' union select '23339034300-XT'

Order by clause execution in SQL

陌路散爱 提交于 2019-12-06 02:34:05
问题 This question isn't about order of executions. It's about just the ORDER BY. In standard execution is: FROM WHERE GROUP BY HAVING SELECT ORDER BY TOP EDIT: This question has been more or less the issue of " Does SQL Server apply short circuit evaluation when executing ORDER BY expressions? " The answer is SOMETIMES! I just haven't found a reasonable reason as to why. See Edit #4. Now suppose I have a statement like this: DECLARE @dt18YearsAgo AS DATETIME = DATEADD(YEAR,-18,GETDATE()); SELECT

Give priority to ORDER BY over a GROUP BY in MySQL without subquery

本小妞迷上赌 提交于 2019-12-06 02:15:51
问题 I have the following query which does what I want, but I suspect it is possible to do this without a subquery: SELECT * FROM (SELECT * FROM 'versions' ORDER BY 'ID' DESC) AS X GROUP BY 'program' What I need is to group by program, but returning the results for the objects in versions with the highest value of "ID". In my past experience, a query like this should work in MySQL, but for some reason, it's not: SELECT * FROM 'versions' GROUP BY 'program' ORDER BY MAX('ID') DESC What I want to do

max(), group by and order by

て烟熏妆下的殇ゞ 提交于 2019-12-06 01:59:38
问题 I have following SQL statement. SELECT t.client_id,max(t.points) AS "max" FROM sessions GROUP BY t.client_id; It simply lists client id's with maximum amount of points they've achieved. Now I want to sort the results by max(t.points). Normally I would use ORDER BY, but I have no idea how to use it with groups. I know using value from SELECT list is prohibited in following clauses, so adding ORDER BY max at the end of query won't work. How can I sort those results after grouping, then? Best

How to improve order by performance with joins in mysql

核能气质少年 提交于 2019-12-06 00:33:23
问题 I am working on a social network tracking application. Even joins works fine with proper indexing. But when I add the order by clause the total query takes 100 times longer time to execute. The following query I used to get the twitter_users without order by clause. SELECT DISTINCT `tracked_twitter`.id FROM tracked_twitter INNER JOIN `twitter_content` ON `tracked_twitter`.`id` = `twitter_content`.`tracked_twitter_id` INNER JOIN `tracker_twitter_content` ON `twitter_content`.`id` = `tracker