CREATE DEFINER=`pmaadmin`@`localhost` EVENT `cleanup_upbit_Ticker_user` ON SCHEDULE EVERY 1 HOUR STARTS '2026-01-19 12:10:00' ON COMPLETION PRESERVE ENABLE COMMENT '자산_개인자산 25시간 보유 이후 데이터 1시간 단위 삭제_삭제' DO DELETE FROM daemon_bybit_coin_1s 
WHERE collected_at < NOW() - INTERVAL 25 HOUR;

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_bybit_coin_12h` ON SCHEDULE EVERY 12 HOUR STARTS '2026-02-08 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_바이비트 플랫폼 테이블을 12시간 단위 테이블로 복사_복사' DO INSERT INTO daemon_bybit_coin_12h (
  symbol, korean_name, `time`,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade, mPnL, isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
)
SELECT
  symbol, korean_name,
  UNIX_TIMESTAMP(NOW(3))*1000,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade,
  NULLIF(mPnL, ''),
  isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
FROM daemon_bybit_Ticker;

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_bybit_coin_15m` ON SCHEDULE EVERY 15 MINUTE STARTS '2026-02-08 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_바이비트 플랫폼 테이블을 15분 단위 테이블 복사_복사' DO INSERT INTO daemon_bybit_coin_15m (
  symbol, korean_name, `time`,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade, mPnL, isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
)
SELECT
  symbol, korean_name,
  UNIX_TIMESTAMP(NOW(3))*1000,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade,
  NULLIF(mPnL, ''),
  isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
FROM daemon_bybit_Ticker;

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_bybit_coin_1h` ON SCHEDULE EVERY 1 HOUR STARTS '2026-02-08 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_바이비트 플랫폼 테이블을 1시간 단위 테이블 복사_복사' DO INSERT INTO daemon_bybit_coin_1h (
  symbol, korean_name, `time`,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade, mPnL, isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
)
SELECT
  symbol, korean_name,
  UNIX_TIMESTAMP(NOW(3))*1000,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade,
  NULLIF(mPnL, ''),
  isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
FROM daemon_bybit_Ticker;

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_bybit_coin_1m` ON SCHEDULE EVERY 1 MINUTE STARTS '2026-02-08 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_바이비트 플랫폼 테이블을 1분 단위 테이블로 복사_복사' DO INSERT INTO daemon_bybit_coin_1m (
  symbol, korean_name, `time`,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade, mPnL, isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
)
SELECT
  symbol, korean_name,
  UNIX_TIMESTAMP(NOW(3))*1000,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade,
  NULLIF(mPnL, ''),
  isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
FROM daemon_bybit_Ticker;

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_bybit_coin_24h` ON SCHEDULE EVERY 24 HOUR STARTS '2026-02-08 08:59:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_바이비트 플랫폼 테이블을 24시간 단위 테이블 복사_복사' DO INSERT INTO daemon_bybit_coin_24h (
  symbol, korean_name, `time`,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade, mPnL, isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
)
SELECT
  symbol, korean_name,
  UNIX_TIMESTAMP(NOW(3))*1000,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade,
  NULLIF(mPnL, ''),
  isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
FROM daemon_bybit_Ticker;

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_bybit_coin_30m` ON SCHEDULE EVERY 30 MINUTE STARTS '2026-02-08 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_바이비트 플랫폼 테이블을 30분 단위 테이블 복사_복사' DO INSERT INTO daemon_bybit_coin_30m (
  symbol, korean_name, `time`,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade, mPnL, isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
)
SELECT
  symbol, korean_name,
  UNIX_TIMESTAMP(NOW(3))*1000,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade,
  NULLIF(mPnL, ''),
  isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
FROM daemon_bybit_Ticker;

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_bybit_coin_4h` ON SCHEDULE EVERY 4 HOUR STARTS '2026-02-08 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_바이비트 플랫폼 테이블을 4시간 단위 테이블 복사_복사' DO INSERT INTO daemon_bybit_coin_4h (
  symbol, korean_name, `time`,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade, mPnL, isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
)
SELECT
  symbol, korean_name,
  UNIX_TIMESTAMP(NOW(3))*1000,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade,
  NULLIF(mPnL, ''),
  isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
FROM daemon_bybit_Ticker;

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_bybit_coin_5m` ON SCHEDULE EVERY 5 MINUTE STARTS '2026-02-08 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_바이비트 플랫폼 테이블을 5분 단위 테이블 복사_복사' DO INSERT INTO daemon_bybit_coin_5m (
  symbol, korean_name, `time`,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade, mPnL, isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
)
SELECT
  symbol, korean_name,
  UNIX_TIMESTAMP(NOW(3))*1000,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade,
  NULLIF(mPnL, ''),
  isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
FROM daemon_bybit_Ticker;

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_bybit_coin_8h` ON SCHEDULE EVERY 8 HOUR STARTS '2026-02-08 08:59:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_바이비트 플랫폼 테이블을 8시간 단위 펀딩 테이블로 복사_복사' DO INSERT INTO daemon_bybit_coin_8h (
  symbol, korean_name, `time`,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade, mPnL, isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
)
SELECT
  symbol, korean_name,
  UNIX_TIMESTAMP(NOW(3))*1000,
  status, baseCoin, quoteCoin, settleCoin, optionsType,
  launchTime, priceScale,
  minLeverage, maxLeverage, leverageStep,
  minPrice, maxPrice, tickSize,
  maxOrderQty, minOrderQty, qtyStep, postOnlyMaxOrderQty,
  unifiedMarginTrade, fundingInterval, copyTrading,
  upperLimitPrice, lowerLimitPrice,
  lastPrice, indexPrice, markPrice,
  prevPrice24h, price24hPcnt, highPrice24h, lowPrice24h, prevPrice1h,
  openInterest, openInterestValue,
  turnover24h, volume24h,
  fundingRate, nextFundingTime,
  ask1Size, bid1Price, ask1Price, bid1Size,
  execId, price, size, side, isAdlTrade,
  NULLIF(mPnL, ''),
  isBlockTrade,
  start, `open`, high, low, `close`, volume, turnover,
  m_close, i_close, p_close,
  buyRatio, sellRatio, period,
  riskId, isLowestRisk, maintenanceMargin, initialMargin, `limit`
FROM daemon_bybit_Ticker;

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_cleanup_candle_1m_except_core7` ON SCHEDULE EVERY 1 MINUTE STARTS '2026-01-19 12:10:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_1분 단위 데이터 수집 : 24시간 이후 데이터는 1시간 단위 삭제한다._삭제' DO DELETE FROM daemon_upbit_coin_1m
WHERE timestamp < (NOW() - INTERVAL 25 HOUR)
  AND market NOT IN (
    'KRW-BTC',
    'KRW-ETH',
    'KRW-XRP',
    'KRW-ADA',
    'KRW-SOL',
    'KRW-DOGE',
    'KRW-BCH'
  )

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_cleanup_candle_1s_except` ON SCHEDULE EVERY 1 MINUTE STARTS '2026-01-19 12:10:00' ON COMPLETION PRESERVE ENABLE COMMENT '플랫폼_1초 단위 데이터 수집 : 24시간 이후 데이터는 1시간 단위 삭제한다._삭제' DO DELETE FROM upbit_data.daemon_upbit_coin_1s 
WHERE collected_at < (NOW() - INTERVAL 25 HOUR)

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_cleanup_candle_5m_except_core7` ON SCHEDULE EVERY 1 MINUTE STARTS '2026-01-19 12:10:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_5분 단위 데이터 수집 : 24시간 이후 데이터는 1시간 단위 삭제한다._삭제' DO DELETE FROM daemon_upbit_coin_5m
WHERE timestamp < (NOW() - INTERVAL 25 HOUR)
  AND market NOT IN (
    'KRW-BTC',
    'KRW-ETH',
    'KRW-XRP',
    'KRW-ADA',
    'KRW-SOL',
    'KRW-DOGE',
    'KRW-BCH'
  )

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_coin_12h` ON SCHEDULE EVERY 12 HOUR STARTS '2026-01-19 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '플랫폼_플랫폼 테이블을 12시간 단위 테이블로 복사_복사' DO INSERT INTO upbit_data.daemon_upbit_coin_12h (
  id,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
)
SELECT
  NULL,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
FROM upbit_data.daemon_upbit_Ticker

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_coin_15m` ON SCHEDULE EVERY 15 MINUTE STARTS '2026-01-19 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '플랫폼_플랫폼 테이블을 15분 단위 테이블로 복사_복사' DO INSERT INTO upbit_data.daemon_upbit_coin_15m (
  id,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
)
SELECT
  NULL,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
FROM upbit_data.daemon_upbit_Ticker

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_coin_1h` ON SCHEDULE EVERY 1 HOUR STARTS '2026-01-19 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '플랫폼_플랫폼 테이블을 1시간 단위 테이블로 복사_복사' DO INSERT INTO upbit_data.daemon_upbit_coin_1h (
  id,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
)
SELECT
  NULL,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
FROM upbit_data.daemon_upbit_Ticker

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_coin_1m` ON SCHEDULE EVERY 1 MINUTE STARTS '2026-01-19 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '플랫폼_플랫폼 테이블을 1분 단위 테이블로 복사_복사' DO INSERT INTO upbit_data.daemon_upbit_coin_1m (
  id,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
)
SELECT
  NULL,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
FROM upbit_data.daemon_upbit_Ticker

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_coin_1s` ON SCHEDULE EVERY 1 SECOND STARTS '2026-02-12 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '플랫폼_플랫폼 테이블을 1초 단위 테이블로 복사_복사' DO -- 이벤트 정의(SQL 탭에 그대로): 전종목(daemon_upbit_Ticker 전부) → 1초 원장(daemon_upbit_coin_1s) 복사

INSERT INTO upbit_data.daemon_upbit_coin_1s (
  id,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
)
SELECT
  NULL,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
FROM upbit_data.daemon_upbit_Ticker

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_coin_24h` ON SCHEDULE EVERY 24 HOUR STARTS '2026-01-20 08:59:00' ON COMPLETION PRESERVE ENABLE COMMENT '플랫폼_플랫폼 테이블을 24시간 단위 테이블로 복사_복사' DO INSERT INTO upbit_data.daemon_upbit_coin_24h (
  id,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
)
SELECT
  NULL,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
FROM upbit_data.daemon_upbit_Ticker

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_coin_30m` ON SCHEDULE EVERY 30 MINUTE STARTS '2026-01-19 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '플랫폼_플랫폼 테이블을 30분 단위 테이블로 복사_복사' DO INSERT INTO upbit_data.daemon_upbit_coin_30m (
  id,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
)
SELECT
  NULL,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
FROM upbit_data.daemon_upbit_Ticker

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_coin_4h` ON SCHEDULE EVERY 4 HOUR STARTS '2026-01-19 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '플랫폼_플랫폼 테이블을 4시간 단위 테이블로복사_복사' DO INSERT INTO upbit_data.daemon_upbit_coin_4h (
  id,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
)
SELECT
  NULL,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
FROM upbit_data.daemon_upbit_Ticker

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_coin_5m` ON SCHEDULE EVERY 5 MINUTE STARTS '2026-01-19 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '플랫폼_플랫폼 테이블을 5분 단위 테이블 복사_복사' DO INSERT INTO upbit_data.daemon_upbit_coin_5m (
  id,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
)
SELECT
  NULL,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
FROM upbit_data.daemon_upbit_Ticker

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_coin_8h` ON SCHEDULE EVERY 8 HOUR STARTS '2026-01-19 08:59:00' ON COMPLETION PRESERVE ENABLE COMMENT '플랫폼_플랫폼 테이블을 8시간 단위 펀딩 테이블로 복사_복사' DO INSERT INTO upbit_data.daemon_upbit_coin_8h (
  id,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
)
SELECT
  NULL,
  market,
  trade_date,
  trade_time,
  trade_date_kst,
  trade_time_kst,
  opening_price,
  high_price,
  low_price,
  trade_price,
  prev_closing_price,
  `change`,
  change_price,
  change_rate,
  signed_change_price,
  signed_change_rate,
  trade_volume,
  acc_trade_volume,
  acc_trade_volume_24h,
  acc_trade_price,
  acc_trade_price_24h,
  highest_52_week_price,
  highest_52_week_date,
  lowest_52_week_price,
  lowest_52_week_date,
  collected_at,
  collected_ms,
  ob_timestamp,
  ob_total_ask_size,
  ob_total_bid_size,
  ob_units,
  ob_collected_at,
  ob_collected_ms,
  tr_trade_timestamp,
  tr_trade_price,
  tr_trade_volume,
  tr_ask_bid,
  tr_trade_date_utc,
  tr_trade_time_utc,
  tr_trade_date_kst,
  tr_trade_time_kst,
  tr_collected_at,
  tr_collected_ms,
  day_of_week,
  korean_name
FROM upbit_data.daemon_upbit_Ticker

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_user_1h` ON SCHEDULE EVERY 1 HOUR STARTS '2026-02-26 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '개인키_플랫폼 테이블을 1시간 단위 테이블로 복사_복사' DO INSERT INTO daemon_upbit_user_1h
  (currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance)
  SELECT
   currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance
  FROM daemon_upbit_Ticker_user
  ON DUPLICATE KEY UPDATE
   balance=VALUES(balance),
   locked=VALUES(locked),
   avg_buy_price=VALUES(avg_buy_price),
   avg_buy_price_modified=VALUES(avg_buy_price_modified),
   unit_currency=VALUES(unit_currency),
   collected_at=VALUES(collected_at),
   current_price=VALUES(current_price),
   evaluation_amount=VALUES(evaluation_amount),
   buy_amount=VALUES(buy_amount),
   profit_amount=VALUES(profit_amount),
   profit_rate=VALUES(profit_rate),
   total_asset_value=VALUES(total_asset_value),
   total_profit_amount=VALUES(total_profit_amount),
   total_profit_rate=VALUES(total_profit_rate),
   cash_balance=VALUES(cash_balance)

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_user_1m` ON SCHEDULE EVERY 1 MINUTE STARTS '2026-02-26 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '개인키_플랫폼 테이블을 1분 단위 테이블로 복사_복사' DO INSERT INTO daemon_upbit_user_1m
  (currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance)
  SELECT
   currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance
  FROM daemon_upbit_Ticker_user
  ON DUPLICATE KEY UPDATE
   balance=VALUES(balance),
   locked=VALUES(locked),
   avg_buy_price=VALUES(avg_buy_price),
   avg_buy_price_modified=VALUES(avg_buy_price_modified),
   unit_currency=VALUES(unit_currency),
   collected_at=VALUES(collected_at),
   current_price=VALUES(current_price),
   evaluation_amount=VALUES(evaluation_amount),
   buy_amount=VALUES(buy_amount),
   profit_amount=VALUES(profit_amount),
   profit_rate=VALUES(profit_rate),
   total_asset_value=VALUES(total_asset_value),
   total_profit_amount=VALUES(total_profit_amount),
   total_profit_rate=VALUES(total_profit_rate),
   cash_balance=VALUES(cash_balance)

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_user_24h` ON SCHEDULE EVERY 24 HOUR STARTS '2026-02-26 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '개인키_플랫폼 테이블을 24시간 단위 테이블로 복사_복사' DO INSERT INTO daemon_upbit_user_24h
  (currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance)
  SELECT
   currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance
  FROM daemon_upbit_Ticker_user
  ON DUPLICATE KEY UPDATE
   balance=VALUES(balance),
   locked=VALUES(locked),
   avg_buy_price=VALUES(avg_buy_price),
   avg_buy_price_modified=VALUES(avg_buy_price_modified),
   unit_currency=VALUES(unit_currency),
   collected_at=VALUES(collected_at),
   current_price=VALUES(current_price),
   evaluation_amount=VALUES(evaluation_amount),
   buy_amount=VALUES(buy_amount),
   profit_amount=VALUES(profit_amount),
   profit_rate=VALUES(profit_rate),
   total_asset_value=VALUES(total_asset_value),
   total_profit_amount=VALUES(total_profit_amount),
   total_profit_rate=VALUES(total_profit_rate),
   cash_balance=VALUES(cash_balance)

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_user_30m` ON SCHEDULE EVERY 30 MINUTE STARTS '2026-02-26 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '개인키_플랫폼 테이블을 30분 단위 테이블로 복사_복사' DO INSERT INTO daemon_upbit_user_30m
  (currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance)
  SELECT
   currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance
  FROM daemon_upbit_Ticker_user
  ON DUPLICATE KEY UPDATE
   balance=VALUES(balance),
   locked=VALUES(locked),
   avg_buy_price=VALUES(avg_buy_price),
   avg_buy_price_modified=VALUES(avg_buy_price_modified),
   unit_currency=VALUES(unit_currency),
   collected_at=VALUES(collected_at),
   current_price=VALUES(current_price),
   evaluation_amount=VALUES(evaluation_amount),
   buy_amount=VALUES(buy_amount),
   profit_amount=VALUES(profit_amount),
   profit_rate=VALUES(profit_rate),
   total_asset_value=VALUES(total_asset_value),
   total_profit_amount=VALUES(total_profit_amount),
   total_profit_rate=VALUES(total_profit_rate),
   cash_balance=VALUES(cash_balance)

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_user_4h` ON SCHEDULE EVERY 4 HOUR STARTS '2026-02-26 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '개인키_플랫폼 테이블을 4시간 단위 테이블로 복사_복사' DO INSERT INTO daemon_upbit_user_4h
  (currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance)
  SELECT
   currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance
  FROM daemon_upbit_Ticker_user
  ON DUPLICATE KEY UPDATE
   balance=VALUES(balance),
   locked=VALUES(locked),
   avg_buy_price=VALUES(avg_buy_price),
   avg_buy_price_modified=VALUES(avg_buy_price_modified),
   unit_currency=VALUES(unit_currency),
   collected_at=VALUES(collected_at),
   current_price=VALUES(current_price),
   evaluation_amount=VALUES(evaluation_amount),
   buy_amount=VALUES(buy_amount),
   profit_amount=VALUES(profit_amount),
   profit_rate=VALUES(profit_rate),
   total_asset_value=VALUES(total_asset_value),
   total_profit_amount=VALUES(total_profit_amount),
   total_profit_rate=VALUES(total_profit_rate),
   cash_balance=VALUES(cash_balance)

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `ev_upbit_user_5m` ON SCHEDULE EVERY 5 MINUTE STARTS '2026-02-26 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '개인키_플랫폼 테이블을 5분 단위 테이블로 복사_복사' DO INSERT INTO daemon_upbit_user_5m
  (currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance)
  SELECT
   currency, balance, locked, avg_buy_price, avg_buy_price_modified, unit_currency,
   collected_at, collected_ms, current_price, evaluation_amount, buy_amount,
   profit_amount, profit_rate, total_asset_value, total_profit_amount,
   total_profit_rate, cash_balance
  FROM daemon_upbit_Ticker_user
  ON DUPLICATE KEY UPDATE
   balance=VALUES(balance),
   locked=VALUES(locked),
   avg_buy_price=VALUES(avg_buy_price),
   avg_buy_price_modified=VALUES(avg_buy_price_modified),
   unit_currency=VALUES(unit_currency),
   collected_at=VALUES(collected_at),
   current_price=VALUES(current_price),
   evaluation_amount=VALUES(evaluation_amount),
   buy_amount=VALUES(buy_amount),
   profit_amount=VALUES(profit_amount),
   profit_rate=VALUES(profit_rate),
   total_asset_value=VALUES(total_asset_value),
   total_profit_amount=VALUES(total_profit_amount),
   total_profit_rate=VALUES(total_profit_rate),
   cash_balance=VALUES(cash_balance)

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `stats_bybit` ON SCHEDULE EVERY 1 SECOND STARTS '2026-02-09 22:51:23' ON COMPLETION NOT PRESERVE ENABLE COMMENT '통계_통계 플랫폼에 바이비트 통계값을 넣는다_실시간' DO INSERT INTO stats_direction_01 (
    market, 
    collected_at, 
    fundingRate, 
    nextFundingTime, 
    openInterest, 
    openInterestValue, 
    markPrice, 
    indexPrice, 
    fxRate, 
    bybit_change_rate, 
    bybit_current_price, 
    current_price
)
SELECT 
    t.symbol, 
    t.collected_at, 
    t.fundingRate, 
    t.nextFundingTime, 
    t.openInterest, 
    t.openInterestValue, 
    t.markPrice, 
    t.indexPrice, 
    t.fxRate, 
    t.price24hPcnt, 
    t.lastPrice,
    t.lastPrice 
FROM daemon_bybit_Ticker AS t
-- [핵심] stats_direction_01에 이미 존재하는 market만 조인 (필터링)
INNER JOIN stats_direction_01 AS s ON t.symbol = s.market
ON DUPLICATE KEY UPDATE
    collected_at = VALUES(collected_at),
    fundingRate = VALUES(fundingRate),
    nextFundingTime = VALUES(nextFundingTime),
    openInterest = VALUES(openInterest),
    openInterestValue = VALUES(openInterestValue),
    markPrice = VALUES(markPrice),
    indexPrice = VALUES(indexPrice),
    fxRate = VALUES(fxRate),
    bybit_change_rate = VALUES(bybit_change_rate),
    bybit_current_price = VALUES(bybit_current_price),
    current_price = VALUES(current_price)

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `stats_direction_01_15m` ON SCHEDULE EVERY 15 MINUTE STARTS '2026-02-02 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '동계_방향 통계 실시간을 15분 테이블에 실시간 복사_복사' DO -- [15m] 원장(stats_direction_01) → 누적(stats_direction_01_15m)
-- side_plus_cnt/side_minus_cnt : Δ side_total_cnt
-- chg_dir : Δ chg_dir_sum
-- streak(등락 연속) : chg_dir 기준으로 15m 테이블 "직전값"으로 계산
INSERT INTO upbit_data.stats_direction_01_15m (
  market, wr_datetime,
  side_plus, side_minus, side_total,
  side_plus_cnt, side_minus_cnt, side_total_cnt,
  side_avg, acc_bid_volume, acc_ask_volume, net_vol,
  trade_price, change_rate,
  chg_dir, chg_dir_sum,
  chg_up_streak, chg_down_streak,
  chg_up_streak_max, chg_down_streak_max,
  chg_rate_avg, chg_rate_avg_cnt
)
SELECT
  s.market,
  s.wr_datetime,
  s.side_plus, s.side_minus, s.side_total,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) > 0 THEN 1 ELSE 0 END AS side_plus_cnt,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) < 0 THEN -1 ELSE 0 END AS side_minus_cnt,
  s.side_total_cnt,
  s.side_avg, s.acc_bid_volume, s.acc_ask_volume, s.net_vol,
  s.trade_price, s.change_rate,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN  1
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN -1
    ELSE 0
  END AS chg_dir,
  s.chg_dir_sum,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
    ELSE 0
  END AS chg_up_streak,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
    ELSE 0
  END AS chg_down_streak,
  GREATEST(
    IFNULL(pm.chg_up_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_up_streak_max,
  GREATEST(
    IFNULL(pm.chg_down_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_down_streak_max,
  s.chg_rate_avg, s.chg_rate_avg_cnt
FROM upbit_data.stats_direction_01 s
LEFT JOIN (
  SELECT market, MAX(wr_id) AS max_id
  FROM upbit_data.stats_direction_01_15m
  GROUP BY market
) last ON last.market = s.market
LEFT JOIN upbit_data.stats_direction_01_15m pm
  ON pm.market = last.market AND pm.wr_id = last.max_id
WHERE s.wr_datetime >= NOW() - INTERVAL 30 MINUTE
  AND NOT EXISTS (
    SELECT 1
    FROM upbit_data.stats_direction_01_15m m
    WHERE m.market = s.market
      AND m.wr_datetime = s.wr_datetime
  )

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `stats_direction_01_1h` ON SCHEDULE EVERY 1 HOUR STARTS '2026-02-02 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '방향_방향 통계 실시간을 1시간 테이블에 실시간 복사_복사' DO -- [1h] 원장(stats_direction_01) → 누적(stats_direction_01_1h)
-- side_plus_cnt/side_minus_cnt : Δ side_total_cnt
-- chg_dir : Δ chg_dir_sum
-- streak(등락 연속) : chg_dir 기준으로 1h 테이블 "직전값"으로 계산
INSERT INTO upbit_data.stats_direction_01_1h (
  market, wr_datetime,
  side_plus, side_minus, side_total,
  side_plus_cnt, side_minus_cnt, side_total_cnt,
  side_avg, acc_bid_volume, acc_ask_volume, net_vol,
  trade_price, change_rate,
  chg_dir, chg_dir_sum,
  chg_up_streak, chg_down_streak,
  chg_up_streak_max, chg_down_streak_max,
  chg_rate_avg, chg_rate_avg_cnt
)
SELECT
  s.market,
  s.wr_datetime,
  s.side_plus, s.side_minus, s.side_total,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) > 0 THEN 1 ELSE 0 END AS side_plus_cnt,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) < 0 THEN -1 ELSE 0 END AS side_minus_cnt,
  s.side_total_cnt,
  s.side_avg, s.acc_bid_volume, s.acc_ask_volume, s.net_vol,
  s.trade_price, s.change_rate,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN  1
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN -1
    ELSE 0
  END AS chg_dir,
  s.chg_dir_sum,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
    ELSE 0
  END AS chg_up_streak,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
    ELSE 0
  END AS chg_down_streak,
  GREATEST(
    IFNULL(pm.chg_up_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_up_streak_max,
  GREATEST(
    IFNULL(pm.chg_down_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_down_streak_max,
  s.chg_rate_avg, s.chg_rate_avg_cnt
FROM upbit_data.stats_direction_01 s
LEFT JOIN (
  SELECT market, MAX(wr_id) AS max_id
  FROM upbit_data.stats_direction_01_1h
  GROUP BY market
) last ON last.market = s.market
LEFT JOIN upbit_data.stats_direction_01_1h pm
  ON pm.market = last.market AND pm.wr_id = last.max_id
WHERE s.wr_datetime >= NOW() - INTERVAL 2 HOUR
  AND NOT EXISTS (
    SELECT 1
    FROM upbit_data.stats_direction_01_1h m
    WHERE m.market = s.market
      AND m.wr_datetime = s.wr_datetime
  )

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `stats_direction_01_1m` ON SCHEDULE EVERY 1 MINUTE STARTS '2026-02-02 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '방향_방향 통계 실시간을 1분 테이블에 실시간 복사_복사' DO -- [1m] 원장(stats_direction_01) → 누적(stats_direction_01_1m)
-- side_plus_cnt/side_minus_cnt : Δ side_total_cnt
-- chg_dir : Δ chg_dir_sum
-- streak(등락 연속) : chg_dir 기준으로 1m 테이블 "직전값"으로 계산

INSERT INTO upbit_data.stats_direction_01_1m (
  market, wr_datetime,
  side_plus, side_minus, side_total,
  side_plus_cnt, side_minus_cnt, side_total_cnt,
  side_avg, acc_bid_volume, acc_ask_volume, net_vol,
  trade_price, change_rate,
  chg_dir, chg_dir_sum,
  chg_up_streak, chg_down_streak,
  chg_up_streak_max, chg_down_streak_max,
  chg_rate_avg, chg_rate_avg_cnt
)
SELECT
  s.market,
  s.wr_datetime,
  s.side_plus, s.side_minus, s.side_total,

  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) > 0 THEN 1 ELSE 0 END AS side_plus_cnt,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) < 0 THEN -1 ELSE 0 END AS side_minus_cnt,
  s.side_total_cnt,

  s.side_avg, s.acc_bid_volume, s.acc_ask_volume, s.net_vol,
  s.trade_price, s.change_rate,

  -- 분봉 등락 방향(Δ chg_dir_sum)
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN  1
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN -1
    ELSE 0
  END AS chg_dir,

  s.chg_dir_sum,

  -- 분봉 연속(streak)
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
    ELSE 0
  END AS chg_up_streak,

  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
    ELSE 0
  END AS chg_down_streak,

  -- 최장 기록(max) 갱신
  GREATEST(
    IFNULL(pm.chg_up_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_up_streak_max,

  GREATEST(
    IFNULL(pm.chg_down_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_down_streak_max,

  s.chg_rate_avg, s.chg_rate_avg_cnt
FROM upbit_data.stats_direction_01 s
LEFT JOIN (
  SELECT market, MAX(wr_id) AS max_id
  FROM upbit_data.stats_direction_01_1m
  GROUP BY market
) last ON last.market = s.market
LEFT JOIN upbit_data.stats_direction_01_1m pm
  ON pm.market = last.market AND pm.wr_id = last.max_id
WHERE s.wr_datetime >= NOW() - INTERVAL 2 MINUTE
  AND NOT EXISTS (
    SELECT 1
    FROM upbit_data.stats_direction_01_1m m
    WHERE m.market = s.market
      AND m.wr_datetime = s.wr_datetime
  )

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `stats_direction_01_1s` ON SCHEDULE EVERY 1 SECOND STARTS '2026-02-05 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '방향_방향 통계 실시간을 1초 테이블에 실시간 복사_복사' DO -- [1s] 원장(stats_direction_01) → 누적(stats_direction_01_1s)
-- side_plus_cnt/side_minus_cnt : Δ side_total_cnt
-- chg_dir : Δ chg_dir_sum
-- streak(등락 연속) : chg_dir 기준으로 1s 테이블 "직전값"으로 계산
INSERT INTO upbit_data.stats_direction_01_1s (
  market, wr_datetime,
  side_plus, side_minus, side_total,
  side_plus_cnt, side_minus_cnt, side_total_cnt,
  side_avg, acc_bid_volume, acc_ask_volume, net_vol,
  trade_price, change_rate,
  chg_dir, chg_dir_sum,
  chg_up_streak, chg_down_streak,
  chg_up_streak_max, chg_down_streak_max,
  chg_rate_avg, chg_rate_avg_cnt
)
SELECT
  s.market,
  s.wr_datetime,
  s.side_plus, s.side_minus, s.side_total,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) > 0 THEN 1 ELSE 0 END AS side_plus_cnt,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) < 0 THEN -1 ELSE 0 END AS side_minus_cnt,
  s.side_total_cnt,
  s.side_avg, s.acc_bid_volume, s.acc_ask_volume, s.net_vol,
  s.trade_price, s.change_rate,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN  1
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN -1
    ELSE 0
  END AS chg_dir,
  s.chg_dir_sum,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
    ELSE 0
  END AS chg_up_streak,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
    ELSE 0
  END AS chg_down_streak,
  GREATEST(
    IFNULL(pm.chg_up_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_up_streak_max,
  GREATEST(
    IFNULL(pm.chg_down_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_down_streak_max,
  s.chg_rate_avg, s.chg_rate_avg_cnt
FROM upbit_data.stats_direction_01 s
LEFT JOIN (
  SELECT market, MAX(wr_id) AS max_id
  FROM upbit_data.stats_direction_01_1s
  GROUP BY market
) last ON last.market = s.market
LEFT JOIN upbit_data.stats_direction_01_1s pm
  ON pm.market = last.market AND pm.wr_id = last.max_id
WHERE s.wr_datetime >= NOW() - INTERVAL 10 SECOND
  AND NOT EXISTS (
    SELECT 1
    FROM upbit_data.stats_direction_01_1s m
    WHERE m.market = s.market
      AND m.wr_datetime = s.wr_datetime
  )

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `stats_direction_01_1s_clean` ON SCHEDULE EVERY 11 MINUTE STARTS '2026-02-05 09:00:00' ON COMPLETION NOT PRESERVE ENABLE COMMENT '통계_통계.1 테이블 10분 보관 11분 단위 삭제_삭제' DO -- 10분 보관, 그 이전(초과분) 삭제
DELETE FROM upbit_data.stats_direction_01_1s
WHERE collected_at < (NOW() - INTERVAL 10 MINUTE)

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `stats_direction_01_24h` ON SCHEDULE EVERY 24 HOUR STARTS '2026-02-02 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '통계_방향 통계 실시간을 24시간 테이블에 실시간 복사_복사' DO -- [24h] 원장(stats_direction_01) → 누적(stats_direction_01_24h)
-- side_plus_cnt/side_minus_cnt : Δ side_total_cnt
-- chg_dir : Δ chg_dir_sum
-- streak(등락 연속) : chg_dir 기준으로 24h 테이블 "직전값"으로 계산
INSERT INTO upbit_data.stats_direction_01_24h (
  market, wr_datetime,
  side_plus, side_minus, side_total,
  side_plus_cnt, side_minus_cnt, side_total_cnt,
  side_avg, acc_bid_volume, acc_ask_volume, net_vol,
  trade_price, change_rate,
  chg_dir, chg_dir_sum,
  chg_up_streak, chg_down_streak,
  chg_up_streak_max, chg_down_streak_max,
  chg_rate_avg, chg_rate_avg_cnt
)
SELECT
  s.market,
  s.wr_datetime,
  s.side_plus, s.side_minus, s.side_total,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) > 0 THEN 1 ELSE 0 END AS side_plus_cnt,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) < 0 THEN -1 ELSE 0 END AS side_minus_cnt,
  s.side_total_cnt,
  s.side_avg, s.acc_bid_volume, s.acc_ask_volume, s.net_vol,
  s.trade_price, s.change_rate,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN  1
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN -1
    ELSE 0
  END AS chg_dir,
  s.chg_dir_sum,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
    ELSE 0
  END AS chg_up_streak,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
    ELSE 0
  END AS chg_down_streak,
  GREATEST(
    IFNULL(pm.chg_up_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_up_streak_max,
  GREATEST(
    IFNULL(pm.chg_down_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_down_streak_max,
  s.chg_rate_avg, s.chg_rate_avg_cnt
FROM upbit_data.stats_direction_01 s
LEFT JOIN (
  SELECT market, MAX(wr_id) AS max_id
  FROM upbit_data.stats_direction_01_24h
  GROUP BY market
) last ON last.market = s.market
LEFT JOIN upbit_data.stats_direction_01_24h pm
  ON pm.market = last.market AND pm.wr_id = last.max_id
WHERE s.wr_datetime >= NOW() - INTERVAL 48 HOUR
  AND NOT EXISTS (
    SELECT 1
    FROM upbit_data.stats_direction_01_24h m
    WHERE m.market = s.market
      AND m.wr_datetime = s.wr_datetime
  )

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `stats_direction_01_5m` ON SCHEDULE EVERY 5 MINUTE STARTS '2026-02-02 09:00:00' ON COMPLETION PRESERVE ENABLE COMMENT '동계_방향 통계 실시간을 5분 테이블에 실시간 복사_복사' DO -- [5m] 원장(stats_direction_01) → 누적(stats_direction_01_5m)
-- side_plus_cnt/side_minus_cnt : Δ side_total_cnt
-- chg_dir : Δ chg_dir_sum
-- streak(등락 연속) : chg_dir 기준으로 5m 테이블 "직전값"으로 계산
INSERT INTO upbit_data.stats_direction_01_5m (
  market, wr_datetime,
  side_plus, side_minus, side_total,
  side_plus_cnt, side_minus_cnt, side_total_cnt,
  side_avg, acc_bid_volume, acc_ask_volume, net_vol,
  trade_price, change_rate,
  chg_dir, chg_dir_sum,
  chg_up_streak, chg_down_streak,
  chg_up_streak_max, chg_down_streak_max,
  chg_rate_avg, chg_rate_avg_cnt
)
SELECT
  s.market,
  s.wr_datetime,
  s.side_plus, s.side_minus, s.side_total,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) > 0 THEN 1 ELSE 0 END AS side_plus_cnt,
  CASE WHEN (s.side_total_cnt - IFNULL(pm.side_total_cnt, s.side_total_cnt)) < 0 THEN -1 ELSE 0 END AS side_minus_cnt,
  s.side_total_cnt,
  s.side_avg, s.acc_bid_volume, s.acc_ask_volume, s.net_vol,
  s.trade_price, s.change_rate,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN  1
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN -1
    ELSE 0
  END AS chg_dir,
  s.chg_dir_sum,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
    ELSE 0
  END AS chg_up_streak,
  CASE
    WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
    ELSE 0
  END AS chg_down_streak,
  GREATEST(
    IFNULL(pm.chg_up_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) > 0 THEN IFNULL(pm.chg_up_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_up_streak_max,
  GREATEST(
    IFNULL(pm.chg_down_streak_max, 0),
    CASE
      WHEN (s.chg_dir_sum - IFNULL(pm.chg_dir_sum, s.chg_dir_sum)) < 0 THEN IFNULL(pm.chg_down_streak, 0) + 1
      ELSE 0
    END
  ) AS chg_down_streak_max,
  s.chg_rate_avg, s.chg_rate_avg_cnt
FROM upbit_data.stats_direction_01 s
LEFT JOIN (
  SELECT market, MAX(wr_id) AS max_id
  FROM upbit_data.stats_direction_01_5m
  GROUP BY market
) last ON last.market = s.market
LEFT JOIN upbit_data.stats_direction_01_5m pm
  ON pm.market = last.market AND pm.wr_id = last.max_id
WHERE s.wr_datetime >= NOW() - INTERVAL 10 MINUTE
  AND NOT EXISTS (
    SELECT 1
    FROM upbit_data.stats_direction_01_5m m
    WHERE m.market = s.market
      AND m.wr_datetime = s.wr_datetime
  )

CREATE DEFINER=`pmaadmin`@`localhost` EVENT `stats_upbit` ON SCHEDULE EVERY 1 SECOND STARTS '2026-02-07 16:04:59' ON COMPLETION NOT PRESERVE ENABLE COMMENT '통계_통계 플랫폼에 업비트 통계값을 넣는다_실시간' DO INSERT INTO stats_direction_01 (
    market, 
    collected_at, 
    upbit_change_rate, 
    current_price
)
SELECT 
    t.market, 
    t.collected_at, 
    t.change_rate, 
    t.trade_price
FROM daemon_upbit_Ticker AS t
-- [필터링] stats_direction_01에 존재하는 market(코인명)만 가져옴
INNER JOIN stats_direction_01 AS s ON t.market = s.market
ON DUPLICATE KEY UPDATE
    collected_at = VALUES(collected_at),
    upbit_change_rate = VALUES(upbit_change_rate),
    current_price = VALUES(current_price)
