PARTITION BY clause is used for partitioning a table into sets based on a particular field/fields so that we may require some operation to be performed on each set.GROUP BY causes the records to be grouped based on field specified and we may use them for operations like aggregation.A typical example can be given asTableID Category Amount123 Cat1 200127 Cat2 150123 Cat1 300136 Cat3 225132 Cat2 123123 Cat3 200136 Cat2 170123 Cat2 200
we have two requirements1. Find total amount of sales done by each person(ID)2.Find the person who did highest sale for each categoryObviuosly, for 1st one we require aggregating sales amt on basis of each ID and return as a single value per ID . Here is where we use group by as:-SELECT ID, Sum(Amount)FROM TableGROUP BY IDORDER BY ID
this givesID Category Amount123 Cat1 900127 Cat2 150132 Cat2 123136 Cat3 395
Now, for second one we need to find the person(ID) who did largest sale for each category. SO here we require first to split table to sets based on category and then arrange records based on decreasing order of sale amount. here is where PARTITION BY...OVER comes to picture.SELECT ROW_NUMBER() OVER (PARTITION BY Category ORDER BY Amount DESC)as 'RowNo',ID,Category,AmountFROM TableORDER BY CategorygivesRowNo ID Category Amount1 123 Cat1 3002 123 Cat1 2001 123 Cat2 2002 136 Cat2 1703 127 Cat2 1504 132 Cat2 1231 136 Cat3 2252 123 Cat3 200
basically what it did is number the records after splitting by category sets and based on decreasing order of Amount.Retrieveing all records with RowNo=1 gives as person who did largest sale based on each category.Hope this made you clear the differences b/w GROUP BY & PARTITION BY