Hi All,I have 2 tables. A Products Table and a Product Attributes Table.A simple Inner Join Brings back all the product with All Attributes. However what I want is only 1 row per SubID and only the values from the Minimum AttributeID rows from each SubID.Or in other words I want ONLY the Min AttributeID's per SubID in the rowset. There are 3 attribute SubID's total so only 3 rows returned. Sample Desired RowsetAttrID AttName AttValue1 Shirt 5555556 Collar ddffff9 Stripe ae97f2
Here is some data to create the tables.CREATE TABLE [dbo].[xTestProducts]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [Name] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Description] [varchar](8000) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Price] [int] NOT NULL CONSTRAINT [DF_xTestProducts_Price] DEFAULT ((0)), [Prestige] [int] NOT NULL CONSTRAINT [DF_xTestProducts_Prestige] DEFAULT ((0)), [Gender] [tinyint] NOT NULL CONSTRAINT [DF_xTestProducts_Gender] DEFAULT ((0)), [Active] [bit] NOT NULL CONSTRAINT [DF_xTestProducts_Active] DEFAULT ((1)), [Deleted] [bit] NOT NULL CONSTRAINT [DF_xTestProducts_Deleted] DEFAULT ((0)), [Inventory] [bigint] NOT NULL CONSTRAINT [DF_xTestProducts_Inventory] DEFAULT ((10000)), [CreateDate] [datetime] NOT NULL CONSTRAINT [DF_xTestProducts_CreateDate] DEFAULT (getdate()), CONSTRAINT [PK_xTestProducts] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOCREATE TABLE [dbo].[xTestProductAttributes]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [ProductID] [bigint] NOT NULL, [SubID] [bigint] NOT NULL, [Name] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Value] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Active] [bit] NOT NULL CONSTRAINT [DF_xTestProductAttributes_Active] DEFAULT ((1)), [Deleted] [bit] NOT NULL CONSTRAINT [DF_xTestProductAttributes_Deleted] DEFAULT ((0)), [CreateDate] [datetime] NOT NULL CONSTRAINT [DF_xTestProductAttributes_CreateDate] DEFAULT (getdate()), CONSTRAINT [PK_xTestProductAttributes] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GODeclare @ProductID bigint-- Insert into Products TableInsert Into xTestProducts (Name,Description,Price,Prestige,Gender)Values ('Polo Shirt','A very cheap Polo Shirt',1200,12,1)-- Get the IDSelect @ProductID = Scope_Identity()-- Insert into ProductAttributes TableInsert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,1,'Shirt','555555')Insert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,1,'Shirt','996633')Insert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,1,'Shirt','ff66ff')Insert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,1,'Shirt','6666cc')Insert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,1,'Shirt','ddffff')Insert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,2,'Collar','ddffff')Insert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,2,'Collar','ffffff')Insert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,2,'Collar','cccccc')Insert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,3,'Stripe','ae97f2')Insert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,3,'Stripe','dcdc06')Insert Into xTestProductAttributes(ProductID,SubID,Name,Value)Values(@ProductID,3,'Stripe','fefefe')Select *From xTestProductsSelect *From xTestProductAttributesSelect *From xTestProducts aInner Join xTestProductAttributes bOn a.ID = b.PRoductIDDrop Table xTestProductAttributesDrop Table xTestProducts
JBelthoff› As far as myself... I do this for fun!