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 Wednesday, November 3, 2010 8:12 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 7:39 AM
Points: 1,426, Visits: 1,841
Comments posted to this topic are about the item Using OVER with an Aggregate Function

Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Post #1015661
Posted Wednesday, November 3, 2010 10:41 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 10, 2011 5:51 AM
Points: 3, Visits: 8
Hi:
If select distinct specified order by item must appear in the select list and there will be error message as follows:
Msg 145, Line 15 St 1


Post #1015676
Posted Thursday, November 4, 2010 2:13 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 8:13 AM
Points: 995, Visits: 981
Nice question, thank you.
It works for me.

Iulian
Post #1015717
Posted Thursday, November 4, 2010 2:16 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, August 29, 2014 5:30 AM
Points: 1,115, Visits: 448
Good question learned something new today.
Post #1015718
Posted Thursday, November 4, 2010 3:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:33 AM
Points: 6,020, Visits: 8,289
mislamuddin (11/3/2010)
If select distinct specified order by item must appear in the select list

It does. The code window has a scroll bar at the bottom; you'll find the column sstr.StateProvinceID to the far right.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1015738
Posted Thursday, November 4, 2010 3:11 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:33 AM
Points: 6,020, Visits: 8,289
Imran Ashraf-452633 (11/4/2010)
Good question learned something new today.

(Not targeting you specifically, but everyone who posted and/or will post similar comments)
The question is good indeed - but please don't learn too much from this.
If you take away the knowledge that you can use the OVER() clause for all aggregates except CHECKSUM, that's fine. But if you intend to use this query for similar purposes, think again. There are far better ways to get the maximum tax rate per state/province id:

SELECT MAX(TaxRate), StateProvinceID
FROM Sales.SalesTaxRate
GROUP BY StateProvinceID
ORDER BY StateProvinceID;

Much easier to understand and maintain in the future, and a more efficient execution plan to boot!

(EDIT: Fixed a silly typo - thanks, Oleg, for the heads-up!)



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1015740
Posted Thursday, November 4, 2010 3:19 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 9:16 AM
Points: 1,766, Visits: 6,386
I knew what the answer was, and thought that "The maximum Tax Rates across all State/Province IDs" described it well. I supopose I should have read more closely and realised that "The maximum Tax Rates per State/Province ID" also described it well, then tried to work out the difference between the two statements. Though I'd probably have picked the wrong one anyway.
Should it have said "The maximum Tax Rate [singular] across all State/Province IDs"?
Post #1015742
Posted Thursday, November 4, 2010 6:18 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 7:10 AM
Points: 1,869, Visits: 3,457
Goo question, thanks, but I don't understand why this was a two point question...
Post #1015799
Posted Thursday, November 4, 2010 7:10 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 7:35 AM
Points: 2,817, Visits: 2,563
I have never used the OVER function before. I had to do a bit of reading to understand the question. I learned about a useful, but limited, function. Good question, thanks.
Post #1015836
Posted Thursday, November 4, 2010 7:48 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 8:17 AM
Points: 1,407, Visits: 1,573
The existence of the Distinct in the query confused me, at first.

The key to using the Over as opposed to doing a Group By is really when you want to see all the rows in the group, but include an aggregate value.

With this example, where the only other field being displayed is the StateProvinceID, multiple rows for the same State add little value -- this is likely where the Distinct comes in.

I have used the Over clause for Row_Number, but not for aggregates, so this question gave me a few things to explore.
Post #1015870
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse