Query Help

  • Hi All,

    I have 3 tables :

    CREATE TABLE [dbo].[T1] (

     [AddressListID] [bigint] IDENTITY (1, 1) NOT NULL ,

     [UserID] [varchar] (15) COLLATE Latin1_General_CI_AS NOT NULL ,

     [ToDisplayName] [varchar] (64) COLLATE Latin1_General_CI_AS NOT NULL ,

     [ToEmailAddress] [varchar] (320) COLLATE Latin1_General_CI_AS NOT NULL

    ) ON [PRIMARY]

    GO

    CREATE TABLE [dbo].[T2] (

     [AddressListID] [bigint] IDENTITY (1, 1) NOT NULL ,

     [ServicePackageCode] [varchar] (15) COLLATE Latin1_General_CI_AS NOT NULL ,

     [ToDisplayName] [varchar] (64) COLLATE Latin1_General_CI_AS NOT NULL ,

     [ToEmailAddress] [varchar] (320) COLLATE Latin1_General_CI_AS NOT NULL

    ) ON [PRIMARY]

    GO

    CREATE TABLE [dbo].[T3] (

     [AddressListID] [bigint] IDENTITY (1, 1) NOT NULL ,

     [ServiceTypeCode] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

     [ToDisplayName] [varchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

     [ToEmailAddress] [varchar] (320) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL

    ) ON [PRIMARY]

    GO

     

    I need to have a query that will return from all 3 tables the ToDisplayName, ToEmailAddress & AddressListID

    In the form of :

    DisplayName                      EmailAddress                                AddressListID

    T1.ToDisplayName                   T1.ToEmailAddress                       T1.AddressListID

    T2.ToDisplayName                   T2.ToEmailAddress                        T2.AddressListID

    T3.ToDisplayName                   T3.ToEmailAddress                        T3.AddressListID

     

    Any help would be greatly appreciated.

     

     

    Hope this helps...

    Ford Fairlane
    Rock and Roll Detective

  • Select T1.ToDisplayName  as DisplayName,                    T1.ToEmailAddress  as EmailAddress ,                      T1.AddressListID  as AddressListID

    FROM T1

    UNION

    Select T2.ToDisplayName  as DisplayName,                    T2.ToEmailAddress  as EmailAddress ,                      T2.AddressListID  as AddressListID

    FROM T2

    UNION

    Select T3.ToDisplayName  as DisplayName,                    T3.ToEmailAddress  as EmailAddress ,                      T3.AddressListID  as AddressListID

    FROM T3


    Regards,

    Meghana

  • Use UNION ALL if you want all records from each table, UNION is do the equivilent of a DISTINCT on the result set.

    --------------------------------- 

    Select T1.ToDisplayName  as DisplayName,                    T1.ToEmailAddress  as EmailAddress ,                      T1.AddressListID  as AddressListID

    FROM T1

    UNION ALL

    Select T2.ToDisplayName  as DisplayName,                    T2.ToEmailAddress  as EmailAddress ,                      T2.AddressListID  as AddressListID

    FROM T2

    UNION ALL

    Select T3.ToDisplayName  as DisplayName,                    T3.ToEmailAddress  as EmailAddress ,                      T3.AddressListID  as AddressListID

    FROM T3

  • Thanks both for your responces.

     

     

    Hope this helps...

    Ford Fairlane
    Rock and Roll Detective

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply