Avatar of tlkngheads
tlkngheadsFlag for United States of America asked on

Get a max value in an update statement

I have the following code below and I would like to get the max HROWID from the logHistory that matches a specified ExpID.  So I would like to find the last entry in logHistory that matches a specific ExpID from my temp table.
Declare @tempTable Table(
	EmployeeID int,
	ExpID int,
	rowID int,
	TAID int,
	Notes varchar(1024),
	ProductName Varchar(512),
	GenericName varchar(512),
	Indication varchar(255),
	FieldName varchar(100),
	UpdateDate smalldatetime,
	HID int,
	change tinyint
)
 
Insert into @tempTable
	SELECT     tblExp_EmpTAExp.EmployeeID, tblExp_EmpTAExp.ExpID, 0, tblExp_EmpTAExp.TAID, null, null, null, null, null, null, null, tblExp_EmpTAExp.Review
	FROM         tblExp_EmpTAExp 
	WHERE     (tblExp_EmpTAExp.Review = 1) OR (tblExp_EmpTAExp.Review = 2)
 
 
-- product change value 1 = change field
Insert into @tempTable (EmployeeID, ExpID, RowID, TAID, change)
	SELECT tblExp_EmpTAExp.EmployeeID, tblExp_EmpTAProd.ExpID, tblExp_EmpTAProd.RowID, tblExp_EmpTAExp.TAID, 1
	FROM         tblExp_EmpTAProd INNER JOIN
                      tblExp_EmpTAExp ON tblExp_EmpTAProd.ExpID = tblExp_EmpTAExp.ExpID
	WHERE ((tblExp_EmpTAProd.Review = 1) OR (tblExp_EmpTAProd.Review = 1)) AND (tblExp_EmpTAProd.ExpID NOT IN (SELECT ExpID FROM @tempTable))
 
 
Update A set A.Notes = C.Notes, A.FieldName = 'Notes', A.UpdateDate = C.LastUpdated From logHistory B JOIN @tempTable A on A.ExpID = B.TRowID JOIN tblExp_EmpTAExp C on A.ExpID = C.ExpID
	WHERE B.FieldName = 'Notes'

Open in new window

Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
elimesika

8/22/2022 - Mon
elimesika


select TOP 1 HROWID  from @tempTable where 
 where ExpID = <put here your value>
order by HROWID DESC

Open in new window

ASKER
tlkngheads

Let me clarify what I am looking for because I don't think I gave a good explanation in my first post.  In the log table, lets say my ExpID is 59.  Well I might have 10 records that match ExpID of 59.  What I am looking for is the last record entered in for ExpID.  All entries do have a log date.  I will also be pulling the ExpID from my temp table.
ASKER CERTIFIED SOLUTION
elimesika

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23