SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Updating First Field Based on the First of Grouped Rows


Updating First Field Based on the First of Grouped Rows

Author
Message
tinausa
tinausa
Mr or Mrs. 500
Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)

Group: General Forum Members
Points: 503 Visits: 194
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
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43222 Visits: 19859
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.
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
tinausa
tinausa
Mr or Mrs. 500
Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)

Group: General Forum Members
Points: 503 Visits: 194
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search