select row_number() over(partition by w.productCode order by w.CreatedDate desc) as row_number,w.wholeprice,w.productcode,w.CreatedDate
into #bb
from Input_Product_WholePrice_Final wselect A.index_id,A.productcode,bb.wholeprice
from #bb bb
right outer join 
(
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;HDR=1;IMEX=1; Database=D:\北京价格.xlsx','SELECT *FROM [Sheet1$]')
)A on bb.productCode=A.productCode
where bb.row_number=1 order by A.index_idexcel表的格式是 
index_id productcode
1            534534
2                     
3            654645
结构选出来的结果index_id=2的数据没选出来一开始用的不是临时表,用的是
with bb as

    select row_number() over(partition by w.productCode order by w.CreatedDate desc) as row_number,w.wholeprice,w.productcode,w.CreatedDate
from Input_Product_WholePrice_Final w

选出的结果跟临时表是一样的,index_id=2的数据自动就筛掉了!我想保留index_id=2的数据~~~
想请教大家为甚么呢????

解决方案 »

  1.   

    index_id =2 的 productcode 值是空的 导入到数据库中应该变为null了 
    所以 join 没有找到拼配项。
      

  2.   

    我想要的就是
    index_id product wholeprice
    2        null    null
    这种结果而且,right join 实现的不就是这种结果么如果#bb不是临时表 我用正常的表试了 ,就能得出2        null    null
    这样的结果
      

  3.   

    楼主你right join的on子句是on bb.productCode=A.productCode,明显是根据productCode字段来匹配的,怎么会把index_id =2的值显示出来呢。因为你index_id =2的记录,productCode字段是空,在数据库中为null,因此不会显示出来。
      

  4.   

    #bb 里面的productCode 有null 就会和 a表(也就是excel数据表)匹配返回index_id =2这条记录
    如果没有,就不会返回。
      

  5.   

    这个我说错了,被你绕晕了,说的乱七八糟的,抱歉哈1、with bb as

      select row_number() over(partition by w.productCode order by w.CreatedDate desc) as row_number,w.wholeprice,w.productcode,w.CreatedDate
    from Input_Product_WholePrice_Final w
    )这个得到的数量和 select * from  Input_Product_WholePrice_Final 是一样的,不会过滤值2、请确认SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;HDR=1;IMEX=1; Database=D:\北京价格.xlsx','SELECT *FROM [Sheet1$]') 这一部分的值 建议把这个 insert到一个临时表里面,然后看一下数据是两天还是三条。
    right join 是返回你a 表(也就是execl)的所有值的,所以index_id =2 在正常情况下 肯定是会出现的