sql-order-by

Order results by number of other rows with the same column value?

Order results by number of other rows with the same column value? Question: I have a table with the columns id, GENUS, SPECIES. The entries of the table many have multiple of the same GENUS but one unique SPECIES per. id, GENUS, SPECIES 0 , Homo, Sapiens 1 , Homo, Habilis 2 , Canis, Familiaris …

Total answers: 1

pyspark groupBy and orderBy use together

pyspark groupBy and orderBy use together Question: Hi there I want to achieve something like this SAS SQL: select * from flightData2015 group by DEST_COUNTRY_NAME order by count My data looks like this: This is my spark code: flightData2015.selectExpr("*").groupBy("DEST_COUNTRY_NAME").orderBy("count").show() I received this error: AttributeError: ‘GroupedData’ object has no attribute ‘orderBy’. I am new to pyspark. …

Total answers: 3

Flask SQLAlchemy order_by relationship

Flask SQLAlchemy order_by relationship Question: I’m working with a Flask application where I have a LargeGroupAttendance model that references another model called Attendee. I’m trying to request all of the LargeGroupAttendance objects that match a certain criteria, but I’m trying to sort them by a column of the Attendee model – is that even possible? …

Total answers: 2

SQLAlchemy ORDER BY FIELD()

SQLAlchemy ORDER BY FIELD() Question: 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, …

Total answers: 3

What is the default order of a list returned from a Django filter call?

What is the default order of a list returned from a Django filter call? Question: Short Question What is the default order of a list returned from a Django filter call when connected to a PostgreSQL database? Background By my own admission, I had made a poor assumption at the application layer in that the …

Total answers: 2

Sorting related items in a Django template

Sorting related items in a Django template Question: Is it possible to sort a set of related items in a DJango template? That is: this code (with HTML tags omitted for clarity): {% for event in eventsCollection %} {{ event.location }} {% for attendee in event.attendee_set.all %} {{ attendee.first_name }} {{ attendee.last_name }} {% endfor …

Total answers: 4

Django Query using .order_by() and .latest()

Django Query using .order_by() and .latest() Question: I have a model: class MyModel(models.Model): creation_date = models.DateTimeField(auto_now_add = True, editable=False) class Meta: get_latest_by = ‘creation_date’ I had a query in my view that did the following: instances = MyModel.objects.all().order_by(‘creation_date’) And then later I wanted instances.latest(), but it would not give me the correct instance, in fact …

Total answers: 4

How to get two random records with Django

How to get two random records with Django Question: How do I get two distinct random records using Django? I’ve seen questions about how to get one but I need to get two random records and they must differ. Asked By: Matt McCormick || Source Answers: Object.objects.order_by(‘?’)[:2] This would return two random-ordered records. You can …

Total answers: 7