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
2 answers

Consolidate rows in PostgreSQL

Here is my data: ID FName LName data1 data2 1 John Doe xxx1 2 John Doe xxx2 yyy2 And here is my desired result: ID FName LName data1 data2 1 John Doe xxx1 yyy2 In short, I have…
BrunoJ
  • 226
  • 2
  • 13
2
votes
2 answers

Finding the total time a match was standing in postgresql

I have a database that has a table called matchstats which includes a column called time and it is updated each time an action takes place. I also have a column called groundstatsid which when it is not null means the action took place on the ground…
Spence
  • 125
  • 1
  • 1
  • 10
2
votes
1 answer

What is the maximum number of key value pairs in a single hstore value?

I've got a postgres table (mapfeatures_20120813) with 2 columns (tags and pky) with around 1000 rows. Each row consists of a hstore and a primary key: tags (hstore) pky "aerialway"=>"cable_car"; 1 "aerialway"=>"chair_lift";…
zehpunktbarron
  • 1,193
  • 3
  • 15
  • 26
2
votes
1 answer

PostgreSQL query efficiency

I'm working with PostgreSQL (I'm a rookie in the database world) and I'd like to know your opinion on the efficiency of this kind of queries I found in the code I'm working with. These queries have a lot of JOINs, and one of them (bold font) has…
2
votes
1 answer

MySQL Query: How to sum 3 different values in a column, showing total of each value in result set

I have a table called inventory that includes two columns as follows: I would like to query this table to return a result set that lists how many items of each condition I have (column headers show in example below are not wanted in result…
user1218172
  • 193
  • 1
  • 12
2
votes
1 answer

Finding the average age of users with 0 posts, 1-5 posts, and >5 posts

I need to generate a report on the age of some of our users with respect to their post count grouped by their country of residence. Here's a simplified version of my schema right now: DESCRIBE…
t-mart
  • 890
  • 11
  • 27
2
votes
1 answer

Aggregate CTE and Update Query

I have studied a number of aggregate and CTE post but have not found a solution for updating a minumum value from a CTE to an additional (non-CTE) outside table. This is using SQL 2008 (Microsoft managment studio). I am combining dates from an…
2
votes
3 answers

TSQL Running Totals aggregate from sum of previous rows

Not sure how to word this. Say i have a select returing this. Name, month, amount John, June, 5 John, July,6 John, July, 3 John August, 10 and I want to aggregate and report beggining blance for each month. name, month, beggining balance. john,…
Hell.Bent
  • 1,667
  • 9
  • 38
  • 73
2
votes
1 answer

Sorting by non-alphabetic but displaying by alphabetic in group by/order by in postgresql

I have a database formed like this: descr | hours | timer ----------+-------+------------ foo | 1.3 | 2012-07-14 foo | 2.5 | 2012-07-15 foo | 2.35 | 2012-07-16 bar | 1 | 2012-08-16 baz | 1 |…
Arima
  • 195
  • 6
2
votes
1 answer

Grouping the results of a query with CTEs

I have a CTE based query into which I pass about 2600 4-tuple latitude/longitude values - that have been ID tagged and held in a second table called coordinates. These top left and bottom right latitude / longitude values are passed into the CTE in…
2
votes
2 answers

Mysql Sum distinct row

I want to sum the value of 3 columns from 3 tables My query: SELECT sum(s.paid_money+d.paid_money+e.paid_money) FROM payment_dates AS dates LEFT JOIN supplier_payments AS s ON dates.id = s.payment_date_id LEFT…
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
2
votes
2 answers

how to selectively count records and group them by date in mysql?

I have three MySQL Tables: customers, persons, and dates_d customers and persons are related through agent_code dates_d is just a table with all the dates of the last and following 10 years. that key allows me to determine how many customers each…
ILikeTacos
  • 17,464
  • 20
  • 58
  • 88
2
votes
2 answers

Distinct Aggregate Query in Microsoft Access with Group By on a Different Field

Among the fields in my table are 2 fields, CTN_NUM and PO_NUM. Each PO_NUM has at least one CTN_NUM, possibly more—distinct or repeated. Any given CTN_NUM cannot have more than 1 PO_NUM. In other words, it's a one to many relationship. I want to…
as9876
  • 934
  • 1
  • 13
  • 38
2
votes
3 answers

Oracle SQL Hierarchical Query: Flatten Hierarchy and Perform Aggregation

I am trying to improve performance for a proof of concept I have already written and am having no luck. I think the approach is probably flawed, but I’m struggling to find another solution. I’ve covered all the Ask Tom articles and forum posts I…
2
votes
1 answer

How to let mysql return the row with highest bullishness value?

Consider the following table: stock _date return2 bullishness ----------------------------------------- 1 2011-03-14 0.10 0.75 2 2011-03-14 0.05 1.75 3 2011-03-14 0.11 1.00 1 2011-03-15 0.03 …
Pr0no
  • 3,910
  • 21
  • 74
  • 121