Reputation: 15
I'm relatively new to SQL so forgive my ignorance...
I have three tables, SRS_CAP, SRS_IPF and SRS_URA.
I would like to copy data from SRS_IPF to SRS_URA where (some of) the primary keys of SRS_IPF equal those of SRS_CAP.
Specifically, and this is where my problem arises, I want to copy multiple rows from SRS_IPF to one row in SRS_URA. The script below illustrates what I want to achieve, but isn't intended to work - there must be a more efficient method (and one that actually works!):
INSERT INTO srs_ura
(ura_stuc, ura_seqn, ura_stat, ura_name, ura_orgn, ura_add1)
SELECT ipf_ipuc, mySEQ, 'GP',
(SELECT ipf_valu
FROM srs_ipf
WHERE ipf_code = 'IPQ_REF1TIT'
AND ipf_ippc = CAP_MCRC
AND ipf_ipuc = CAP_STUC),
(SELECT ipf_valu
FROM srs_ipf
WHERE ipf_code = 'IPQ_REF1ORG'
AND ipf_ippc = CAP_MCRC
AND ipf_ipuc = CAP_STUC,
(SELECT ipf_valu
FROM srs_ipf
WHERE ipf_code = 'IPQ_REF1AL1'
AND ipf_ippc = CAP_MCRC
AND ipf_ipuc = CAP_STUC)
FROM srs_ipf, srs_cap
WHERE ipf_ippc = CAP_MCRC AND ipf_ipuc = CAP_STUC;
As you can see - I want to repeatedly insert the ipf_valu field into multiple columns in srs_ura, but where the ipf_code of the same row in srs_ipf differs.
So my question is, what area of SQL programming should I be researching, to achieve the above?
I have further issues regarding auto-sequencing and NCLOBs but I'll tackle those later. I'm on Oracle server.
Thanks in advance
Upvotes: 1
Views: 3599
Reputation: 67722
Does this select return the rows you want to insert?
SELECT ipf_ipuc, /*mySEQ,*/ 'GP',
MAX(CASE WHEN ipf_code = 'IPQ_REF1TIT' THEN ipf_valu END),
MAX(CASE WHEN ipf_code = 'IPQ_REF1ORG' THEN ipf_valu END),
MAX(CASE WHEN ipf_code = 'IPQ_REF1AL1' THEN ipf_valu END)
FROM srs_ipf, srs_cap
WHERE ipf_ippc = CAP_MCRC AND ipf_ipuc = CAP_STUC
AND ipf_code IN ('IPQ_REF1TIT', 'IPQ_REF1ORG', 'IPQ_REF1AL1')
GROUP BY ipf_ipuc
Upvotes: 1