I have movies, each movie has comments added to it. I want to create a function that will rank my Movies based on a number of comments using dense ranking. This is what I got so far - the problem is, every movie always has rank 1.
commented_movies = Movie.objects.annotate(comment_count=Count('comments', distinct=True)).annotate(
rank=Window(expression=DenseRank()
, order_by=F('comment_count').desc()
, partition_by=[F('id')]))
I guess the problem might be in partition_by, but I have no idea how to solve it. id is a primary key of the Movie.
Okay, that was quick. I've just figured out partition_by is not necessary - removed it and it works:
commented_movies = Movie.objects.annotate(comment_count=Count('comments', distinct=True)).annotate(
rank=Window(expression=DenseRank()
, order_by=F('comment_count').desc()
, partition_by=[F('id')]))
Related
Hello, people.
As you guys can see on the table above, I´have more than one "seller" per "order_num" and it’s ok. But I need to SUM the "sku_qtd" without repeating the items from the order. Right now, SUM is returning 6 instead of 3.
Pls help me, I have been looking for an answer for hours.
If you cannot modify the data model to properly represent the relationship between order_num and seller (many-to-one or many-to-many), this DAX formula should do it:
sum_qtd = SUMX(DISTINCT('table'[order_num]),FIRSTNONBLANK('table'[sku_qtd],0))
I have a dataset of movies with the amount of views per day, I want to sum total views per movie and I am doing the following:
SUM(IF [Title] = [Title]
THEN [Views]
END)
But is not giving me the right numbers, can anyone please help me?
You don't need to make a calculated field every time you want to see values in Tableau - that's part of the beauty of it.
Assuming that the Views field is a numeric measure by default, and the Movie field is a text dimension by default, then just drag Movie to one shelf (such as Rows) and Views to another (such as Columns).
I am a newbie with Django trying to create a dashboard application reporting on some key milestone dates. I want to be able to track how the key dates are changing. For example:If the kick off date has been changed 5 times I want to be able to report 1. the first date entered, 2. Current date, 3. The date before the last update.
Thank you
Your question is not clear. But for the logic you have asked one thing we can do is to make a model in which the edited dates and user will be fields. Use user as foreign key of your User model. I will just give an example model.
class Dates(models.Model):
event = models.ForeignKey(Event)
date = models.DateField()
This is a very basic method which i am saying. This is a bit complex and you will have to check if the field has changed five times and all.
For a better answer please make the question clear.
I'm trying to order a list of items in django by the number of comments they have. However, there seems to be an issue in that the Count function doesn't take into account the fact that django comments also uses a content_type_id to discern between comments for different objects!
This gives me a slight problem in that the comment counts for all objects are wrong using the standard methods; is there a 'nice' fix or do I need to drop back to raw sql?
Code to try and ge the correct ordering:
app_list = App.objects.filter(published=True)
.annotate(num_comments=Count('comments'))
.order_by('-num_comments')
Sample output from the query (note no mention of the content type id):
SELECT "apps_app"."id", "apps_app"."name",
"apps_app"."description","apps_app"."author_name", "apps_app"."site_url",
"apps_app"."source_url", "apps_app"."date_added", "apps_app"."date_modified",
"apps_app"."published", "apps_app"."published_email_sent", "apps_app"."created_by_id",
"apps_app"."rating_votes", "apps_app"."rating_score", COUNT("django_comments"."id") AS
"num_comments" FROM "apps_app" LEFT OUTER JOIN "django_comments" ON ("apps_app"."id" =
"django_comments"."object_pk") WHERE "apps_app"."published" = 1 GROUP BY
"apps_app"."id", "apps_app"."name", "apps_app"."description", "apps_app"."author_name",
"apps_app"."site_url", "apps_app"."source_url", "apps_app"."date_added",
"apps_app"."date_modified", "apps_app"."published", "apps_app"."published_email_sent",
"apps_app"."created_by_id", "apps_app"."rating_votes", "apps_app"."rating_score" ORDER
BY num_comments DESC LIMIT 4
Think I found the answer: Django Snippet
I'm working on a Django timesheet application and am having trouble figuring out how to include aggregate sums that equal zero. If I do something like:
entries = TimeEntry.objects.all().values("user__username").annotate(Sum("hours"))
I get all users that have time entries and their sums.
[{'username': u'bob' 'hours__sum':49}, {'username': u'jane' 'hours__sum':10}]
When I filter that by a given day:
filtered_entries = entries.filter(date="2010-05-17")
Anyone who didn't enter time for that day is excluded. Is there a way to include those users who's sums are 0?
Thanks
Maybe you could try the relationship the other way round - start with users, and link to entries:
User.objects.all().values("username").annotate(Sum("timeentry__hours"))
Does that work?