sql-order-by

Group by year in date field in MySQL

早过忘川 提交于 2019-12-04 05:01:12
问题 I have a MySQL database which has a customer table. Some dummy data is: customer_id date 000001 2008-10-10 000002 2008-11-11 000003 2010-01-02 000004 2007-04-03 000005 2010-05-05 I want to run a query which will give me a result like so: year customer_count 2007 1 2008 2 2010 2 I know I need to use group by, however I am unable to wrap my head around how to group based on year value of a date field, and how to have them in an order. 回答1: Extract the year from the date and group by it select

SQL: SELECT with UNION, ORDER BY and LIMIT

自作多情 提交于 2019-12-04 03:39:11
问题 I'm getting Errors that ORDER by should come after UNION but i want these to queries ordered before combined to one and then limited to 10. SELECT * FROM (SELECT time, x, y, z FROM db WHERE time >= now ORDER by time, x UNION SELECT time, x, y, z FROM db WHERE time < now ORDER by time, x) LIMIT 10 I hope you understand, what I'm trying to do and can help me ;-) 回答1: That's not how it works, at least in MySQL (you didn't specify). The ORDER operation comes after the data is selected and all

SQLAlchemy ORDER BY FIELD()

天大地大妈咪最大 提交于 2019-12-04 03:38:43
I am trying to sort an SQLAlchemy ORM object by a field, but with a specific order of the values (which is neither ascending or descending). If I was doing this query on MySQL, it would look like; SELECT letter FROM alphabet_table WHERE letter in ('g','a','c','k') ORDER BY FIELDS( letter, 'g','a','c','k'); for the output: letter ------ g a c k For SQLAlchemy, I've been trying things along the lines of: session.query(AlphabetTable).filter(AlphabetTable.letter.in_(('g','a','c','k'))).order_by(AlphabetTable.letter.in_(('g','a','c','k'))) Which doesn't work... any ideas? It's a small one-time

Sort by Date in SQL

余生长醉 提交于 2019-12-04 03:37:20
问题 I have a resources table, one of the fields is a date field with the Data Type of date. I want to have to following output: Current month records (say May - year is not important) Then the following (again, assuming May is the current month) June Records July Records August Records September Records October Records November Records December Records January Records February Records March Records April Records Come June, June is the current month and then the order would be: July Records August

SQL CASE: Does the order of the WHEN statements matter?

天大地大妈咪最大 提交于 2019-12-04 03:36:31
问题 I am using PHP to generate a SQL query that needs to be ordered in a custom way. I am generating a CASE block with a number of WHEN statements that assign a number ranking. The WHEN statements that are included are contingent on how much information I have available for querying. For example, if I have a phone available, I will generate three WHEN statements: WHEN phone = '(202) 555-5555' AND last_name = 'Smith' and first_name = 'John' THEN 1 WHEN phone = '(202) 555-5555' AND last_name =

how to order 2 SQL Fields in asc and desc dynamically

≯℡__Kan透↙ 提交于 2019-12-04 03:33:06
问题 I want to Order a SQL Select Query where there's 2 fields that are in the order by. I then need to decide if one is Descending and the other as Ascending. How is this done I want something like: Select * from Customer Order By Date @asc_or_Desc_date, Name @asc_or_Desc_name Anyone got any ideas? I have tried this but it seems to fail SELECT Customer_ID, Name, Age FROM #Customer ORDER BY CASE WHEN @fieldSort ='Name' THEN ROW_NUMBER() over (order by Name) * case when @directionOfSort = 'A' THEN

MySQL - Select most recent date out of a set of several possible timestamps?

旧时模样 提交于 2019-12-04 02:26:38
I would like to know if this is possible using a mysql query: I have a table called updates. In the updates table I have 2 columns, id and timestamp . I have 5 rows each with the same id but with different date timestamps. An example of this timestamp would be the value: 2012-08-04 23:14:09 . I would like to select only the most recent timestamp value out of the 5 results. This could also be explained by saying that I would like to select the value that is closest to the current date and time. How could I do this? Vitor M SELECT id, timestamp FROM updates ORDER BY timestamp DESC LIMIT 1 have

SQL syntax error at or near 'union'

时间秒杀一切 提交于 2019-12-04 02:20:12
I have a small query, and a union to put another small query next to it. However, the union has a syntax error in it. Select <column1> ,<column2> From <Table1> <Some joins in there> Where <conditions> group by <column2> order by <column2> union select <column2> ,<column3> ,<column4> From <Table2> <Some more joins here> Where <conditions> group by <column2> order by <column2> This is the Error I receive ERROR: Syntax error at or near 'union' I see what was wrong. You have to place the order by at the end of the query, and only at the end. It gave me an error because it thought the query had

How to have a custom sort order for a union query in Postgres

大兔子大兔子 提交于 2019-12-04 00:55:26
With a query like this (simplified for clarity): SELECT 'East' AS name, * FROM events WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00' UNION SELECT 'West' AS name, * FROM events WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00' UNION SELECT 'Both' AS name, * FROM events WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00' I want to customise the order of the resulting rows. Something like: ORDER BY name='East', name='West', name='Both' Or ORDER BY CASE WHEN name='East' THEN 1 WHEN name='West' THEN 2 WHEN name=

Django order items by two fields, but ignoring them if they're zero

早过忘川 提交于 2019-12-04 00:50:03
I have the following model (greatly simplified for the purposes of this question): class Product(models.Model): price = models.DecimalField(max_digits=8, decimal_places=2) sale_price = models.DecimalField(max_digits=10, blank=True, null=True, decimal_places=2) For the majority of products, price will be filled but sale_price will not be. So, I can order products by price like so: Product.objects.order_by('price') Product.objects.order_by('-price') However, some products will have a sale_price, and I can't find a way to order these neatly so that the sale price interleaves with the normal price