sql-order-by

Mysql: order a table using 2 columns in Descending order

泪湿孤枕 提交于 2019-12-12 06:34:36
问题 i'm a bit stomped.. I'm trying to achieve a fairly simple logic.. I have a "user_sessions" table with log_in and log_out (int, epoch time stamps) userID | log_in | log_out trying to show the RECENT activities, I need to list all the sessions in DESCENDING ORDER based on the LATEST ACTIVITY, and this could either be log_in or log_out.. and this is where i'm stomped. I tried different ORDER and SORT methods... I'm just not getting it. 回答1: SELECT * FROM user_sessions ORDER BY GREATEST(log_in,

Give priority to data from Specific Datasource

独自空忆成欢 提交于 2019-12-12 06:20:09
问题 I have data as below ID Name Description DataSource Year 1 Apple Sweet & Tasty Source_A 2016 1 Apple Red and Sweet & Tasty Source_B 2015 2 Apple Delicious Source_A 2016 2 Apple Delicious and Red Source_C 2015 3 Apple Source_C 2013 3 Apple Green and Large Source_B 2016 In my case, I would like to give priority to source_B because it is more reliable. So if there is a data from Soure_B, I would like to display that row for the specific ID and ignore others. If data from source_B is not present,

rails complex order_by with argument

给你一囗甜甜゛ 提交于 2019-12-12 05:25:10
问题 I have a rails app. I would like to display user profiles ordered by the number of the common tasks they have with the current user. Every task has one assigner and one executor. The number should include both the executed_tasks and assigned_tasks for the same user. So for example if current_user assigned 5 tasks to User4 and User4 assigned 3 tasks to current_user then this number would be 8. My main problem is that I don't know how to use the given user as arg for the count. Should I do in

MySQL using different index depending on limit value with ORDER BY query

。_饼干妹妹 提交于 2019-12-12 04:56:45
问题 This is weird to me: One table 'ACTIVITIES' with one index on ACTIVITY_DATE. The exact same query with different LIMIT value results in different execution plan. Here it is: mysql> explain select * from ACTIVITIES order by ACTIVITY_DATE desc limit 20 -> ; +----+-------------+------------+-------+---------------+-------------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+-------+-------

MS ACCESS VBA ORDER BY CASE When

那年仲夏 提交于 2019-12-12 03:47:00
问题 I a lot of orders in my Factory . Our regular time to finish our job is 10 days but sometimes we promise to the customer that it will be shorter (lets assume 5 days). I tried to build an SQL that order by the minimum Date of the two columns : SELECT id, Status, DeliveryDate, PromiseDate FROM CustomerOrderT WHERE Status > 2 and Status <7 ORDER BY CASE WHEN DeliveryDate > PromiseDate then PomiseDate ELSE DeliveryDate END; But i get a problem message from ms access. maybe write it correctly The

How can I group on the SAME table, order by date and limit the number of rows returned?

对着背影说爱祢 提交于 2019-12-12 03:28:52
问题 I would like to return a set that is grouped by ID_F, limited in count within ID_F and sorted by date within ID_F Here is my database setup: CREATE TABLE [dbo].[U]( [ID] [uniqueidentifier] NOT NULL, [Name] [nvarchar](max) NULL, CONSTRAINT [PK_U] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[F]( [ID] [uniqueidentifier] NOT NULL, [Name]

MySQL order by category and subcategory

那年仲夏 提交于 2019-12-12 03:09:13
问题 I am trying to sort some Nagios alerts in MySQL. I would like column 2 to be grouped together as it is now. All alerts are grouped. The problem is that i would like those groups to be in descending order. For example, "All Hosts | All Events | 65 |" should be at the top, next would be "All Hosts | memory-usage-UNX | 25", this would be followed by the 25 memory-usage-UNX alerts. Next would be "All Hosts | CPU-Load-UNX | 9" and then the rest of the CPU-Load-UNX alerts in descending order based

Order query results by two key/value pairs, where the fields for the key/value pairs are the same

 ̄綄美尐妖づ 提交于 2019-12-12 03:08:30
问题 I have a query to select posts ( wp_posts table ) where either 1 key/value pair or another is present on the a second table ( wp_postmeta ). Esentially, this works, but I then need to order the results by the first key/pair value ( department_head ), and then the second ( staff_surname ). The problem is, becuause of the way the results are grabbed by the query, there is no department_head key to use to order the results by. I'm guessing that this is because the query is finding the staff

Query returns a different result every time it is run

丶灬走出姿态 提交于 2019-12-12 02:48:10
问题 This query always returns the same amount of rows but, in a different order, every time. Why does this happen? I have more filters to add but I can't get past this step. BEGIN DECLARE @lastStatus Varchar(10) SELECT [Job].[Job], [Job].[Part_Number], [Job].[Rev], [Job_Operation].[Description], [Job].[Customer_PO], [Job].[Customer_PO_LN], [Delivery].[Promised_Date], [Job_Operation].[Operation_Service], [Job].[Note_Text], [Job_Operation].[Status], [Job_Operation].[Sequence] INTO [#tmpTbl] FROM

MySQL ORDER BY two fields condition

陌路散爱 提交于 2019-12-12 02:27:29
问题 I have a problem with creating select query ordered by following logic: SELECT * FROM Products WHERE 1 ORDER BY Stock > 0, Price DESC Table sample: +---------+-------+-------+ | Product | Price | Stock | +---------+-------+-------+ | Car | 3500 | 30 | | Boat | 7500 | 6 | | Bike | 150 | 220 | | Plane | 55000 | 0 | +---------+-------+-------+ The desired result is, that the table will be ordered by price if the stock value is greater than 0. So the result should be: +---------+-------+-------+