简体   繁体   中英

Copying values from one column to another in the same table

EDIT

The code below is providing particular records from the PRDTQUESTION table. Based on that segregated records I am trying to UPDATE values in column TO_PRODUCTFRAMESEQNO with values from column FROM_PRODUCTFRAMESEQNO (copy values from FROM_PRODUCTFRAMESEQNO to TO_PRODUCTFRAMESEQNO).

I am still quite new to SQL and I am not sure if my approach is correct. Can any of you guys have a look? Thanks for any help.

select  p.PRDTQUESTIONSEQNO AS FROM_PRDTQUESTION,
        p.PRODUCTFRAMESEQNO AS FROM_PRODUCTFRAMESEQNO,
        v.PRDTQUESTIONSEQNO AS TO_PRDTQUESTION,
        V.PRODUCTFRAMESEQNO AS TO_PRODUCTFRAMESEQNO 
    FROM prdtquestion p
    JOIN PRDTQUESTION v
    ON p.parttypeseqno = v.parttypeseqno
    where p.questionseqno in (4958) and p.parttypeseqno 
          in (select parttypeseqno from parttypeversummary where mqparttypeverseqno = 99464)
    AND v.PRDTQUESTIONseqno in 
            (select PRDTQUESTIONseqno from prdtquestion where questionseqno in (31138) 
            and parttypeseqno in (select parttypeseqno from parttypeversummary where mqparttypeverseqno = 99464));

Output from the script above - SEGREGATED TABLE - values in columns FROM_PRODUCTFRAMESEQNO and TO_PRODUCTFRAMESEQNO are different in almost all rows

FROM_PRDTQUESTION  FROM_PRODUCTFRAMESEQNO  TO_PRDTQUESTION  TO_PRODUCTFRAMESEQNO 
            999228              14193               999051               14189
            999229              14169               998957               14169
            999230              14177               998983               14173
            999231              14121               999068               14117
            999232              14145               998941               14141
            999233              14137               998924               14133
            999234              14185               998907               14181
            999235              18862               999161               18859
            999236              18870               999178               18867
            999237              18878               999195               18875
            999238              17943               999110               17940

EXPECTED RESULT: - as you can see values in columns FROM_PRODUCTFRAMESEQNO and TO_PRODUCTFRAMESEQNO are the same

FROM_PRDTQUESTION  FROM_PRODUCTFRAMESEQNO  TO_PRDTQUESTION  TO_PRODUCTFRAMESEQNO 
            999228              14193               999051               14193
            999229              14169               998957               14169
            999230              14177               998983               14177
            999231              14121               999068               14121
            999232              14145               998941               14145
            999233              14137               998924               14137
            999234              14185               998907               14185
            999235              18862               999161               18862
            999236              18870               999178               18870
            999237              18878               999195               18878
            999238              17943               999110               17943

I have tried update, but with no success:

update PRDTQUESTION 
set TO_PRODUCTFRAMESEQNO = FROM_PRODUCTFRAMESEQNO, usid = 'ACKLIM01', tmstamp = SYSDATE
where PRDTQUESTION in (
     select  p.PRDTQUESTIONSEQNO AS FROM_PRDTQUESTION,
             p.PRODUCTFRAMESEQNO AS FROM_PRODUCTFRAMESEQNO,
             v.PRDTQUESTIONSEQNO AS TO_PRDTQUESTION,
             V.PRODUCTFRAMESEQNO AS TO_PRODUCTFRAMESEQNO 
         FROM PRDTQUESTION p
         JOIN PRDTQUESTION v
         ON p.parttypeseqno = v.parttypeseqno
         where p.questionseqno in (4958) and p.parttypeseqno 
              in (select parttypeseqno from parttypeversummary where mqparttypeverseqno = 99464)
    AND v.PRDTQUESTIONseqno in 
            (select PRDTQUESTIONSEQNO from PRDTQUESTION where questionseqno in (31138) 
            and parttypeseqno in (select parttypeseqno from parttypeversummary where mqparttypeverseqno = 99464))
);

For a single update in the PRDTQUESTION table what has worked for me:

'SINGLE UPDATE SCRIPT'

update prdtquestion
set PRODUCTFRAMESEQNO = (select PRODUCTFRAMESEQNO from prdtquestion where prdtquestionseqno in (999229)), tmstamp = SYSDATE, usid = 'ACKLIM01'
where prdtquestionseqno = 998957;

values in the script: 'prdtquestionseqno in (999229)' and 'prdtquestionseqno = 998957' are taken from the 'SEGREGATED TABLE'.

I could repeat that 'SINGLE UPDATE SCRIPT' to updated individually every cell in the 'SEGREGATED TABLE' - but I am looking for some shortcut

Let me know is anything is still unclear. Thanks

As per my understanding, following query may help you.

select * from (select  p.PRDTQUESTIONSEQNO AS FROM_PRDTQUESTION,
        p.PRODUCTFRAMESEQNO AS FROM_PRODUCTFRAMESEQNO,
        v.PRDTQUESTIONSEQNO AS TO_PRDTQUESTION,
        V.PRODUCTFRAMESEQNO AS TO_PRODUCTFRAMESEQNO 
    FROM prdtquestion p
    JOIN PRDTQUESTION v
    ON p.parttypeseqno = v.parttypeseqno
    where p.questionseqno in (4958) and p.parttypeseqno 
          in (select parttypeseqno from parttypeversummary where mqparttypeverseqno = 99464)
    AND v.PRDTQUESTIONseqno in 
            (select PRDTQUESTIONseqno from prdtquestion where questionseqno in (31138) 
            and parttypeseqno in (select parttypeseqno from parttypeversummary where mqparttypeverseqno = 99464)))A where A.FROM_PRODUCTFRAMESEQNO=A.TO_PRODUCTFRAMESEQNO ;

And if you want to match each value of FROM_PRODUCTFRAMESEQNO for all TO_PRODUCTFRAMESEQNO values irrespective of which row it belongs to, then you can use exists/in operator.

This should work and is simpler. I have trimmed the SQL for simplicity.

select *
from (
select  p.PRDTQUESTIONSEQNO AS FROM_PRDTQUESTION,
        p.PRODUCTFRAMESEQNO AS FROM_PRODUCTFRAMESEQNO,
        v.PRDTQUESTIONSEQNO AS TO_PRDTQUESTION,
        V.PRODUCTFRAMESEQNO AS TO_PRODUCTFRAMESEQNO 
    FROM prdtquestion p
    JOIN PRDTQUESTION v
    ON p.parttypeseqno = v.parttypeseqno
) where FROM_PRDTQUESTION <> TO_PRDTQUESTION

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM