SQL Query
Results 1 to 2 of 2

Thread: SQL Query

  1. #1
    Join Date
    Jan 2009
    Posts
    5

    SQL Query

    Hello,

    I need your help in the following scenario.

    I have customer information in two tables, ProductA_Customer and ProductB_Customer.
    I am trying to create a report that would include all Customers from ProductA_Customer
    and customers from ProductB_Customer that have no record in ProductA_Customer.

    Here is the table structure,

    CREATE TABLE ProductA_Customer
    (
    Cust_No_A char(13) NOT NULL,
    Cust_Name_A varchar(60) NULL,
    Cust_Address_A varchar(60) NULL,
    Cust_City_A varchar(20) NULL,
    Cust_State_A char(2) NULL,
    Cust_Zip_A char(10) NULL
    )

    CREATE UNIQUE CLUSTERED INDEX XPKProductA_Customer ON ProductA_Customer
    (
    Cust_No_A
    )


    CREATE TABLE ProductB_Customer
    (
    Cust_No_B char(13) NOT NULL,
    Cust_Name_B varchar(60) NULL,
    Cust_Address_B varchar(60) NULL,
    Cust_City_B varchar(20) NULL,
    Cust_State_B char(2) NULL,
    Cust_Zip_B char(10) NULL
    )

    CREATE UNIQUE CLUSTERED INDEX XPKProductB_Customer ON ProductB_Customer
    (
    Cust_No_B
    )


    The result needs to be formatted in the following table

    Cust_No_A
    Cust_Name_A
    Cust_Address_A
    Cust_City_A
    Cust_State_A
    Cust_Zip_A
    Cust_No_B
    Cust_Name_B
    Cust_Address_B
    Cust_City_B
    Cust_State_B
    Cust_Zip_B

    We are using SQL Server 2005. I would appreciare your help

    Thanks

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,861
    You can use UNION of two SELECTs

    select
    Cust_No_A
    Cust_Name_A
    Cust_Address_A
    Cust_City_A
    Cust_State_A
    Cust_Zip_A
    from customer_a
    UNION
    select
    Cust_No_B
    Cust_Name_B
    Cust_Address_B
    Cust_City_B
    Cust_State_B
    Cust_Zip_B
    from customer_B

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •