Microsoft, SQL, 2005, Update Script Help Needed

Experts,

I have a field in my PatientProfile table  - 'ReleaseOfInformationIndicatorDate' I wish to update. The only twist is I want to set this date equal to the patients last visit. That table is Patientvisit. How can I tell my update script to give me the date of last visit in this field right? If no visit, leave Null.
update PatientProfile
inner join patientvisit pv on pp.PatientProfileId = pv.PatientProfileId
Set ReleaseOfInformationIndicatorDate = max(pv.visit)

Open in new window

LVL 7
Jeff SAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tigin44Commented:
try this

update pp
Set ReleaseOfInformationIndicatorDate = max(pv.visit)
from PatientProfile pp
                          inner join patientvisit pv on pp.PatientProfileId = pv.PatientProfileId
Jeff SAuthor Commented:
I am so sorry, If no visit then set equal to current date.

So, I need it to first look for a date ... if none, set to todays date. Make sense?
tigin44Commented:
then try this
update pp
Set ReleaseOfInformationIndicatorDate = ISNULL(max(pv.visit), GETDATE())
from PatientProfile pp
                          inner join patientvisit pv on pp.PatientProfileId = pv.PatientProfileId
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Jeff SAuthor Commented:
I get this back:

An Aggregate may not appear in the set list of an update statement
tigin44Commented:
sory for my carefullnes... this does what you want

update pp
Set ReleaseOfInformationIndicatorDate = t.VisitDate
from PatientProfile pp
        inner join (SELECT PatientProfileId, ISNULL(max(pv.visit), GETDATE()) AS VisitDate
                              FROM patientvisit pv
                              WHERE pp.PatientProfileId = pv.PatientProfileId) t ON t..PatientProfileId = pv.PatientProfileId
Jeff SAuthor Commented:
Server: Msg 107, Level 16, State 2, Line 3
The column prefix 'pp' does not match with a table name or alias name used in the query.
Server: Msg 107, Level 16, State 1, Line 3
The column prefix 'pv' does not match with a table name or alias name used in the query.
tigin44Commented:
This is tried...

update pp
Set ReleaseOfInformationIndicatorDate = t.VisitDate
from PatientProfile pp
        inner join (SELECT PatientProfileId, ISNULL(max(pv.visit), GETDATE()) AS VisitDate
                    FROM patientvisit pv
                    GROUP BY pv.PatientProfileId
                                    ) t ON t.PatientProfileId = pp.PatientProfileId

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.