I am developing a table that will contain the properties of some objects that will change over time.
CREATE TABLE [dbo].[ObjectProperties] ( [Id] INT NOT NULL PRIMARY KEY IDENTITY, [ObjectType] SMALLINT NOT NULL, [Width] SMALLINT NOT NULL, [Height] SMALLINT NOT NULL, [Weight] SMALLINT NOT NULL )
Let's say I have ObjectTypes: 1 = chair 2 = table
And the data for this table:
INSERT INTO [dbo].[ObjectProperties] ([Id], [ObjectType], [Width], [Height], [Weight]) VALUES (1, 1, 50, 50, 1000) INSERT INTO [dbo].[ObjectProperties] ([Id], [ObjectType], [Width], [Height], [Weight]) VALUES (2, 2, 80, 40, 500) INSERT INTO [dbo].[ObjectProperties] ([Id], [ObjectType], [Width], [Height], [Weight]) VALUES (3, 1, 50, 50, 2000)
So, as you can see, I had a Chair object, whose weight was 1000, then I changed the weight to 2000. And I keep something like a history of changes in the properties of objects. Now I want to select new data from this table for each object. I know how to select new data for each object one at a time:
SELECT TOP 1 * FROM [ObjectProperties] WHERE ObjectType = 1 ORDER BY Id DESC
But what if I want to select multiple objects with a single query? how
SELECT ... * FROM [ObjectProperties] WHERE ObjectType IN (1, 2) ...
And get the rows with identifiers 2 and 3 (because 3 has newer properties for the chair than 1)