thevan
thevan

Reputation: 10354

Is it possible to use Aggregate function in a Select statment without using Group By clause?

So far I have written Aggregate function followed by Group By clause to find the values based on SUM, AVG and other Aggregate functions. I have a bit confusion in the Group By clause. When we use Aggregate functions what are the columns I need to specify in the Group By clause. Otherwise Is there any way to use Aggregate functions without using Group By clause.

Upvotes: 31

Views: 88397

Answers (7)

Chandu Reddy
Chandu Reddy

Reputation: 1

Yes, without aggregate functions we can use group by column but it will be a distinct function.

select <column name> from <table name> group by <column name> 

Given the table EMP:

select * from EMP

That outputs:

7369    SMITH   CLERK       7902
7499    ALLEN   SALESMAN    7698
7521    WARD    SALESMAN    7698
7566    JONES   MANAGER     7839

Adding a group by

select job from EMP group by job

Outputs:

CLERK
SALESMAN
MANAGER

Upvotes: 0

Bhawesh Deepak
Bhawesh Deepak

Reputation: 71

The Columns which are not present in the Aggregate function should come on group by clause:

Select 
Min(col1), 
Avg(col2), 
sum(col3) 
from table

then we do not required group by clause, But if there is some column which are not present in the Aggregate function then you must use group by for that column.

Select 
col1, 
col2, 
sum(col3) 
from  table 
group by col1,col2

then we have to use the group by for the column col1 and col2

Upvotes: 3

gbn
gbn

Reputation: 432261

All columns in the SELECT clause that do not have an aggregate need to be in the GROUP BY

Good:

SELECT col1, col2, col3, MAX(col4)
...
GROUP BY col1, col2, col3

Also good:

SELECT col1, col2, col3, MAX(col4)
...
GROUP BY col1, col2, col3, col5, col6

No other columns = no GROUP BY needed

SELECT MAX(col4)
...

Won't work:

SELECT col1, col2, col3, MAX(col4)
...
GROUP BY col1, col2

Pointless:

SELECT col1, col2, col3, MAX(col4)
...
GROUP BY col1, col2, col3, MAX(col4)

Having an aggregate (MAX etc) with other columns without a GROUP BY makes no sense because the query becomes ambiguous.

Upvotes: 49

anthony sottile
anthony sottile

Reputation: 69914

You can use Select AGG() OVER() in TSQL

SELECT *,
SUM(Value) OVER()
FROM Table

There are other options for Over such as Partition By if you want to group:

SELECT *,
SUM(Value) OVER(PARTITION By ParentId)
FROM Table

http://msdn.microsoft.com/en-us/library/ms189461.aspx

Upvotes: 35

nvogel
nvogel

Reputation: 25526

Yes you can use an aggregate without GROUP BY:

SELECT SUM(col) FROM tbl;

This will return one row only - the sum of the column "col" for all rows in tbl (excluding nulls).

Upvotes: 10

Bohemian
Bohemian

Reputation: 425023

You must group by columns that do not have aggregate functions on them.

You may avoid a group by clause if all columns selected have aggregate functions applied.

Upvotes: 3

niktrs
niktrs

Reputation: 10066

You omit columns from the SELECT inside aggregate functions, all other columns should exist in GROUP BY clause seperated by comma.

You can have query with aggregates and no group by, as long as you have ONLY aggregate values in the SELECT statement

Upvotes: 1

Related Questions