日期:2014-05-18  浏览次数:20583 次

求一条sql语句写法
我写的SQL语句如下:

SELECT *
FROM vipPlan LEFT OUTER JOIN
(select top 1 ID,FanganNumber from VipProgress where fangannumber=vipPlan.FanganNumber ) as cc on cc.fangannumber = vipPlan.FanganNumber


报错内容如下:
消息 4104,级别 16,状态 1,第 1 行
无法绑定由多个部分组成的标识符 "vipPlan.FanganNumber"。


------解决方案--------------------
SQL code

declare @VipPlan table (ID int,fanganhao int)
insert into @VipPlan
select 1,111 union all
select 2,222

declare @VipProgress table (ID int,fanganhao int,[datetime] datetime,weight int)
insert into @VipProgress
select 1,111,'2012-2-1',50 union all
select 2,222,'2012-2-1',51 union all
select 3,111,'2012-2-4',50 union all
select 4,111,'2012-2-2',53

select b.id as bid,b.fanganhao as afanganhao,
a.id as aid,a.[datetime],a.weight from @vipplan b
left join @vipprogress a
on a.fanganhao=b.fanganhao
where [datetime]=(select max([datetime]) from @vipprogress where fanganhao=a.fanganhao)

/*
bid         afanganhao  aid         datetime                weight
----------- ----------- ----------- ----------------------- -----------
2           222         2           2012-02-01 00:00:00.000 51
1           111         3           2012-02-04 00:00:00.000 50
*/