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

Linq grouping and nested grouping

I am in the process of creating a TreeView that can be displayed with various groupings. The essential classes are below: public class MachineStatus { public string MachineName {get;set;} public string Department {get;set;} public string…
Killnine
  • 5,728
  • 8
  • 39
  • 66
2
votes
2 answers

Sql Server Multiple Joins with Aggregate

I'm trying to fix a bug in inherited code. This query is meant to bring back an amex_meal_amount_total of $33, but it is giving $99. The problem is with the second join - there are three associated items in the EE table that is making the…
CutOffTies
  • 23
  • 4
2
votes
2 answers

How to improve performance of FOR XML PATH with large data set

I am attempting to use the FOR XML PATH technique of concatenating strings from a result set, but am running into some performance issues. I'm wondering if there's any way to improve this type of query. I am writing a query for a report that is…
Matt J
  • 41
  • 1
  • 7
2
votes
2 answers

% of total calculation without subquery in Postgres

I'm trying to create a "Percentage of Total" column and currently using a subquery with no issues: SELECT ID, COUNT(*), COUNT(*) / (SELECT COUNT(*) FROM DATA) AS % OF TOTAL FROM DATA GROUP BY ID; | ID | COUNT | % OF TOTAL | | 1 | 100 | …
AvocadoRivalry
  • 411
  • 1
  • 7
  • 16
2
votes
2 answers

Using Aggregate functions in DataView filters

i have a DataTable that has a column ("Profit"). What i want is to get the Sum of all the values in this table. I tried to do this in the following manner... DataTable dsTemp = new DataTable(); dsTemp.Columns.Add("Profit"); DataRow dr = null; dr…
Shrewdroid
  • 800
  • 1
  • 10
  • 31
2
votes
1 answer

Filter by count in elasticsearch

Consider the following data POST programming/languages/1 { "name":"java", "type":"general_purpose" } POST programming/languages/2 { "name":"javascript", "type":"scripting" } POST programming/languages/3 { "name":"c", …
Hariharan
  • 881
  • 1
  • 13
  • 25
2
votes
2 answers

How to change the value of sum function inside the query?

I have a query with sum aggregation function : SELECT sum(case when result=1 then 1 when result=2 then 0)as final_result From results I want to change this part when result=2 then 0 to something like that when result=2 then final_result equals…
Basel
  • 1,305
  • 7
  • 25
  • 34
2
votes
1 answer

Elasticsearch: aggregation min_doc_count for weeks doesn't work

I've the following aggregation with interval=week and min_doc_count=0 { "aggs": { "scores_by_date": { "date_histogram": { "field": "date", "format": "yyyy-MM-dd", "interval": "week", "min_doc_count": 0 } …
sultan
  • 5,978
  • 14
  • 59
  • 103
2
votes
2 answers

How to Select the Most Occuring Items in SQL with Names

I'm trying to select the most occurring item. This code works, but I don't know how to get it's respective ItemName from the Item table without it erroring SELECT TransactionDetail.ItemID, COUNT(*) AS 'AmountSold' FROM TransactionDetail GROUP…
user4481538
2
votes
3 answers

Mysql: Group by Hour, 0 if no data

I have the following query: SELECT count(*) as 'totalCalls', HOUR(`end`) as 'Hour' FROM callsDataTable WHERE company IN ( SELECT number FROM products WHERE products.id IN (@_PRODUCTS)) AND YEAR(`end`) = @_YEAR AND MONTH(`end`) =…
alex
  • 1,300
  • 1
  • 29
  • 64
2
votes
1 answer

In Postgres, how can I get the sub-grouping with the max value for a group?

In Postgres, I have a historical table for a subway system that has this structure: CREATE TABLE stop_history ( stop_id character varying, route_id character varying, next_stop_id character varying ); I'm trying to figure out: For a…
Oved D
  • 7,132
  • 10
  • 47
  • 69
2
votes
3 answers

mysql get the sum of unique date

From MySQL table I have the list amount based on the dates. I need to get the sum of amount for each date: ex: id type date amount 1 1 2015-01-01 100 2 1 2015-01-01 150 3 1 2015-01-02 10 4 1 2015-01-03 250 Here…
Subha
  • 741
  • 2
  • 9
  • 23
2
votes
4 answers

Get the row with the latest date in a group?

I have two tables: items: | item_id | title | comments: | comment_id | item_id | posted_at | author_id | text | Where posted_at is the time a comment was posted. How can I get a list of all items, with the time each of them was last commented on…
Mikhail Batcer
  • 1,938
  • 7
  • 37
  • 57
2
votes
2 answers

How to transform sql query results to pivot table using php arrays?

I am trying to create some statistics for a library reservation system. The result of my sql query looks like the following structure. total_no_students| department | property | month 241 | Physics | undergraduate | Nov …
George
  • 129
  • 2
  • 12
2
votes
1 answer

How to calculate a GPA from grades and credits?

I want to calculate a GPA for each student from grades and credits. I have executed something like this SET GPA=(SELECT((t.grade*c.credits)/c.credits) FROM Student s, Take t, Courses c WHERE s.sid=t.sid and t.cid=c.cid) It doesn't work. The…
faksu3442
  • 301
  • 1
  • 4
  • 11