sql-order-by

SQLITE DB sort query by string formatted date field

我是研究僧i 提交于 2019-12-02 03:06:46
问题 I have a SQLITE DB with a string field which contains a date in the following format: "01.01.2012". What I want is to sort the by this string formatted date in a query. I tried this without success: SELECT * FROM fahrer, fahrzeuge, nutzungsarten, fahrtenbuch WHERE fahrtenbuch.nutzungsart_id = nutzungsarten._id AND fahrtenbuch.fahrzeuge_id = fahrzeuge._id AND fahrtenbuch.fahrer_id = fahrer._id ORDER BY strftime ('%d.%m.%Y', fahrtenbuch.startdatum) What I am doing wrong? 回答1: The values in the

PHP: do an ORDER BY using external data?

五迷三道 提交于 2019-12-02 02:22:18
问题 Ahoy all! Long story short with this one if you don't mind lending a hand to this novice PHPer. :) I have a database field called "Categories" that has this stored: Fruit, People, Place, Animals, Landscape I also have a separate table in the DB that has items with these category names in the fields for each item. Right now, the script (i am trying to fork it a bit) uses: SELECT DISTINCT(type), type FROM the_categories ORDER BY type ASC in order to display a list of all categories available.

Sort data (order by) before group by in mysql

非 Y 不嫁゛ 提交于 2019-12-02 02:18:51
问题 I want to group below data from sub_prd_id . but before that i need order data from max created_at . I wrote query as below. select * FROM prd_data group by sub_prd_id order by created_at desc But this query not returned what i need. for example, according to below data, after query executes, result should be as below, id name sub_prd_id created_at 4 Grape 10 2013-04-28 03:11:55 6 Banana 11 2013-04-28 03:23:14 7 Pineapple 12 2013-04-28 03:23:44 Here is Table Structure with data. id name sub

OrderBy is not translated into SQL when passing a selector function

邮差的信 提交于 2019-12-02 01:36:20
问题 When I execute: var t = db.Table1.OrderBy(x => x.Name).ToList(); In SQL profiler, this is the translated SQL: SELECT [Extent1].[ID] AS [ID], [Extent1].[Name] AS [Name] FROM [dbo].[Table1] AS [Extent1] ORDER BY [Extent1].[Name] ASC Which is correct. However, if I pass a selector function to OrderBy: Func<Table1, string> f = x => x.Name; var t = db.Table1.OrderBy(f).ToList(); The translated SQL is: SELECT [Extent1].[ID] AS [ID], [Extent1].[Name] AS [Name] FROM [dbo].[Table1] AS [Extent1] The

How to provide the sort order in a variable in sqlalchemy?

只愿长相守 提交于 2019-12-02 01:25:39
Right now what I am doing is as : if order_type == 'desc': result = session.\ query(Customer).\ order_by(desc(getattr(Customer, sorting_column_name))).\ all() else: result = session.\ query(Customer).\ order_by(asc(getattr(Customer, sorting_column_name))).\ all() Is there any way to call order_by just once and use sorting order provided in order_type as a variable to decide whether to sort asc or desc ? Martijn Pieters asc and desc are just objects, pick one based on the ordering you want: direction = desc if order_type == 'desc' else asc result = session.\ query(Customer).\ order_by(direction

SQL: case statement in order by clause

让人想犯罪 __ 提交于 2019-12-02 00:52:11
问题 http://msdn.microsoft.com/en-us/library/ms181765.aspx I see the sql below from above link: SELECT BusinessEntityID, SalariedFlag FROM HumanResources.Employee ORDER BY CASE SalariedFlag WHEN 1 THEN BusinessEntityID END DESC ,CASE WHEN SalariedFlag = 0 THEN BusinessEntityID END; GO Here is one result I get: BusinessEntityID,SalariedFlag 7,1 5,1 3,1 1,1 2,0 4,0 6,0 8,0 Could anyone explain why the records with same salariedFlag are next to each other and why salariedFlag=1 chunk is above the

Optimizing ORDER BY

孤街醉人 提交于 2019-12-02 00:24:30
问题 I am trying to optimize this query that sorts posts by reputation field (1st) and then id field (2nd). Without 1st field query takes ~0.250sec, but with it takes up to ~2.500sec (means 10x times slower, terrible). Any suggestion? SELECT -- everything is ok here FROM posts AS p ORDER BY -- 1st: sort by reputation if exists (1 reputation = 1 day) (CASE WHEN p.created_at >= unix_timestamp(now() - INTERVAL p.reputation DAY) THEN +p.reputation ELSE NULL END) DESC, -- also used 0 instead of NULL --

Sort data (order by) before group by in mysql

放肆的年华 提交于 2019-12-02 00:09:55
I want to group below data from sub_prd_id . but before that i need order data from max created_at . I wrote query as below. select * FROM prd_data group by sub_prd_id order by created_at desc But this query not returned what i need. for example, according to below data, after query executes, result should be as below, id name sub_prd_id created_at 4 Grape 10 2013-04-28 03:11:55 6 Banana 11 2013-04-28 03:23:14 7 Pineapple 12 2013-04-28 03:23:44 Here is Table Structure with data. id name sub_prd_id created_at 2 Apple 10 2013-04-28 03:04:51 3 Orange 10 2013-04-28 03:08:19 4 Grape 10 2013-04-28

Optimizing ORDER BY

点点圈 提交于 2019-12-01 22:34:47
I am trying to optimize this query that sorts posts by reputation field (1st) and then id field (2nd). Without 1st field query takes ~0.250sec, but with it takes up to ~2.500sec (means 10x times slower, terrible). Any suggestion? SELECT -- everything is ok here FROM posts AS p ORDER BY -- 1st: sort by reputation if exists (1 reputation = 1 day) (CASE WHEN p.created_at >= unix_timestamp(now() - INTERVAL p.reputation DAY) THEN +p.reputation ELSE NULL END) DESC, -- also used 0 instead of NULL -- 2nd: sort by id dec p.id DESC WHERE p.status = 'published' -- the only thing for filter LIMIT 0,10 --

SQL Case Order By specific order

大城市里の小女人 提交于 2019-12-01 21:22:38
Ok, I asked something similar before, but I've researched it and haven't found this specifically. I have a table that I need sorted on fields OptionName(NVarChar) and IsActive(BIT). I need the results to be in the following order for a DDL: Option A Option B Option C Options that are Active, by OptionName ASC Option D Options that are Inactive, by OptionName ASC So far I have ORDER BY CASE WHEN PortalName = 'Company, Inc' THEN 0 ELSE 1 END, CASE WHEN PortalName = 'Setup' THEN 1 ELSE 2 END, CASE WHEN PortalName = 'Daily Routine' THEN 2 ELSE 3 END, CASE WHEN IsActive = 1 THEN 3 ELSE 4 END, CASE