Questions tagged [aggregate]

Aggregate refers to the process of summarizing grouped data, commonly used in Statistics.

Aggregate refers to the process of summarizing grouped data, commonly used in Statistics. Typically this involves replacing groups of data with single values (e.g. sum, mean, standard deviation, etc.). In SQL databases and data manipulation libraries such as in , this is accomplished with the use of GROUP BY and aggregate functions.

Documentation:

8256 questions
20
votes
6 answers

Can I use non-aggregate columns with group by?

You cannot (should not) put non-aggregates in the SELECT line of a GROUP BY query. I would however like access the one of the non-aggregates associated with the max. In plain english, I want a table with the oldest id of each kind. CREATE TABLE…
deft_code
  • 57,255
  • 29
  • 141
  • 224
19
votes
5 answers

C#: How to use the Enumerable.Aggregate method

Lets say I have this amputated Person class: class Person { public int Age { get; set; } public string Country { get; set; } public int SOReputation { get; set; } public TimeSpan TimeSpentOnSO { get; set; } ... } I can then…
Svish
  • 152,914
  • 173
  • 462
  • 620
19
votes
4 answers

How do I get a SUM to calculate properly with a join?

So I'm trying to count the number of parts, number of tasks, the quantity in each job and the time that it took to manufacture each job but I'm getting some funky results. If I run this: SELECT j.id, mf.special_instructions, count(distinct…
davidahines
  • 3,976
  • 16
  • 53
  • 87
19
votes
1 answer

CQRS event store aggregate vs projection

In a CQRS event store, does an "aggregate" contain a summarized view of the events or simply a reference to the boundary of those events? (group id) A projection is a view or representation of events so in the case of an aggregate representing a…
webish
  • 701
  • 2
  • 9
  • 17
19
votes
3 answers

Why do I need to explicitly specify all columns in a SQL "GROUP BY" clause - why not "GROUP BY *"?

This has always bothered me - why does the GROUP BY clause in a SQL statement require that I include all non-aggregate columns? These columns should be included by default - a kind of "GROUP BY *" - since I can't even run the query unless they're…
SqlRyan
  • 33,116
  • 33
  • 114
  • 199
19
votes
5 answers

Convert a "by" object to a data frame in R

I'm using the "by" function in R to chop up a data frame and apply a function to different parts, like this: pairwise.compare <- function(x) { Nright <- ... Nwrong <- ... Ntied <- ... return(c(Nright=Nright, Nwrong=Nwrong, Ntied=Ntied)) } Z.by <-…
Lorin Hochstein
  • 57,372
  • 31
  • 105
  • 141
19
votes
7 answers

SSRS Expression: The value expression for textbox has scope parameter that is invalid for aggregate

i'm recieving the following error: Error 1 [rsInvalidAggregateScope] The Value expression for the text box ‘Textbox2’ has a scope parameter that is not valid for an aggregate function. The scope parameter must be set to a string constant that…
18
votes
4 answers

Faster ways to calculate frequencies and cast from long to wide

I am trying to obtain counts of each combination of levels of two variables, "week" and "id". I'd like the result to have "id" as rows, and "week" as columns, and the counts as the values. Example of what I've tried so far (tried a bunch of other…
user592419
  • 5,103
  • 9
  • 42
  • 67
18
votes
1 answer

Concatenate JSON arrays in PostgreSQL aggregate

I have a table that contains a field of JSON type, containing arrays of data: Column | Type -------------------+--------- id | integer user_id | uuid changes | jsonb exercise_entry_id |…
Dave Vogt
  • 18,600
  • 7
  • 42
  • 54
18
votes
3 answers

How do you handle associations between aggregates in DDD?

I'm still wrapping my head around DDD, and one of the stumbling blocks I've encountered is in how to handle associations between separate aggregates. Say I've got one aggregate encapsulating Customers and another encapsulating Shipments. For…
Erik Forbes
  • 35,357
  • 27
  • 98
  • 122
18
votes
5 answers

Aggregating hourly data into daily aggregates

I have an hourly weather data in the following format: Date,DBT 01/01/2000 01:00,30 01/01/2000 02:00,31 01/01/2000 03:00,33 ... ... 12/31/2000 23:00,25 What I need is a daily aggregate of max, min, ave like…
ery
  • 992
  • 3
  • 14
  • 25
18
votes
3 answers

Python Pandas: Passing arguments to a function in agg()

I am trying to reduce data in a pandas dataframe by using different kind of functions and argument values. However, I did not manage to change the default arguments in the aggregation functions. Here is an example: >>> df = pd.DataFrame({'x':…
Tanguy
  • 3,124
  • 4
  • 21
  • 29
18
votes
2 answers

How do I concatenate strings in Entity Framework Query?

How do I concatenate strings in Entity Framework 4 I have a data from a column and I want to save as a string a comma separated string like "value1, value2, value3" Is there a method or an operator do do this in EF4? Example: lets say that I have…
18
votes
5 answers

Run a custom function on a data frame in R, by group

Having some trouble getting a custom function to loop over a group in a data frame. Here is some sample data: set.seed(42) tm <- as.numeric(c("1", "2", "3", "3", "2", "1", "2", "3", "1", "1")) d <- as.numeric(sample(0:2, size = 10, replace =…
BillPetti
  • 511
  • 2
  • 7
  • 14
18
votes
4 answers

How to do Linq aggregates when there might be an empty set?

I have a Linq collection of Things, where Thing has an Amount (decimal) property. I'm trying to do an aggregate on this for a certain subset of Things: var total = myThings.Sum(t => t.Amount); and that works nicely. But then I added a condition…
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387