Teradata SQL Test – 3

teradata

We have a table which holds the information about CUSTOMERS and the CONTACT NUMBER of the CUSTOMERS. So in table we have CUST_ID, CUST_NUMBER and CUST_OFF_NUMBER. For few CUSTOMERS, the NUMBER and OFFICE NUMBER is same and for few CUSTOMERS , NUMBER and OFFICE NUMBER is different. Below is the snapshot of the CUSTOMERS table:

Snapshot of CUSTOMERS table
Snapshot of CUSTOMERS table

The requirement is to fetch output in two columns (CUST_ID, CUST_NUMBER) such that if the CUSTOMER has same CUST_NUMBER and CUST_OFF_NUMBER then only one row should be present for the CUSTOMER however if the CUST_NUMBER and CUST_OFF_NUMBER are different then two rows should be present ; one for CUST_ID,CUST_NUMBER and second for CUST_ID,CUST_OFF_NUMBER. Below is the output which is required from the SQL Query.

Ouput - Teradata SQL Test -3
Ouput – Teradata SQL Test -3

Can you write the correct query? Leave your SQL Query as comment below. There may be several ways of getting the desired output. You can check the answer on ANSWERS PAGE too. However for better understanding of Teradata SQL , it is suggested that you leave a comment below with your SQL Query and then check our answer. We may get better and more ways of getting the result.
Hint: Try it via SQL Set Operators

10 thoughts on “Teradata SQL Test – 3”

  1. Select cust_Id, cust_number from customer
    Union
    Select cust_Id, cust_off_number from customer where cust_numbercust_off_number

  2. select cuse_id, (case when cust_num=cust_off_num then cust_num
    else cust_off_num end) as cust_num from customer

    union

    select cuse_id,cust_num from customer
    where cust_numcust_off_num

  3. rammohan bolineni

    select * from (
    select cust_id, case when cust_numbercust_office then cust_office else cust_number end cust_num from customer
    union
    select cust_id,cust_number from customer) A
    order by cust_id;

  4. Select cust_Id, cust_number from customer
    Union all
    Select cust_Id, cust_off_number from customer where cust_number cust_off_number

  5. WITH cust(cust_id,phone_no)
    as
    (select cust_id,
    case x
    when 1 then CUST_NUMBER
    when 2 then CUST_OFF_NUMBER
    end as phone_no
    from CUSTOMERS cross join
    (select day_of_calendar as x from sys_calendar.calendar
    where x between 1 and 2) as dt
    )
    sel distinct cust_id,phone_no from cust;

  6. SELECT * FROM

    (

    SELECT DISTINCT id, CUST_NUMBER FROM CUSTOMER

    UNION

    SELECT DISTINCT ID, CUST_OFF_NUMBER AS CUST_NUMBER FROM CUSTOMER

    )A

    ORDER BY ID ASC

Leave a Reply

Your email address will not be published. Required fields are marked *