Im trying to create the derived attribute availability in the item table, in order to do that do i need to create a join?
Availability Checks if todays date is between checkoutDate and returndate.
Here is the ER diagram:
Im trying to create the derived attribute availability in the item table, in order to do that do i need to create a join?
Availability Checks if todays date is between checkoutDate and returndate.
Here is the ER diagram:
If you want to use this as a computed column, then I would recommend using a UDF. In your diagram, you have Availability as a boolean. SQL Server does not have a boolean data type, so I assume you mean a bit. This should be pretty close, but you'd need to test it:
CREATE FUNCTION dbo.GetValue
(
@barcode int
)
RETURNS bit
AS
BEGIN
DECLARE @field bit
SELECT @field = CASE WHEN GETDATE()
BETWEEN L.LastReturnDate AND R.ReturnDate THEN 1 ELSE 0 END
FROM [Item] I
JOIN [Load] L ON I.barcode = L.barcode
JOIN [Return] R ON I.barcode = R.barcode
WHERE I.barcode = @barcode
RETURN @field
END
GO
And then using that as your computed column:
ALTER TABLE dbo.Item
ADD Availability AS dbo.GetValue(barcode)
With that said, this might be better suited for a View.
Yes, you need to do a join.
I would recommend that you use a view to put together the data from the different tables.