group-by

Correct indexing by Join-Where-Group By select queries avoiding Using temporary; Using filesort

筅森魡賤 提交于 2019-12-22 09:48:15
问题 I've searched a lot about a solution of the described below case, but unfortunately I haven't found a similar case. I have the following scenario: (as a new user the site rejected my picture, but I can send it via mail. Below is a textual representation of it) Table 1 "swap_plan" Table 2 "cell" ClusterName | SiteID SiteID | Cell | Time | Counter ----------------------- --------------------------------------------- Cluster A | SiteID A1 SiteID A1 | Cell A1-1 | day1 | 5 Cluster A | SiteID A2

Django annotate and values(): extra field in 'group by' causes unexpected results

ⅰ亾dé卋堺 提交于 2019-12-22 09:37:52
问题 I must be missing something obvious, as the behavior is not as expected for this simple requirement. Here is my model class: class Encounter(models.Model): activity_type = models.CharField(max_length=2, choices=(('ip','ip'), ('op','op'), ('ae', 'ae'))) cost = models.DecimalField(max_digits=8, decimal_places=2) I want to find the total cost for each activity type. My query is: >>> Encounter.objects.values('activity_type').annotate(Sum('cost')) Which yields: >>> [{'cost__sum': Decimal("140.00")

Apply COUNT function on a subgroup of groups

孤者浪人 提交于 2019-12-22 09:18:54
问题 I made up this weird example trying to illustrate what I want to do (it's kind of stupid, but bear with me): Consider the following table: EMPLOYEES married , certified and religious are just boolean fields (in case of Oracle, they are of type NUMBER(1,0)). I need to come up with SQL that displays for each hire_year, count of married, certified and religious employees within the following salary categories: A SALARY > 2000 B SALARY BETWEEN 1000 AND 2000 C SALARY < 1000 Based on the above

Active Record LIMIT within GROUP_BY

二次信任 提交于 2019-12-22 08:46:42
问题 SCENARIO I have a table full of posts with a users table. I want to be able to fetch all the posts and group them by users but I want to set a limit of say 10 per user. class Post < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :posts end # I thought this might work but it just grabs the first 10 posts and groups them Post.find(:all, :limit=>10).group_by(&:user) Any thoughts? Do I have to write custom SQL for or can Active Record do this? 回答1: Something like

How to get percentage of counts of a column after groupby in Pandas

╄→尐↘猪︶ㄣ 提交于 2019-12-22 08:08:23
问题 I'm trying to get the distribution of grades for each rank for names in a list of data. However, I can't figure out how to get the proportion/percentage of each grade count over its rank group. Here's an example: df.head() name rank grade Bob 1 A Bob 1 A Bob 1 B Bob 1 C Bob 2 B Bob 3 C Joe 1 C Joe 2 B Joe 2 B Joe 3 A Joe 3 B Joe 3 B I use grade_count = df.groupby(['name', 'rank', 'grade']).['grade'].size()) to give me the count of each grade within its (name,rank) group: name rank grade Bob 1

Select, group and sum results from database

安稳与你 提交于 2019-12-22 07:21:04
问题 I have a database with some fields I'd like to sum. But that's not the big problem, I want to group those fields by the month they were created. ActiveRecord automaticaly created a field named "created_at". So my question; how can I group the result by month, then sum the fields for each month? Updated with code @hours = Hour.all(:conditions => "user_id = "+ @user.id.to_s, :group => "strftime('%m', created_at)", :order => 'created_at DESC') This is the code I have now. Managed to group by

Applying cumulative mean function to a grouped object

允我心安 提交于 2019-12-22 06:43:18
问题 I have a DataFrame df where each record represents a soccer game. Teams will appear more than once. I need to compute some sort of a rolling mean for each team scores(well, not exactly the rolling mean to the letter). date home away score_h score_a 166 2013-09-01 Fulham Chelsea 0 0 167 2013-09-03 Arsenal Everton 0 2 164 2013-09-05 Arsenal Swansea 5 1 165 2013-09-06 Fulham Norwich 0 1 163 2013-09-18 Arsenal Swansea 0 0 What I need to calculate, is the mean score for each team (home and away).

group by day/month and take average of rating on that day/month in mongo

别说谁变了你拦得住时间么 提交于 2019-12-22 05:21:59
问题 I have this data in mongodb: [ { "rating": 4, "ceatedAt": ISODate("2016-08-08T15:32:41.262+0000") }, { "rating": 3, "createdAt": ISODate("2016-08-08T15:32:41.262+0000") }, { "rating": 3, "ceatedAt": ISODate("2016-07-01T15:32:41.262+0000") }, { "rating": 5, "createdAt": ISODate("2016-07-01T15:32:41.262+0000") } ] I need to take the average of rating for that particular day/month depending upon the filter criteria. if the filter range for date is less than a month then it will be grouped basis

Group by date without time

主宰稳场 提交于 2019-12-22 05:08:16
问题 I was wondering whether there was a way to group dates that are 2014-01-26 05:39:29.000 and 2014-01-26 07:45:31.000 into one day when counting them. I currently have the following code that just groups them by their unique datetime. SELECT ETK_ExpirationDateTime, COUNT(*) as TotalRows FROM History_Action WHERE [State] = 4 GROUP BY ETK_ExpirationDateTime ORDER BY ETK_ExpirationDateTime Is there a cast or something I can do to make those 2 dates above appear as one row with a total sum? 回答1:

Filling in missing dates using a linq group by date query

血红的双手。 提交于 2019-12-22 04:15:08
问题 I have a Linq query that basically counts how many entries were created on a particular day, which is done by grouping by year, month, day . The problem is that because some days won't have any entries I need to back fill those missing "calendar days" with an entry of 0 count. My guess is that this can probably be done with a Union or something, or maybe even some simple for loop to process the records after the query. Here is the query: from l in context.LoginToken where l.CreatedOn >= start