Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Updating First Field Based on the First of Grouped Rows Expand / Collapse
Author
Message
Posted Thursday, June 13, 2013 12:47 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, May 12, 2014 11:18 AM
Points: 56, Visits: 127
I have the following table.
Month EmpNum Prod intRate YTDRate
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


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
Post #1463261
Posted Thursday, June 13, 2013 1:15 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 3,313, Visits: 7,153
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_Month int,
empnum int,
prod char(4),
intRate decimal(15,9),
YTDRate decimal(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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1463275
Posted Thursday, June 13, 2013 1:49 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, May 12, 2014 11:18 AM
Points: 56, Visits: 127
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.
Post #1463291
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse