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


How to update Flag for maximum amount in a a group


How to update Flag for maximum amount in a a group

Author
Message
avdhut.k
avdhut.k
SSChasing Mays
SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)

Group: General Forum Members
Points: 619 Visits: 266
Create Table #Temp
(
Pk_Id Int Identity(1,1),
Lnno Int,
Amount Int,
Flag Char(1)

)

Insert Into #Temp(Lnno,Amount)
Values (1,5),(1,10),(2,20),(2,25)

Select * from #Temp

Hi,
My requirement is,I want to update Flag as 'Y' for the Lnno which has maximum amount.

Please help this is urgent to me.

Thanks in Advance!
Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6287 Visits: 25530

WITH CTE AS (
SELECT Pk_Id,Lnno,Amount,Flag,
ROW_NUMBER() OVER(PARTITION BY Lnno ORDER BY Amount DESC) AS rn
FROM #Temp)
UPDATE CTE
SET Flag='Y'
WHERE rn=1;



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




keebler96
keebler96
SSC Journeyman
SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)

Group: General Forum Members
Points: 88 Visits: 147
update #temp
set flag = 'Y'
from #temp
join (select lnno ,max(amount) amount from #temp group by lnno) temp2 on
#temp.lnno = temp2.lnno
and #temp.amount = temp2.amount


avdhut.k
avdhut.k
SSChasing Mays
SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)SSChasing Mays (619 reputation)

Group: General Forum Members
Points: 619 Visits: 266
Thanks a lot!

Its work for me.
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