I have a stored procedure that is working well, it’s a select query. I want to change what is returned though with some aggregations, using the current query as a subquery. The current query returns data as such:
A 1/1/12
A 1/2/12
A 1/3/12
B 2/1/12
B 5/3/12
B 5/5/12
I’d like to see this as:
A 1/1/12 1/3/12
B 2/1/12 5/5/12
So I want the min of the date value, then the max of the date value, per stock symbol. Here is the current sproc that I’d like to stick with:
ALTER PROCEDURE dbo.sprocSymbsDatesForHistoricalPricingVol
AS
DECLARE @NxtAvailableDataDownloadDate date
SET @NxtAvailableDataDownloadDate = dbo.NextAvailableDataDownloadDate()
SELECT Symbol, TradingDate
FROM (SELECT tblSymbolsMain.Symbol, tblTradingDays.TradingDate
FROM tblSymbolsMain CROSS JOIN
tblTradingDays
WHERE (tblTradingDays.TradingDate <= @NxtAvailableDataDownloadDate)) AS T1
WHERE (NOT EXISTS
(SELECT TradeDate, Symbol
FROM tblDailyPricingAndVol
WHERE (TradeDate = T1.TradingDate) AND (Symbol = T1.Symbol)))
ORDER BY Symbol, TradingDate
My attempts to wrap that select query as a subquery aren’t working… any takers? I’m sure it’s quite simple. Thanks in advance…
1 Answer