1👍
✅
Referring to the SQL you posted, you can place that into a Django extra()
Queryset modifier:
qs = YourModel.objects.extra(select={
'max_time': '''
select * from t where (
start_time_a in (
select greatest(max(start_time_a), max(start_time_b)) from t
) or start_time_b in (
select greatest(max(start_time_a), max(start_time_b)) from t
)
)'''
})
# each YourModel object in the queryset will have an extra attribute, max_time
for obj in qs:
print obj.max_time
1👍
For getting row of greatest value from two column I found this answer and it is quite usefull
select * from t where (
start_time_a in (select greatest(max(start_time_a), max(start_time_b)) from t) or
start_time_b in (select greatest(max(start_time_a), max(start_time_b)) from t)
);
mysql greatest() function
- [Answered ]-Confirmation form step2.html Django
- [Answered ]-Django-countries and TastyPie: Get country name
0👍
MySQL solution:
If you want to identify latest dates from all records, irrespective of other column values, you can use MAX
function on the date columns.
Example:
select max( start_time_a ) as mx_start_time_a
, max( start_time_b ) as mx_start_time_b
from table_name
- [Answered ]-How to capture blank return with AJAX & jQuery?
- [Answered ]-Remove trailing data from django timesince — template equivalent
- [Answered ]-How to to override database settings in a Django TestCase
- [Answered ]-Why won't my Angular front-end pass correct $http.get parameters to Django back-end?
Source:stackexchange.com