Selecting records from an associated table

  • CREATE TABLE [dbo].[A](

    [ID] [smallint] NOT NULL,

    Name nvarchar(10),

    [AssocTableName] [nvarchar](50) NULL,

    data:

    1 TableType AssociatedTable1

    CREATE TABLE [dbo].[AssociatedTable1](

    [ID] [smallint] NOT NULL,

    [Desc] [nvarchar](80) NULL,

    data: 1 Supp 2 Sup2

    Is there any way i can join these two tables?

  • I don't see any relationship between these two tables.

    Is it that in your Table A, you are maintaining list of Tables

    and AssociatedTable1 is an another table for which there will be an entry in Table A.

    Is it correct?

  • The relationship stems through the Associated Table so the associated table has the corresponding values for the first table. Did that answer your question ?

  • SQLTestUser (9/12/2013)


    The relationship stems through the Associated Table so the associated table has the corresponding values for the first table. Did that answer your question ?

    Not really, it's too vague. Which columns match?


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • well none of the columns explicity match each other. the table A stores the name of the associated table name.

    [AssocTableName] [nvarchar](50) NULL stores the name for the associated table

    data:

    1 TableType AssociatedTable1

    CREATE TABLE [dbo].[AssociatedTable1](

    [ID] [smallint] NOT NULL,

    [Desc] [nvarchar](80) NULL,

    data: 1 Supp

    2 Sup2

    3 Sup3

    4 sup4

    the data for both is stored in a third table as

    CREATE TABLE [dbo].[FinalStore](

    [ID] [smallint] NOT NULL, -- this Id is from Table A

    [Desc] [nvarchar](80) NULL, -- this Desc is from the associated table

    as

    TableType Supp

  • Do you need to create the third table by querying first two tables?

  • no i am trying to connect these three table

  • What you have is a painful twist to an EAV type of system. This is going to be nothing but slow, tedious and nightmarish as the system gets larger.

    We can help you figure out how to query this but we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sample DDL in the form of CREATE TABLE statements

    CREATE TABLE [dbo].[FinalStore1](

    [AID] [smallint] NULL,

    [AssociatedTableID] [smallint] NULL

    ) ON [PRIMARY]

    CREATE TABLE [dbo].[StoreAssociatedTable1](

    [AID] [smallint] NULL,

    [AName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

    [AssociatedTableName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL

    ) ON [PRIMARY]

    CREATE TABLE [dbo].[AssociatedTable2](

    [AssociatedTableID] [smallint] NULL,

    [Desc] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL

    ) ON [PRIMARY]

    2. Sample data in the form of INSERT INTO statements

    INSERT [dbo].[AssociatedTable2] ([AssociatedTableID], [Desc]) VALUES (1, N'ThisExample')

    INSERT [dbo].[AssociatedTable2] ([AssociatedTableID], [Desc]) VALUES (2, N'ThisStore')

    INSERT [dbo].[StoreAssociatedTable1] ([AID], [AName], [AssociatedTableName])

    VALUES (1, N'StoreValues', N'AssociatedTable1')

    INSERT [dbo].[StoreAssociatedTable1] ([AID], [AName], [AssociatedTableName])

    VALUES (2, N'MerchantValues', N'AssociatedTable1')

    INSERT [dbo].[FinalStore1] ([AID], [AssociatedTableID]) VALUES (1, 1)

    3. Expected results based on the sample data

    A join that would show the values as:

    1)The values for StoreValues are ThisExample,ThisStore

Viewing 9 posts - 1 through 8 (of 8 total)

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