Joining a table to itself with the unique ID the same, Possible????

  • Hello,

    I'm hoping that SQL can do the below...

    Table Product_Options has been filtered by the ProductID, which results in the 2 OptionID's I need

    to filter Table Product_Options_Values...

    For easy explanation 962 is the parent and 963 is the Child in the Table Product_Options...

    Can a query be built by joining the 2 tables or Self Joining the Table Product_Options_Values, to produce the results down below.... ( )

    Thanks,

    John

    OptionID DateCreated ProductID OptionType

    962 52:44.4 6818 Size

    963 52:44.4 6818 Color

    ValueID DateCreated OptionID VendorSKU ShortDesc

    4019 52:43.1 962 NULL No Selection

    4020 52:43.1 962 NULL Adult- US-001-UA

    4021 52:43.1 962 NULL Pediatric- US-001-UP

    4022 52:43.1 963 NULL No Selection

    4023 52:43.1 963 NULL Orange-OR

    4024 52:43.1 963 NULL Hot Pink-HP

    4025 52:43.1 963 NULL Light Pink-LP

    4026 52:43.1 963 NULL Burgundy-BD

    4027 52:43.1 963 NULL Red-RD

    4028 52:43.1 963 NULL Yellow-YL

    4029 52:43.1 963 NULL Light Green-LG

    4030 52:43.1 963 NULL Green-GR

    4031 52:43.1 963 NULL Teal-TL

    4032 52:43.1 963 NULL Light Blue-LB

    4033 52:43.1 963 NULL Navy-NA

    OptionID ShortDesc OptionID ShortDesc

    962 No Selection 963 No Selection

    962 No Selection 963 Orange-OR

    962 No Selection 963 Hot Pink-HP

    962 No Selection 963 Light Pink-LP

    962 No Selection 963 Burgundy-BD

    962 No Selection 963 Red-RD

    962 No Selection 963 Yellow-YL

    962 No Selection 963 Light Green-LG

    962 No Selection 963 Green-GR

    962 No Selection 963 Teal-TL

    962 No Selection 963 Light Blue-LB

    962 No Selection 963 Navy-NA

    962 Adult- US-001-UA 963 No Selection

    962 Adult- US-001-UA 963 Orange-OR

    962 Adult- US-001-UA 963 Hot Pink-HP

    962 Adult- US-001-UA 963 Light Pink-LP

    962 Adult- US-001-UA 963 Burgundy-BD

    962 Adult- US-001-UA 963 Red-RD

    962 Adult- US-001-UA 963 Yellow-YL

    962 Adult- US-001-UA 963 Light Green-LG

    962 Adult- US-001-UA 963 Green-GR

    962 Adult- US-001-UA 963 Teal-TL

    962 Adult- US-001-UA 963 Light Blue-LB

    962 Adult- US-001-UA 963 Navy-NA

    962 Pediatric- US-001-UP 963 No Selection

    962 Pediatric- US-001-UP 963 Orange-OR

    962 Pediatric- US-001-UP 963 Hot Pink-HP

    962 Pediatric- US-001-UP 963 Light Pink-LP

    962 Pediatric- US-001-UP 963 Burgundy-BD

    962 Pediatric- US-001-UP 963 Red-RD

    962 Pediatric- US-001-UP 963 Yellow-YL

    962 Pediatric- US-001-UP 963 Light Green-LG

    962 Pediatric- US-001-UP 963 Green-GR

    962 Pediatric- US-001-UP 963 Teal-TL

    962 Pediatric- US-001-UP 963 Light Blue-LB

    962 Pediatric- US-001-UP 963 Navy-NA

  • Please, post proper DDL, sample data and expected results.

    See this article for reference:

    http://www.aspfaq.com/etiquette.asp?id=5006

    Anyway, what you're describing is a self join. In order to achieve that, you need to introduce aliases for the referenced objects.

    E.g.:

    use AdventureWorks

    select Employees.Title as EmployeeTitle

    ,Managers.Title as ManagerTitle

    from HumanResources.Employee Employees

    inner join HumanResources.Employee Managers

    on Managers.EmployeeID = Employees.ManagerID

    go

    ML

    ---
    Matija Lah, SQL Server MVP
    http://milambda.blogspot.com

Viewing 2 posts - 1 through 1 (of 1 total)

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