I need to find out average age of items (groupped by various criteria, but it does not seem to be the issue). However I fail to find how to effectively create aggregation over DateTimeField using Django (on top of MySQL).
Pure Item.objects.all.aggregate(Avg('created')) seems to produce absolutely bogus values (eg. 20081988007238.133) and using Item.objects.all.extra(...) I have not found a way how to aggregate.
Of course I can manually create SQL query (something like SELECT AVG(UNIX_TIMESTAMP(created)) FROM items_item), but I'd prefer to avoid using MySQL specific code in the application.
Just for the reference, sample model I use for testing:
class Item(models.Model):
name = models.CharField(max_length = 255)
created = models.DateTimeField()
I think there is no other way as using the extra method. It should look like this then (not tested):
Item.objects.extra('avg': 'AVG(UNIX_TIMESTAMP(created))'.values('avg')
The problem is, that you have to convert the date to a UNIX timestamp before. Otherwise you cannot calculate the average. An average of string will indeed produce garbage.
Related
Alright so I'm re-writing some scripts that were provided to me and part of the script is constructing a date from two different columns, one of which could contain data such as this 3|15 and then that constructed date is being compared to today's date.
Original query section:
CASE WHEN rs.RecurrencePattern = 'Monthly' AND DAYDIFF((CreateDate(GetYear(rs.ws_wg_mig_start_date),GetMonth(rs.ws_wg_mig_start_date), toint(StrParts(rs.RecurrencePatternParms, '|',1)))),(now())) < 0 THEN GetMonth(rs.ws_wg_mig_start_date)+1
I think this is MySQL but I'm not real sure. I'm trying to recreate this in SQL Server and so far I'm using DATEFROMPARTS and stringing everything together. I'm using a NULLIF(CHARINDEX to get the value after the | but if it doesn't exist, then the whole date field is null. I need another pair of eyes on this before I go insane.
Current SQL Code:
DATEFROMPARTS(datepart(year,rs.ws_wg_mig_start_date),datepart(month,rs.ws_wg_mig_start_date),Convert(int,Right(recurrence_pattern_params, NULLIF(CHARINDEX('|', reverse(recurrence_pattern_params)),0)-1)))
I run a gym and am trying to query my database to find out how many customers who signed up to the intro class also signed up to the next level class. For starters I am issuing a simple SQL command as follows:
Select * from 'classes' where 'name' = '4 Week Intro Class'
This comes back with zero matches because I assume there is no match due to the name of the class also contains dates and times. I want to only match on the partial class name not the whole name. I've tried = and LIKE and MATCH to no avail.
Once I get this result then I want to expand it further to show me all the customers who took this class also took the next level class name. Baby steps.
I can provide more info if needed. I'm using MySQL btw.
Thanks.
Ed
For your first query, there are no matches because you are comparing two strings, and they are not equal. Presumably, you intend:
Select *
from classes
where name = '4 Week Intro Class';
Only use single quotes for string and date constants.
You can use like by doing:
Select *
from classes
where name like '%Intro%';
When you ask another question, include sample data and desired results, as well as your attempt to answer the question.
I need to get the number of records created in a specific period of time. What I'm trying to use right now looks kinda like this:
User.where('created_at between ? and ?', start, finish).group("date(created_at)").count
What i get is hash with dates as keys and numbers as values. BUT when a value is 0 for a specific day, it is not included in the hash. how can i include these dates as well? I mean MySQL, NOT Ruby language, i want it to be as fast as possible.
AR or MySQL can not create the group if there are no records in DB within the given range.
I had a similar issue and I only was able to solve it with Ruby..
User
.where('created_at between ? and ?', start, finish)
.group("date(created_at)")
.flat_map{ |a,b| [a => b.count] }
.inject(:merge)
I'd like to add a specific number of days to a date column and then compare that to the current date. Is there any way I can achieve this that is compatible with both HSQLDB (for testing) and MySQL (production)?
To make things even more complicated, I am using Hibernate.
Don't know the background of your problem, but can you do it the other way around: compare dae column with current date substracted by number of days?
I meant something like this in hql (I'm using jpql in this example, if you prefer JPA interface):
TypedQuery<SomeClass> q = getEntityManager().crateQuery(
"select s from SomeClass s where s.date = :date", SomeClass.class);
Calendar now = Calendar.getInstance();
// subtract number of days from current date, e.g. 10 days
now.add(Calendar.DAY, -10);
q.setParameter("date", now);
List<SomeClass> = q.getResultList();
This approach is database-agnostic, but of course works only for trivial cases, in some mor e complicated cases it will not work.
I have an Access database that I want to return a value based on the contents of two fields. I have detinations, and would like to return mileage that probably would be hard coded using Case.
Example: Case 1 Dest1 = "Savannah" and Dest2 ="Atlanta"
mileage = 248
I am new to VBA and seem to have trouble bringing in the field as a variable
You must provide a more detailed question here. I'm guessing that you want to hard-code standard trip-mileages in a case statement. A much better design would be to place these standard mileages between end points in a table and look them up dynamically using the two end points as a key...