This question is a continuation of the same effort to code my first cte that I mentioned in a couple other recent posts. In a nutshell, I’m working through a couple compiler errors for the query below. I’m now getting the error “Subquery returned more than one value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression.” But what I’ve come up with below seems “legal” to me based on my current abilities… any help would be great. BTW I either get that error, or Visual Studio 2010 shuts down when I try to run this…
WITH Symb AS
(
SELECT Symbol
FROM tblSymbolsMain
),
DatesNotNeeded AS
(
SELECT Date
FROM tblDailyPricingAndVol inner join Symb on
tblDailyPricingAndVol.Symbol = Symb.Symbol
),
WideDateRange AS
(
SELECT TradingDate
FROM tblTradingDays
WHERE (TradingDate >= dbo.NextAvailableDataDownloadDateTime()) AND (TradingDate <= dbo.LatestAvailableDataDownloadDateTime())
),
DatesNeeded AS
(
SELECT TradingDate
FROM WideDateRange wdr
WHERE NOT EXISTS (SELECT * FROM DatesNotNeeded d where d.Date = wdr.TradingDate)
)
SELECT Symb.Symbol, DatesNeeded.TradingDate
FROM Symb CROSS JOIN DatesNeeded
And my functions, as requested:
ALTER FUNCTION dbo.LatestAvailableDataDownloadDateTime()
RETURNS date
BEGIN
RETURN (SELECT DATEADD(hour, 18, MAX(TradingDate)) AS LatestTradingDateAvailForDL
FROM tblTradingDays
GROUP BY TradingDate
HAVING (DATEADD(hour, 18, MAX(TradingDate)) < GETDATE()))
END
ALTER FUNCTION dbo.NextAvailableDataDownloadDateTime()
RETURNS date
BEGIN
RETURN (SELECT DATEADD(hour, 18, MIN(TradingDate)) AS TrDate
FROM tblTradingDays
HAVING (DATEADD(hour, 18, MIN(TradingDate)) > dbo.LatestDataDownloadDate()))
END
Your functions are returning more than one value. Try this:
And: