Questions tagged [aggregate-functions]

Aggregate functions are a subset of SQL functions that compute a single value from multiple input rows, mostly used in `SELECT` queries with a `GROUP BY` clause. Practically all modern RDBMS feature aggregate functions. Typical examples include `COUNT()`, `SUM()`, `MIN()`, `MAX()`, and `AVG()`.

Aggregate functions are a subset of SQL functions that compute a single value from multiple input rows, mostly used in SELECT queries with a GROUP BY clause. Practically all modern RDBMS feature aggregate functions.

Typical examples include COUNT(), SUM(), MIN(), MAX(), and AVG().

5778 questions
2
votes
1 answer

Aggregated product generation on runtime for SQL Server 2008 R2

I have a large amount of data. I need to implement a product aggregation on each value. Let me explain with example to make it clear. This is a sample data- /*SampleTable*/ |ID|Date |Value | | 1|201401|25 | | 1|201402|-30 | | 1|201403|-15 …
2
votes
3 answers

MySQL query problem on group by and max

My table structure is (id,cluster,qid,priority). I'm trying to figure out how I can display the maximum value of priority for each cluster. Say cluster 1 has priorities 100, 102, 105. I want to display the record containing 105. Please help.
1s2a3n4j5e6e7v
  • 1,243
  • 3
  • 15
  • 29
2
votes
3 answers

R data.table pick 2 continue rows to create new table

I want to create new table which is pair of 2 continue rows from my old table. A B 1 a 2 b 3 c 4 d 5 e I want to create new table as below. A1 B1 A2 B2 1 a 2 b 2 b 3 c 3 c 4 d 4 d 5 e I want to find simple solution for this case.
Minh Ha Pham
  • 2,566
  • 2
  • 28
  • 43
2
votes
1 answer

Aggregrate query with related models, not just related ID values

Say I have three models: from django.db import models class X(models.Model): y = models.ForeignKey(Y, on_delete=models.PROTECT) z = models.ForeignKey(Z, on_delete=models.PROTECT) a = models.DecimalField(...) b =…
pleasedesktop
  • 1,395
  • 3
  • 14
  • 25
2
votes
2 answers

How to calculate MAX(SUM()) in postgresql in particular to query below

I am trying to get MAX(SUM()) in PostgreSQL, which is not possible directly in PostgreSQL. I am trying to get the result by any method, but unable to get the desired result. My table is like this - CREATE TABLE user_transactions ( id integer…
vrOom
  • 23
  • 1
  • 5
2
votes
2 answers

Get separate count for each condition within group

I am trying to get a view of table information from and Oracle 10g table that lists the counts of specific values of a column in their own columns with each row being the group value. for example: The first select is : SELECT processed_by,…
bryce
  • 23
  • 2
2
votes
2 answers

Find duplicated values on array column

I have a table with a array column like this: my_table id array -- ----------- 1 {1, 3, 4, 5} 2 {19,2, 4, 9} 3 {23,46, 87, 6} 4 {199,24, 93, 6} And i want as result what and where is the repeated values, like this: value_repeated …
2
votes
4 answers

Count rows for each unique combination of columns in SQL

I would like to return a set of unique records from a table based on two columns along with the most recent posting time and a total count of the number of times the combination of those two columns has appeared before (in time) the record of their…
Alan Wayne
  • 5,122
  • 10
  • 52
  • 95
2
votes
2 answers

Finding MAX of Column in Oracle SQL

I have the following table structure: Table A (A1, ...) where A1 is PK Table B (B1, B2, ...) where A.A1 = B.B1 and B2 is PK Table C (C1, C2, C3, ...) where C.C1 = B.B2 and C1 is PK Given A.A1 and C.C2 are provided, I need to obtain the MAX() of…
Ulvon
  • 91
  • 1
  • 6
2
votes
2 answers

Executing queries dynamically in PL/pgSQL

I have found solutions (I think) to the problem I'm about to ask for on Oracle and SQL Server, but can't seem to translate this into a Postgres solution. I am using Postgres 9.3.6. The idea is to be able to generate "metadata" about the table…
2
votes
1 answer

How is an index used in an query with aggregation?

Given a query like SELECT franchise, MAX(worth) FROM figurines GROUP BY franchise what sort of index would speed this query up, and how would the database use that index? If more detail is required, assume that the column franchise has a relatively…
Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
2
votes
2 answers

Generating unique combinations in postgres of size two from an array

I have a followup to this question in which I generated arrays of this type of table: val | fkey | num ------------------ 1 | 1 | 10 1 | 2 | 9 1 | 3 | 8 2 | 3 | 1 In which the resulting returned row would like…
Clicquot the Dog
  • 530
  • 2
  • 6
  • 19
2
votes
3 answers

Combine the most recent entries from a number of tables

I have a master table with a number of IDs in it: ID ... 0 ... 1 ... And multiple tables (say vtbl1, vtbl2, vtbl3) with a foreign key to master, a timestamp and a value: ID Timestamp Value 0 01/01/01.. 2 1 01/01/02.. 7 0 …
Hector
  • 1,170
  • 2
  • 10
  • 27
2
votes
2 answers

SQL - Summing with Sub/Main Groups

Let's say I have a table as the following: LogicalRef Code Balance 1 320.01 11.5 2 320.01 9 3 320.01.03 …
SQLfreaq
  • 141
  • 2
  • 11
2
votes
3 answers

Get last element of an ordered set in postgresql

I am trying to get the last element of an ordered set, stored in a database table. The ordering is defined by one of the columns in the table. Also the table contains multiple sets, so I want the last one for each of the sets. As an example consider…
LiKao
  • 10,408
  • 6
  • 53
  • 91