|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 23, 2013 1:02 AM
Points: 1,277,
Visits: 1,609
|
|
|
|
|
|
Forum 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
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Saturday, March 16, 2013 9:53 AM
Points: 847,
Visits: 768
|
|
Nice question, thank you. It works for me.
Iulian
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 2:54 AM
Points: 1,103,
Visits: 411
|
|
| Good question learned something new today.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 3:49 PM
Points: 5,244,
Visits: 7,063
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 3:49 PM
Points: 5,244,
Visits: 7,063
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 9:52 AM
Points: 1,356,
Visits: 4,761
|
|
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"?
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Thursday, May 23, 2013 8:52 AM
Points: 1,788,
Visits: 3,327
|
|
| Goo question, thanks, but I don't understand why this was a two point question...
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 1:10 PM
Points: 2,673,
Visits: 2,418
|
|
| 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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 3:56 PM
Points: 1,235,
Visits: 1,397
|
|
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.
|
|
|
|