通过查询将列插入另一个表

时间:2012-06-05 05:50:19

标签: sql-server-2005

如何更新此表格的范围字段

enter image description here

(不要介意上面范围内的'1'值,它应该是 NULL

使用下表中的Range值

enter image description here

当我内部将表与另一个加入他们的EquipmentCode时,值会加倍,如下所示: enter image description here

这是我使用的查询:

SELECT 
  C.*, 
  R.Range AS RangeFromOtherTable 
FROM 
  EquipmentCalibrationRef AS C
INNER JOIN 
  Range AS R 
ON 
  R.EquipmentCode = C.EquipmentCode

1 个答案:

答案 0 :(得分:0)

首先,您可以使用以下语句获取伪1对1匹配

SELECT  ecr.*, r.[Range]
FROM    EquipmentCalibrationRef ecr
        INNER JOIN (
          SELECT  RefNo = ROW_NUMBER() OVER (PARTITION BY EquipmentCode ORDER BY [Range]), *          
          FROM    [Range] r
        ) r ON r.RefNo = ecr.RefNo AND r.EquipmentCode = c.EquipmentCode