Keep a computed ranking updated in SQL-Server

user9430427

Here i have a requirement like biddin db structor is like

Id   Name       Amount    Bidding_Level
1     Apple      50         L3
2     Mac        30        L2
3     Nokia      10        L1

Here my Requirement is whenever user Enter amount Bidding_Level Comes dynamic Like if any user enter Amount as 5 He will Become L1 And all other Like as Nokia L2 Mac L3 Apple L4

EzLo

First of all, as Damien suggested, it seems that this value shouldn't be stored but computed when needed. When you have computed columns (that don't automatically update) then you are begging for additional maintenance tasks (what happens if someone changes the product name but keeps the same ID? and when someone deletes a bid?) and pointless processing (you might calculate the bid level and recieve another bid before actually needing the current bid level).

That being said, you can reset the bid ranking with a trigger, which will have to be after inserts, updates or deletes (you don't explain your business logic much).

CREATE TRIGGER tr_SetBiddingLevel ON Bids AFTER INSERT, UPDATE, DELETE
AS
BEGIN

    -- If the amount was updated, a new row was added or a row was deleted
    IF UPDATE(Amount) OR (
        EXISTS (SELECT 1 FROM deleted) AND
        NOT EXISTS (SELECT 1 FROM inserted))
    BEGIN

        ;WITH NewBidLevels AS
        (
            SELECT
                B.Id,
                NewBidLevel = ROW_NUMBER() OVER (ORDER BY B.Amount ASC)
            FROM
                Bids AS B
        )
        UPDATE B SET
            Bidding_Level = 'L' + CONVERT(VARCHAR(10), N.NewBidLevel)
        FROM
            Bids AS B
            INNER JOIN NewBidLevels AS N ON B.Id = N.Id

    END

END

Please note that the tigger will consider all records of the table, since it has to update all records for each change on any amount (which is very bad for performance). You should calculate this ranking each time it's needed instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ranking with LIKE keyword in SQL Server

From Dev

TryParse in a Sql Server computed column

From Dev

TryParse in a Sql Server computed column

From Dev

SQL Server - computed date column

From Dev

Sql Server - Counting partitions with Ranking functions

From Dev

Sql Server - Counting partitions with Ranking functions

From Dev

SQL Server: add ranking to nested selects

From Dev

SQL Server RANK() Function to get Overall Ranking

From Dev

SQL Server Computed Column as Primary Key

From Dev

Specify a computed column in SQL Server 2012 as an average

From Dev

SQL Server computed columns (0 if value is negative)

From Dev

SQL Server database not getting updated

From Dev

Knockout Computed with Parameter not updated

From Dev

sql server 2012 - ranking orders by year, month, supplier and location

From Dev

Sql server union but keep order

From Dev

Sql server union but keep order

From Dev

What is the best way to keep a list of products updated from a server?(IOS)

From Dev

Ember computed property not being updated

From Dev

Ember computed properties not getting updated

From Dev

Ember computed properties not getting updated

From Dev

Get the Identity of Last Updated Row in SQL Server

From Dev

Check if field value updated on sql server trigger

From Dev

SQL Server database last updated date time

From Dev

SQL Server view not updated in MVC view

From Dev

SQL Server database not being updated by query

From Dev

Updated a table based on a condtion Sql server?

From Dev

SQL Server Get Updated Column Value

From Dev

SQL Server : get value of @Columns_Updated

From Dev

SQL Server - How to remove IsPersisted attribute on computed column

Related Related

HotTag

Archive