Output  hist_qry2.sql

Appendix A: SQL Scripts for Sample Queries

SQL

The following script contains the SQL statements for this query.

-- Get the closing price of a set of 10 stocks for a 10-year period and
-- group into weekly, monthly and yearly aggregates.
-- For each aggregate period determine the low, high and average closing
-- price value.
-- The output should be sorted by INSTRUMENT_ID and trade date.

commit
;

SELECT sh.TRADING_SYMBOL,
DATEPART(yy,sh.TRADE_DATE) AS YEAR,
DATEPART(mm,sh.TRADE_DATE) AS MON,
DATEPART(wk,sh.TRADE_DATE) AS WEEK, 
MAX(sh.CLOSE_PRICE) AS MAX_PRICE,
MIN(sh.CLOSE_PRICE) AS MIN_PRICE,
AVG(sh.CLOSE_PRICE) AS AVG_PRICE
FROM  STOCK_HISTORY sh 
WHERE
sh.TRADE_DATE BETWEEN '2005-02-08'
	and '2015-02-07'
and sh.TRADING_SYMBOL in ('AAA','AAB','AAC','AAD','AAE','AAF','AAG','AAH','AAI','AAJ' )
GROUP BY ROLLUP ( sh.TRADING_SYMBOL,
DATEPART(yy,sh.TRADE_DATE),
	 DATEPART(mm,sh.TRADE_DATE),
DATEPART(wk,sh.TRADE_DATE))
;




Copyright © 2005. Sybase Inc. All rights reserved. hist_qry2.sql

View this book as PDF