Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234

Using OVER with an Aggregate Function Expand / Collapse
Author
Message
Posted Thursday, November 25, 2010 7:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:35 PM
Points: 5,930, Visits: 8,179
Gopinath Srirangan (11/25/2010)
Hi,
How do we filter the result using WHERE and HAVING clause as used in group by

Say like...

select MAX(TaxRate) AS 'Tax Rate',StateProvinceID
from Sales.SalesTaxRate where Name <> 'Canadian GST'
group by StateProvinceID
having StateProvinceID IN (1,6,7,9)
order by StateProvinceID

Thanks for posting this..

Hi Gopinath,

In this case, the filter on StateProvinceID can be in either the WHERE or the HAVING, because this is one of the columns in the GROUP BY. I personally prefer to filter as much as possible in the WHERE, so that's where I would move the filter. But in the HAVING is fine as well; just a matter of personal preference.
The filter on Name must be in the WHERE. The Name is not in the GROUP BY, so tha HAVING cannot filter on Name - after all, the rows in a group can have different names.

The HAVING clause is mainly used to filter on aggregate. For instance, if you want to see only the states/provinces with a highest tax rate of less than 20%, you would put "MAX(TaxRate) <= 20" in the HAVING clause. Since this refers to an agregate, it cannot be in the WHERE, but must be in the HAVING clause.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1026485
Posted Thursday, March 24, 2011 1:09 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, December 3, 2013 4:40 PM
Points: 654, Visits: 375
Thanks for the great question.

Window functions are really useful, and can make some complicated queries much simpler.
However, its one area that I really wish SQL Server would catch up to Oracle and Db2.

For example, Db2 provides the ability to define a set of rows to apply the aggregate to, with code such as:
sum(X) over (partition by Y order by Z rows between 1 preceding and current row)

This would allow the SUM of X to be applied for each group of Y, ordered by Z - but only aggregating the previous row and the current row. Essentially creating a running total, but without CTEs or nested queries.

Just my 2 cents worth :)
Post #1083107
Posted Monday, February 27, 2012 9:03 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, June 19, 2014 10:32 AM
Points: 483, Visits: 243
Great question and I have learnt more on 'OVER Clause' that I had not taken used much.

Thank you.
Post #1258327
« Prev Topic | Next Topic »

Add to briefcase «««1234

Permissions Expand / Collapse