Updating First Field Based on the First of Grouped Rows

  • I have the following table.

    MonthEmpNumProdintRateYTDRate

    11111prd123.123240814

    21111prd221.205388462

    31111prd120.789172069

    41111prd123.566112884

    51111prd222.669155099

    12222prd420.054345462

    22222prd122.369777067

    32222prd21.256.345834007

    42222prd123.496047561

    52222prd124.054220056

    62222prd222.760099161

    23333prd21.253.934591185

    33333prd222.220853136

    43333prd121.71819

    53333prd21.251.083734747

    63333prd21.253.84453072

    I wrote the following query to find the first month in which YTDRate > intRate by Prod and by employee No.

    SELECT MIN(t1.Trans_Month) as Trans_Month,

    t1.EmpNum,

    t1.Prod,

    t1.intRate,

    t1.YTDRate

    FROM Sales_Data t1

    JOIN (SELECT Trans_Month,

    EmpNum,

    Prod,

    intRate,

    YTDRate

    FROM Sales_Data t2

    where YTDRate > intRate

    GROUP BY Trans_Month, t2.EmpNum, Prod, intRate, YTDRate) t3 ON

    t3.EmpNum = t1.EmpNum

    AND t3.YTDRate = t1.YTDRate

    AND t3.intRate = t1.intRate

    AND t3.Prod = t1.Prod

    AND t3.Trans_Month = t1.Trans_Month

    GROUP BY t1.Trans_Month, t1.EmpNum, t1.Prod, t1.YTDRate, t1.intRate

    Now I want to update a field Status with text "Exceeds intRate". How do I modify the queyr to do the update?

    Any help would be greatly appreciated.

    Thanks

  • I'm not sure what you're trying to do, I might need some expected results based on the data you provided. To get better help, you should post DDL and consumable sample data. I'm helping you with this, but in order to help you with your problem, you must give more information.

    DECLARE @Sales_Data TABLE(

    Trans_Monthint,

    empnumint,

    prodchar(4),

    intRatedecimal(15,9),

    YTDRatedecimal(15,9))

    INSERT @Sales_Data

    VALUES

    (1, 1111, 'prd1', 2, 3.123240814),

    (2, 1111, 'prd2', 2, 1.205388462),

    (3, 1111, 'prd1', 2, 0.789172069),

    (4, 1111, 'prd1', 2, 3.566112884),

    (5, 1111, 'prd2', 2, 2.669155099),

    (1, 2222, 'prd4', 2, 0.054345462),

    (2, 2222, 'prd1', 2, 2.369777067),

    (3, 2222, 'prd2', 1.25, 6.345834007),

    (4, 2222, 'prd1', 2, 3.496047561),

    (5, 2222, 'prd1', 2, 4.054220056),

    (6, 2222, 'prd2', 2, 2.760099161),

    (2, 3333, 'prd2', 1.25, 3.934591185),

    (3, 3333, 'prd2', 2, 2.220853136),

    (4, 3333, 'prd1', 2, 1.71819),

    (5, 3333, 'prd2', 1.25, 1.083734747),

    (6, 3333, 'prd2', 1.25, 3.84453072)

    SELECT *

    FROM @Sales_Data

    Notice that the code you posted will give the same results without the aggregate. And basically the same results as your inner query.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thanks, what I want to do is for each employee and for each product i wan to find the first month where YTDRate > IntRate. If it is, update the status to "exceeds rate".

    So for empNum = 1111, product = prd1 the first month is 1 whereas for prd2, the first month is 5.

    For empNum = 2222, product = prd1 it is 2, but for prd2 it is 3

    and for empNum = 3333 and prd2 the first month is 2.

    For the above rows, status will be updated to "exceeds rate" but left null for other months even if

    ytdRate > intRate.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply