Question

in sql i have a table of items. An item can have multiples prices. But when i come to using the data, most of the time i only need the currentPrice that i can find with the date.

My question : Would it be wrong to have 2 relation on price where and Item would have a relation with the currentPrice and all the price. Do there is a solution for this kind of problem to prevent complication in my mvc. Im currently using a viewModel with and the current price but if i could handle this in the database it seem better to me. (tell me if its not).

Note: I don't think this exact methode would work since I would need to create my price table before my item table and the same for the item table. But this show my problem.

Thanks alot for your help.

CREATE TABLE Item.Items
(
    ItemId INT NOT NULL 
        CONSTRAINT PK_Items_ItemId 
        PRIMARY KEY
        IDENTITY,
    --------------------------------------------------
    --CurrentPriceId INT NOT NULL
        CONSTRAINT FK_Prices_Items
        FOREIGN KEY REFERENCES Item.Items (ItemId),
    --------------------------------------------------
    Name VARCHAR(100) NOT NULL,
    Points INT NOT NULL DEFAULT 0,
    Description VARCHAR(5000) NULL
)

CREATE TABLE Item.Prices
(
    PriceId INT NOT NULL 
        CONSTRAINT PK_ItemPrices_ItemPriceId
        PRIMARY KEY
        IDENTITY,
    ItemId INT NOT NULL
        CONSTRAINT FK_ItemPrices_Item
        FOREIGN KEY REFERENCES Item.Items (ItemId),
    EffectiveDate DATETIME NOT NULL,
    Value MONEY NOT NULL
)
Was it helpful?

Solution

better would be:

CREATE TABLE Item.Items
(
  ItemId INT NOT NULL 
      CONSTRAINT PK_Items_ItemId 
      PRIMARY KEY IDENTITY,
  --------------------------------------------------
  CurrentPrice MONEY NOT NULL,  -- optional optimization, redundant as
                                -- current price is available from item.Prices
  --------------------------------------------------
  Name VARCHAR(100) NOT NULL,
  Points INT NOT NULL DEFAULT 0,
  Description VARCHAR(5000) NULL
)

CREATE TABLE Item.Prices
(
   ItemId INT NOT NULL
      CONSTRAINT FK_ItemPrices_Item
      FOREIGN KEY REFERENCES Item.Items (ItemId),
   EffectiveUtc DATETIME NOT NULL,
   Price MONEY NOT NULL,
   Constraint [ItemPricesPK] PRIMARY KEY CLUSTERED 
   (
      [ItemId] ASC,
      [EffectiveUtc] ASC
   )
)

As mentioned by @popovitz, you can always get the price as of any specific date using a correlated subquery ...

Select i.name, p.price 
From Item.Items i
   Join Item.Prices p 
      ON i.ItemId = p.ItemId
Where p.EffectiveUtc = 
   (Select Max(EffectiveUtc ) 
    From Item.Prices
    Where ItemId = i.ItemId 
       And EffectiveUtc <= @asOfDate)

OTHER TIPS

I would not recommend adding a currentPrice column, because you would have to constantly make sure that the item table is being updated whenever there a new price becomes effective. It's not that hard to query the current price when you have a table that contains the prices with an EffectiveDate column:

SELECT i.name, p.price FROM Items i
INNER JOIN Prices p ON i.ItemId = p.ItemId
WHERE p.EffectiveDate = 
     (SELECT MAX(EffectiveDate) FROM Price
     WHERE EffectiveDate <= SYSDATE
     AND ItemId = i.ItemId)

This will select the correct price for the current system time, assuming that (EffectiveDate, ItemId) is unique for the table prices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top