I have a table called TaxLots that was created with these columns:
CREATE TABLE Portfolio.TaxLots
(
Ticker varchar(22) NOT NULL,
SecurityDescription varchar(50) NOT NULL,
Class varchar(15) NULL,
Ccy varchar(5) NULL,
LSPosition char(3) NULL,
Date date NULL,
Quantity int NULL,
LocAvgCost decimal(8,3) NULL,
LocTaxCostBasis int NULL,
LocMktVal int NULL,
BaseAvgCost decimal(8,3) NULL,
BaseTaxCostBasis int NULL,
BaseMktVal int NULL,
BaseUNRL int NULL,
DateCreated DATE NOT NULL DEFAULT Cast(GetDate() as Date)
)
GO
I want to create a function that will pull up some of these columns for a specific Ticker (which would be my parameter) for the last DateCreated. I created the following function:
USE FundDB
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.GetTaxLots
(
@Ticker varchar
)
RETURNS TABLE
AS
RETURN
(
SELECT
TL.Ticker,
TL.SecurityDescription,
TL.LSPosition,
TL.Date,
TL.Quantity,
TL.LocAvgCost,
TL.LocTaxCostBasis,
TL.BaseAvgCost,
TL.BaseTaxCostBasis,
TL.DateCreated,
(SELECT SUM(Quantity)
FROM
Portfolio.TaxLots) AS TotalQuantity
FROM
Portfolio.TaxLots TL
WHERE
TL.DateCreated= (SELECT Max(TL.DateCreated) FROM Portfolio.TaxLots TL)
AND TL.Ticker = @Ticker
)
I tested the SELECT statement by itself with a Ticker hard-coded and it gives my the desired result but when I try to use the function using a SELECT statement like this:
USE FundDB
SELECT *
FROM dbo.GetTaxLots('MSFT')
GO
I get empty rows. Very confused. I'm pretty new at this so I feel like I'm missing something obvious.