sql-order-by

Order Results By Occurrence

半腔热情 提交于 2019-12-11 18:17:58
问题 I have the following two tables. BookmarkTag ( BookmarkID, TagID ) Tag ( TagID, Title) Currently I am selecting all the tags with the appropriate BookmarkID. The problem is I want to only select the tag once to avoid duplication in the result and also only bring back the tags the occur the most. This is my current SQL query: SELECT Tag.Title FROM `Tag` INNER JOIN BookmarkTag WHERE BookmarkTag.BookmarkID = 1 AND Tag.TagID = BookmarkTag.TagID' 回答1: You need to put the join condition in an ON

SQL ORDER BY - Why is it not working here?

橙三吉。 提交于 2019-12-11 17:54:33
问题 This is the table I have: +---------------------+--------+----------+ | date | sku | quantity | +---------------------+--------+----------+ | 2017-08-23 14:58:00 | 123333 | 2 | | 2017-08-23 14:58:00 | 123333 | 1 | | 2017-08-23 14:58:00 | 123333 | 1 | | 2017-08-23 14:58:00 | 123337 | 2 | | 2017-08-23 14:58:00 | 123335 | 1 | | 2017-08-23 14:58:00 | 123331 | 1 | | 2017-08-23 14:58:00 | 123332 | 2 | | 2017-08-23 14:58:00 | 123333 | 1 | | 2017-08-23 14:58:00 | 123334 | 1 | | 2017-08-23 14:58:00 |

MySQL multiple order by (kind of nested)

久未见 提交于 2019-12-11 16:16:29
问题 A strange problem, in that it is working but i dont understand why. I am hoping to learn why this is working. I have searched the net and stackOverflow for an answer but can not find one (i came out with this code through trial and error...) I have been trying to order by 3 columns. I wanted the results from the first 2 columns mixed together but first and then all results ordered by the third column (a date) This is the basic database table structure: HomePage = boolean, FeaturedProfile =

Greatest n-per-group With Multiple Joins

老子叫甜甜 提交于 2019-12-11 15:41:49
问题 Evening, I am trying to get an output of rows that are limited to n per group in MySQL. I can get it to work without joins, but with it I am just shy. I've pasted a dump of the relevant tables here: http://pastebin.com/6F0v1jhZ The query I am using is: SELECT title, catRef, RowNum, pCat, tog FROM ( SELECT title, catRef, @num := IF(@prevCat=catRef,@num+1,1) AS RowNum, @prevCat AS tog, @prevCat := catRef AS pCat FROM (select @prevCat:=null) AS initvars CROSS JOIN ( SELECT p.title, oi.catRef

ORDER BY is being ignored in subquery join?

依然范特西╮ 提交于 2019-12-11 14:48:47
问题 I have 3 tables: users, projects, and files. Here's the relevant columns: users: [userid](int) projects: [userid](int) [projectid](int) [modified](datetime) files: [userid](int) [projectid](int) [fileid](int) [filecreated](datetime) I'm using a query to list all projects, but I also want to include the most recent file from another table. My approach to this was using a subquery to join on. Here's what I came up with, but my problem is that it's returning the oldest file: SELECT * FROM

Get data from Select top

徘徊边缘 提交于 2019-12-11 14:31:35
问题 I already had a question answered on my previous problem Select top using SQL Server returns different output than select * I want to get select top n data from a database based on alphabetical & numbering format. The output must order by alphabet first and number after that. When I try to get all data ( select * ), I get the correct output: select nocust, share from TB_STOCK where share = ’BBCA’ and concat(share, nocust) < ‘ZZZZZZZZ’ order by case when nocust like ‘[a-z]%’ then 0 else 1 end,

SQL Server query with union and different order by to each section?

佐手、 提交于 2019-12-11 14:28:42
问题 I've searched here on the site. and there are many version of answers to this question. But couldn't find what I was looking for for this specific question : lets say that xxx,yyy,zzz has 1 column, 3 rows: 1 2 3 SELECT a FROM xxx order by a asc UNION SELECT f FROM yyy order by f desc UNION SELECT t FROM zzz order by t asc So the desired result set is: 1 2 3 3 2 1 1 2 3 There is an error of incorrect syntax near UNION. I'm aware of the order problem with union (and know how to solve it only if

Sorting and ordering by two columns

帅比萌擦擦* 提交于 2019-12-11 14:03:38
问题 I have a query which returns a result set like this: Proj | release | releaseDt 1 | 2 | 1/2/2013 1 | 1 | 4/5/2012 2 | 1 | [null] 3 | 1 | 22/2/2013 1 | 3 | [null] 3 | 2 | [null] I need to sort this by releaseDt , but I need to have all the records for that Proj together. After sorting, the result should be something like this: Proj | release | releaseDt 1 | 2 | 1/2/2013 1 | 1 | 4/5/2012 1 | 3 | [null] 3 | 1 | 22/2/2013 3 | 2 | [null] 2 | 1 | [null] How can I do this with SQL Server? 回答1: You

AngularJS: orderBy

删除回忆录丶 提交于 2019-12-11 13:44:19
问题 So i have got a ng-repeat with some names in it. What i want to do is following. The names that are outputted to the screen are ordered alphabetically. <div ng-repeat="n in names | orderBy:'name'"></div> Anne Jane John Mike Ziggy But is it possible to always show the name 'Mike' as the top name and order the rest alphabetically? Mike Anne Jane John Ziggy 回答1: You can write additional sorting function for this. For example: HTML: <div ng-repeat="n in names | orderBy:[egoSort, 'name']">{{n}}<

Preserving the order of records from subquery while using “Union distinct” construct

狂风中的少年 提交于 2019-12-11 13:39:01
问题 I want to make sure that the order of the result from subquery are preserved while using Union distinct. Please note that "union distinct" is required to filter on duplicates while doing the union. For example: select columnA1, columnA2 from tableA order by [columnA3] asc union distinct select columnB1, columnB2 from tableB When I run this, I am expecting that the records ordered from subquery ( select columnA1 , columnA2 from tableA sort by [columnA3] asc) comes in first (as returned by