Skip to content
Advertisement

Calculation of rank according to supplier table & sales table

I’m working with SQL Server 2014; I attached a purchase table (from suppliers) and sales table.

First, I need to use a sales table based on the rankings from the purchase table, product bought first from supplier table sold first in sales table (FIFO method).

How do I do these calculations?

Attached is a rank calculation in the purchase table.

Thanks.

Tables:

Rank from table Purchases:

Advertisement

Answer

Try this …

Select X.*
From
(
    select 
        P.ItemID, 
        P.PurId, 
        Rank () Over 
        (
            Partition By 
            (
                Select Min(S.InvID)
                From Sales_5 As S With(NoLock)
                Where S.ItemID = P.ItemID
            ) 
            Order By P.PurId ASC
        )
        As rank_BY_Fifo 
    from Purchases As P With(NoLock)
) As X
Order By rank_BY_Fifo
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement