MS SQL Server statetment

I have a table that contains the following columns and data.  It lists visits by a patient to a clinic, the diagnose and treatment.

Problem is the Diagnose column only contains data for the first visit.

PatientID VisitDate             Diagnose, Treament
1             2009-01-01        AMD         AN
1             2009-01-06        null           AN
1             2009-01-06        null           AN
2             2009-01-02        MYS           AN
2             2009-04-02        null           AN
2             2009-08-03        null           AN
3             2009-01-04        MYS           AN
3             2009-03-02        null           AN

I want to update the table so that it becomes:  
1             2009-01-01        AMD         AN
1             2009-01-06        AMD         AN
1             2009-01-06        AMD         AN
2             2009-01-02        MYS         AN
2             2009-04-02        MYS         AN
2             2009-08-03        MYS        AN
3             2009-01-04        MYS        AN
3             2009-03-02        MYS         AN

in other words I want to find the diagnose given at the first visit and update all the other rows belonging to that patient.

How could I do this?
soozhCEOAsked:
Who is Participating?
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.

Aneesh RetnakaranDatabase AdministratorCommented:
test this on a test table

UPDATE t
SET Diagnose  = (SELECT Diagnose from PatientTable p2 where p2.PatientID = p.PatientID  AND p.Treatment= p1.Treatment  and p2.Diagnose is not null )
FROM PatientTable p
WHERE p.Diagnose IS null
0
bmatumburaCommented:
Try the code below:
DECLARE @id int, @current_diagnose NVARCHAR(50), @last_diagnose NVARCHAR(50), @visitdate DateTime
 
DECLARE patient_cursor CURSOR FORWARD_ONLY STATIC FOR 
SELECT id, VisitDate, diagnose
FROM PatientVisits ORDER BY PatientID, VisitDate 
 
OPEN patient_cursor
 
 
FETCH NEXT FROM patient_cursor
INTO @id, @visitdate, @current_diagnose
 
WHILE @@FETCH_STATUS = 0
BEGIN
 
IF (@current_diagnose IS NULL)
	BEGIN
		BEGIN
			UPDATE PatientVisits SET Diagnose = @last_diagnose WHERE PatientID = @id AND VisitDate = @visitdate
			SET @t = @t + 1
		END
	END
ELSE
	BEGIN
		@last_diagnose = @current_diagnose
	END
 
FETCH NEXT FROM patient_cursor
INTO @id, @visitdate, @current_diagnose
 
END
 
CLOSE patient_cursor
DEALLOCATE patient_cursor

Open in new window

0
bmatumburaCommented:
Please use the revised code below:
DECLARE @id int, @current_diagnose NVARCHAR(50), @last_diagnose NVARCHAR(50), @visitdate DateTime
 
DECLARE patient_cursor CURSOR FORWARD_ONLY STATIC FOR 
SELECT id, VisitDate, diagnose
FROM PatientVisits ORDER BY PatientID, VisitDate 
 
OPEN patient_cursor
 
 
FETCH NEXT FROM patient_cursor
INTO @id, @visitdate, @current_diagnose
 
WHILE @@FETCH_STATUS = 0
BEGIN
 
IF (@current_diagnose IS NULL)
	BEGIN
		BEGIN
			UPDATE PatientVisits SET Diagnose = @last_diagnose WHERE PatientID = @id AND VisitDate = @visitdate
		END
	END
ELSE
	BEGIN
		@last_diagnose = @current_diagnose
	END
 
FETCH NEXT FROM patient_cursor
INTO @id, @visitdate, @current_diagnose
 
END
 
CLOSE patient_cursor
DEALLOCATE patient_cursor

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

soozhCEOAuthor Commented:
When I take the previous code and put it in my store procedure I get a syntax error "Incorrect syntax near '@Last_Diagnose'."

See the code below
DECLARE @Kli_KlinikNr int
declare @PatientId varchar(15) 
declare @Öga char
declare @BesöksDag datetime
declare @Last_Diagnose varchar(50)
declare @Current_Diagnose varchar(50)
 
 
DECLARE patient_cursor CURSOR FORWARD_ONLY STATIC FOR 
SELECT Kli_KlinikNr, PatientId, Öga, BesöksDag, DiagnosAktuelltÖga
FROM @ReportData ORDER BY Kli_KlinikNr, PatientID, Öga, BesöksDag 
 
OPEN patient_cursor
  
FETCH NEXT FROM patient_cursor
INTO @Kli_KlinikNr, @PatientId, @Öga, @BesöksDag, @Current_Diagnose
 
WHILE @@FETCH_STATUS = 0
BEGIN
 
  IF (@Current_Diagnose IS NULL)
  begin
 
    UPDATE @ReportData SET DiagnosAktuelltÖga = @last_diagnose 
    WHERE (kli_kliniknr = @Kli_KlinikNr) and (PatientID = @PatientId) and (Öga = @Öga) and (BesöksDag = @BesöksDag) 
	
  end
  else
  begin
    @Last_Diagnose = @Current_Diagnose  <===== SYNTAX ERROR????
  end 
  FETCH NEXT FROM patient_cursor
  INTO @Kli_KlinikNr, @PatientId, @Öga, @BesöksDag, @current_diagnose
 
END
 
CLOSE patient_cursor
DEALLOCATE patient_cursor

Open in new window

0
Aneesh RetnakaranDatabase AdministratorCommented:
did you check my post, i dont think you need a cursor here
0
Jagdish DevakuSr DB ArchitectCommented:
try this...

though i have not tested it... i think this works...

please let me know if there is any issue...
update t
set t.diagnose = t1.diagnose
from PatientTable t inner join
PatientTable t1 on t.PatientID = t1.PatientID 
where t.PatientID not in (select t.PatientID from t1.PatientTable where diagnose is null)

Open in new window

0
Jagdish DevakuSr DB ArchitectCommented:
Hi...

please ignore my above post as the above query is not working...

try this one which is working and tested too...
update t
set t.diagnose = t1.diagnose
from PatientTable t inner join PatientTable t1 on t.PatientID = t1.PatientID 
where t1.diagnose is not null

Open in new window

0
bmatumburaCommented:
Sorry about the late response, but here is the fixed code... (actually I missed the SET statement on that line)
DECLARE @id int, @current_diagnose NVARCHAR(50), @last_diagnose NVARCHAR(50), @visitdate DateTime
 
DECLARE patient_cursor CURSOR FORWARD_ONLY STATIC FOR 
SELECT id, VisitDate, diagnose
FROM PatientVisits ORDER BY PatientID, VisitDate 
 
OPEN patient_cursor
 
 
FETCH NEXT FROM patient_cursor
INTO @id, @visitdate, @current_diagnose
 
WHILE @@FETCH_STATUS = 0
BEGIN
 
IF (@current_diagnose IS NULL)
        BEGIN
                BEGIN
                        UPDATE PatientVisits SET Diagnose = @last_diagnose WHERE PatientID = @id AND VisitDate = @visitdate
                END
        END
ELSE
        BEGIN
                SET @last_diagnose = @current_diagnose
        END
 
FETCH NEXT FROM patient_cursor
INTO @id, @visitdate, @current_diagnose
 
END
 
CLOSE patient_cursor
DEALLOCATE patient_cursor

Open in new window

0

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
Jagdish DevakuSr DB ArchitectCommented:
do we require cursors here... could you please explain it...???
0
bmatumburaCommented:
Oh yes, you require cursors. Take, for example, a person who visits again for a different "Diagnose" and "Treatment"...

PatientID VisitDate             Diagnose, Treament
1             2009-01-01        AMD         AN
1             2009-01-06        null           AN
1             2009-01-06        null           AN
2             2009-01-02        MYS           AN
2             2009-04-02        null           AN
2             2009-08-03        null           AN
3             2009-01-04        MYS           AN
3             2009-03-02        null           AN
1             2009-05-01        MYS         AN
1             2009-05-06        null           AN

In the sample data above, note that Patient 1 has 2 diagnose values: AMD and MYS. How are you going to ensure that the "Diagnose" for the last visit for patient 1 (last record in sample data) is updated to MYS and not to AMD?
0
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.