|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 8:16 AM
Points: 1,234,
Visits: 1,394
|
|
Hugo Kornelis (11/4/2010)
(If you take away the knowledge that you can use the OVER() clause for all aggregates except CHECKPOINT, 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:
One other important take away, although not illustrated in this question, is that a Select can have multiple Over clauses with different Partitions. This is something that Group By cannot do.
Select StateProvinceID, TaxType, Max(TaxRate) Over (Partition by StateProvinceID), Max(TaxRate) Over (Partition by StateProvinceID, TaxType) from Sales.SalesTaxRate Order by StateProvinceID
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232,
Visits: 1,046
|
|
mislamuddin (11/3/2010) 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
I did not get this error. Of course I also did not get the differance between the first two answers. 
Nice question about a great feature!
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 7:15 AM
Points: 2,865,
Visits: 2,467
|
|
Very good question - I learned something today.
Hugo If you take away the knowledge that you can use the OVER() clause for all aggregates except CHECKPOINT, 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:
To look at the question and only talk about it's limitations makes one wonder why is it there.
brdudley One other important take away, although not illustrated in this question, is that a Select can have multiple Over clauses with different Partitions. This is something that Group By cannot do.
Excellent addition to the questions functionality. I learned something more, and have already started playing with tests to see about changes, especially in some complex reports.
Thanks
Steve Jimmo Sr DBA “If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 2:02 PM
Points: 594,
Visits: 889
|
|
| I worry that the DISTINCT is going to give people the impression that OVER/PARTITION does the same thing as GROUP BY. It's an important distinction and the explanation doesn't make it clear.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, February 12, 2013 1:09 PM
Points: 335,
Visits: 391
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, July 25, 2012 9:04 PM
Points: 542,
Visits: 187
|
|
got Confused between across all state & each state and got it wrong...
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Tuesday, April 02, 2013 8:28 PM
Points: 555,
Visits: 2,137
|
|
My engish beeing what it is, I did also not get the differance between the first two answers, but that's ok. I learned something new and it is the important thing. Thank you! I am not sure of what "over" does thought...and yes, I was starting to thing that it does the same thing than a group by. So, what would be the first purpose of using an Over(partition by) function?
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 2:11 AM
Points: 9,378,
Visits: 6,473
|
|
tilew-948340 (11/4/2010) My engish beeing what it is, I did also not get the differance between the first two answers, but that's ok. I learned something new and it is the important thing. Thank you! I am not sure of what "over" does thought...and yes, I was starting to thing that it does the same thing than a group by. So, what would be the first purpose of using an Over(partition by) function?
First of all, nice question. I was remembered of the fact that OVER() can also be used with other functions except ranking functions.
To answer tilews question: most of the time (in my case that is ), OVER() and partition by is used with ranking functions. An example:
SELECT * FROM (SELECT column1 ,column2 ... , ROW_NUMBER() OVER(PARTITION BY myBusinessKey ORDER BY Priority DESC) AS RID FROM myTable) tmp WHERE RID = 1 This code will select all the rows from a table, partition them by business key, order it by some column that designates a priority (doesn't have to be, but just for the sake of the example) and then selects every row with a row_number of 1. This will select all the rows with unique values of the business key and with the highest priority (thus returning no duplicates).
How to post forum questions. Need an answer? No, you need a question. What’s the deal with Excel & SSIS?
Member of LinkedIn. My blog at LessThanDot.
 MCSA SQL Server 2012 - MCSE Business Intelligence
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:22 AM
Points: 5,244,
Visits: 7,059
|
|
tilew-948340 (11/4/2010) My engish beeing what it is, I did also not get the differance between the first two answers, but that's ok. I learned something new and it is the important thing. Thank you! I am not sure of what "over" does thought...and yes, I was starting to thing that it does the same thing than a group by. So, what would be the first purpose of using an Over(partition by) function? Hi Tilew,
If you take out the DISTINCT from the SELECT (and/or add a few extra columns), it becomes obvious. The MAX(...) OVER(PARTITION BY ...) gives you the maximum of a group in each row. In this case, without the DISTINCT, you would get one row for each SalesTaxRate, and each row would include the maximum TaxRate for the StateProvinceID of that row. Before we had the OVER() clause for aggregates, this could only be achieved with a subquery or a join with a derived table.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|