Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Ranking records

Author  Topic 

benking9987
Posting Yak Master

124 Posts

Posted - 2011-04-14 : 14:35:57
I have a table containing sales information for multiple items. I have thousands of items that are sold to one or more customers. The layout of the table looks something like this:


Item1, Customer1, MarginCustomer1, [Rank] (this is what I need)
Item1, Customer2, MarginCustomer2, [Rank]
Item1, Customer3, MarginCustomer3, [Rank]
Item1, Customer4, MarginCustomer4, [Rank]

Item2, Customer1, MarginCustomer1, [Rank]
Item2, Customer4, MarginCustomer4, [Rank]

Item3, Customer2, MarginCustomer2, [Rank]
Item3, Customer3, MarginCustomer3, [Rank]
Item3, Customer4, MarginCustomer4, [Rank]

Item4, Customer3, MarginCustomer3, [Rank]


Since not every item is sold to every customer, there are not always the same number of customer listings per sku. What i ultimately need to arrive at is being able to rank each Item/Customer pair. The top ranking Customer for each item will have the highest MarginCustomer, then ordered descending thereafter. These ranks will then be used to help me determine priorities

The company is limited to these 4 customers for now. Since the sales data (margin amounts) are always changing, i need a way to dynamically generate these rankings to use within the rest of my allocation regime.

Some sample data might look like this:

Item1, Customer1, .375
Item1, Customer2, .321
Item1, Customer3, .102
Item1, Customer4, .412

In this case, the ranking I would need to generate and write back to the table would look like this:

Item, Customer, Margin, Rank

Item1, Customer1, .375, 2
Item1, Customer2, .321, 3
Item1, Customer3, .102, 4
Item1, Customer4, .412, 1

Anyone out there who could help me with this?

Thanks a million in advance.

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2011-04-14 : 14:48:01
[code]select
rank() over (Partition By ItemID order by Margin desc),
*
from
...
[/code]
Go to Top of Page

benking9987
Posting Yak Master

124 Posts

Posted - 2011-04-14 : 16:12:34
This works for the most part. Awesome.

One tweak I think I need help with is when a customer sales the same item for the same margin, it gives the same rank for the two customers. Any way to have a secondary ranking value based on the alphabetical sorting of the customer?

Go to Top of Page

benking9987
Posting Yak Master

124 Posts

Posted - 2011-04-14 : 16:43:33
I figured this out BTW. i just added a secondary criteria for the ORDER BY clause...
Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2011-04-15 : 09:05:31
quote:
Originally posted by benking9987

I figured this out BTW. i just added a secondary criteria for the ORDER BY clause...



Thankyou for the update.

Here is a link on the topic: [url]http://msdn.microsoft.com/en-us/library/ms189798.aspx[/url]
Go to Top of Page
   

- Advertisement -