初步的sql指令:
SELECT STOCKID,MIN((LEAST(CAST(OPEN AS DECIMAL(6,2)),CAST(HIGH AS
DECIMAL(6,2)),CAST(LOW AS DECIMAL(6,2)),CAST(close AS DECIMAL(6,2))))) AS
max_value FROM yahoo_finance.table_price WHERE STOCKID IN ('6807', '6854', '1101', '2067',
'2069', '2070', '2101', '2102', '2103' ) and DATE BETWEEN '20240731' AND '20241011' GROUP
BY STOCKID
進階:
最近想要增加查詢日期,但查詢的日期,希望是該指令查詢後的結果對應的日期,不想查詢兩次,無奈sql修練不夠,那就上網請大神幫忙,誤。以前有論壇可以留言,現在都要靠ai了,來試試勘。
來做:
WITH max_value_data AS (
SELECT STOCKID, MIN((LEAST(CAST(OPEN AS DECIMAL(6,2)),
CAST(HIGH AS DECIMAL(6,2)),
CAST(LOW AS DECIMAL(6,2)),
CAST(close AS DECIMAL(6,2))))) AS max_value
FROM yahoo_finance.table_price
WHERE STOCKID IN ('6807', '6854', '1101', '2067', '2069', '2070', '2101', '2102', '2103')
AND DATE BETWEEN '20240731' AND '20241010'
GROUP BY STOCKID
)
SELECT DISTINCT a.STOCKID, a.DATE, b.max_value
FROM yahoo_finance.table_price a
JOIN max_value_data b ON a.STOCKID = b.STOCKID
WHERE (LEAST(CAST(a.OPEN AS DECIMAL(6,2)),
CAST(a.HIGH AS DECIMAL(6,2)),
CAST(a.LOW AS DECIMAL(6,2)),
CAST(a.close AS DECIMAL(6,2))) = b.max_value)
AND a.DATE BETWEEN '20240731' AND '20241010'
ORDER BY a.STOCKID, a.DATE;
沒有留言:
張貼留言