如果某些列相等,如何替换一个表中的数据

时间:2018-09-10 09:48:50

标签: sql sql-server

我已经提取了以下数据,并希望基于相同的均等Hole_no列(例如,如果2b24d001的hole_no = 2b25d001。)将(2b24d001)的x,y,z替换为(2b24d001)的x,y,z。 / p>

hole_id     block_id    xcollar     ycollar  zcollar  HOLE_NO
2B24D001B01 2B24D001    4382.693    3858.52  109.725    B01
2B25D001B01 2B25D001    5372.693    3358.52  392.5      B01
2B25D001B02 2B25D001    7383.893    9356.03  662.5      B02
2B24D001B02 2B24D001    5373.893    3656.03  969.525    B02
2B24D001B03 2B24D001    8876.038    3352.216 399.38     B03
2B25D001B03 2B25D001    5376.038    3752.216 392.5      B03
1B13D078B03 1B13D078    1628.253    3956.26  692.53     B03
1B12D078B03 1B13D078    1828.253    3556.26  392.53     B03

3 个答案:

答案 0 :(得分:1)

您可以使用row_number()

params = open('params.bpr','r').read()


paramsencoded = base64.b64encode(bytes(params,'utf-8'))

print(paramsencoded)

paramsdecoded = str(base64.b64decode(str(paramsencoded,'utf-8')),'utf-8')

newparams = open('paramsencoded.bpr','w+',encoding='utf-8')
newparams.write(str(paramsencoded))
newparams.close()

params2 = open('paramsencoded.bpr',encoding='utf-8').read()
print(params2)

paramsdecoded = str(base64.b64decode(str(paramsencoded,'utf-8')),'utf-8')

paramsdecoded = base64.b64decode(str(params2))

print(str(paramsdecoded,'utf-8'))

或者您可以在同一件事中使用cte

select * from
 (
 select * from
(
select *,row_number() over(partition by HOLE_NO order by case when block_id='2b24d001' then 0 case when block_id='2b25d001' then 1 end desc ) as rn 
form tablet
) as t
where rn=1
) t1 innerjoin tablet t2 on t1.block_id=t2.block_id

答案 1 :(得分:0)

如果我正确理解,您似乎想基于UPDATE JOINJOINREPLACE (hole_id,block_id,'')

UPDATE t1
SET xcollar = t2.xcollar,
    ycollar = t2.ycollar,
    zcollar = t2.zcollar
FROM T t1 JOIN T t2
    ON REPLACE (t1.hole_id,t1.block_id,'') = REPLACE (t2.hole_id,t2.block_id,'')

sqlfiddle

答案 2 :(得分:0)

我认为您想要一个update / join,但是像这样:

UPDATE t1
    SET xcollar = t2.xcollar,
        ycollar = t2.ycollar,
        zcollar = t2.zcollar
    FROM T t1 JOIN
         T t2
         ON t1.hole_no = t2.hole_no
    WHERE t1.block_id = '2b24d001' AND
          t2.block_id = '2b25d001';