1

I have a table A with columns as below:

Addr_id   Street_Add      Addr_type_cd        Acct_nbr         CITY                     
   1        12 ST             PROP               A              MN
   1        12 ST             MAIL               A1             MN
   1        12 ST             PROP               B              MN
   1        12 ST             PROP               A              MN
   1        12 ST             PROP               A              MN
   2        13 ST             PROP               A2             MI
   2        13 ST             MAIL               A3             MI
   3        14 ST             PROP               AB             MO
   4        15 ST             MAIL               AD             IL  

Output is as below;

OUTPUT - ADDR_ID            OUTPUT - ADDR_TYPE_ CD          
    1      PROP         
    1      MAIL         
    1      RPT          
    2      PROP         
    2      MAIL         
    2      RPT          
    3      PROP         
    3      RPT          
    4      MAIL         
    4      RPT          

The natural key is Street_addr and CITY. I am trying to write a Case expression populating another column ADDR_TYPE_CD in Table B based on below rules

  • WHERE STREET_ADD=STREET_ADD and CITY=CITY
  • IF ADDR_TYPE_cd= PROP and MAIL,
  • THEN ADDR_TYPE_CD in TABLE B = PROP, MAIL, RPT
  • IF ADDR_TYPE_cd= PROP
  • THEN ADDR_TYPE_CD in TABLE B = PROP, RPT
  • IF ADDR_TYPE_cd= MAIL
  • THEN ADDR_TYPE_CD in TABLE B = MAIL, RPT

I have tried writing through CASE statement as :

SELECT
     CASE addr_type_cd
     WHEN addr_type_cd='PROP' AND addr_type_cd='MAIL'                               
     THEN addr_type_cd='RPT' AND addr_type_cd='PROP' AND addr_type_cd='MAIL'    
     END                        
FROM Table A                        
WHERE street_addr = street_addr                     
AND city = city                     

I get missing keyword error, plus am not sure if the logic would work. Any inputs ideas will be very helpful. Hope the question is clear.

Thanks,

Pallavi

2
  • Sorry. I couldnt add Image with table format correctly
    – Pallavi
    Commented Feb 5, 2015 at 23:12
  • 1
    You can't assign multiple column values within a single CASE test like you're attempting. The THEN portion of your statement simply won't work. You can at best output a single aliased column. I can't offer a suggestion as to how to fix it, because I find your explanation of what you're trying to do rather confusing. How can street_addr NOT equal street_addr or ` city NOT equal city when they're the same column in the same row? IOW, your logic as written will always return every single row, because the columns will always be equal.
    – Ken White
    Commented Feb 5, 2015 at 23:23

1 Answer 1

0

There are few problems,

  1. your are trying to insert multiple rows into Target table based on single row in source table, if Addr_type_cd = 'PROP' insert two rows one with 'Prop' and another with 'RPT'

  2. Same way you are trying to capture data from two rows at same time and insert according to that, if prop, mail insert prop,mail,rpt

What you should do is fix your source Column (Addr_type_cd) to have grouped values where needed. After that it is simple. If source was like below, I have pasted corresponding code:

Table_A_Modified

Insert into Table_B
(   
    [Addr_id], 
    [Street_Add],
    [Addr_type_cd],
    [Acct_nbr] ,
    [CITY]
)
Select
    [Addr_id], 
    [Street_Add],        
        CASE 
            WHEN addr_type_cd = 'PROP' THEN 'PROP;RPT'   
            WHEN addr_type_cd='MAIL' THEN 'MAIL;RPT'    
            WHEN addr_type_cd='PROP;MAIL' THEN 'PROP;MAIL;RPT'
        END ,
    [Acct_nbr] ,
    [CITY]  
From Table_A

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.