Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#TAble is ambiguous

I am getting an error that states #Plans is ambiguous. This happens when I am joining a table to itself on a self join and am not sure why. Here is the code that leads to the error:

Alter Table #Plans
Add SecondPlanDate date

Update #Plans
Set 
    SecondPlanDate = Min (P2.PlanPurchaseDate) Over (Partition By P1.PatientID, P1.PlanPurchaseDate)
From
#Plans as P1
Inner Join
#Plans as P2
on
P1.PatientID = P2.PatientID
Where
P2.PlanPurchaseDate > P1.PlanPurchaseDate
;

Select
*
From
#Plans  

Any suggestions would be greatly appreciated.

Thanks,

like image 661
SASUSMC Avatar asked May 08 '14 23:05

SASUSMC


1 Answers

I think you need to use the table alias:

UPDATE P1
SET P1.SecondPlanDate ...
  • SQL Fiddle Demo
like image 127
sgeddes Avatar answered Sep 21 '22 18:09

sgeddes