메뉴 건너뛰기

XEDITION

MaraSong :: 정보와 기술 IT

서브쿼리 - 바로 이전 값과 비교해서 얼마나 올라갔는지, 얼마나 내려갔는지 확인할 수 있는 쿼리 

 

game_ccu_log 라는 테이블에 "YYYY-MM-DD HH:MM:SS" 로 ccu_count 라는 값이 저장되어 있다면,

아래와 같은 서브쿼리를 이용해서 바로 이전과 비교해서 ccu_count 값이 올라갔는지, 내려갔는지.

올라갔다면 이전 값에 비해 몇 퍼센트 올라갔는지 알 수 있습니다.

 

 

Select 
    reg_time, ccu_count, diff_time, diff_count, ccu_count*0.07 as ccu_7p, 
    if(diff_count<0,diff_count*-1, diff_count) as diff_count2,
    if(ccu_count*0.07 < if(diff_count<0,diff_count*-1, diff_count),1,0) as check_ccu, 
    adddate(reg_time,interval 9 hour) as reg_time2
FROM
(
    Select 
        reg_time, ccu_count, timediff(now(), t1.reg_time)>'00:05:00' as diff_time,
        (
            cast(t1.ccu_count as signed)-
            (
            SELECT cast(t2.ccu_count as signed) 
            FROM  
                game_ccu_log as t2 
            WHERE 
                server_type='Game' and t2.reg_time < t1.reg_time 
            ORDER BY 
                t2.reg_time DESC LIMIT 1
            )
        ) as diff_count
    FROM 
        game_ccu_log as t1 
    WHERE 
        reg_time>='${date_now}' AND server_type='GameCCU' 
    ORDER BY reg_time desc limit 1
) as a

 

 

날짜 데이터는 ${date_now} <- 이런 식으로 변수로 입력받게 되어있습니다.

 

적절하게 사용하면 동접 급감, 급증에 대한 알람을 받을 수 있습니다.

 





* 쿠팡 파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있습니다.

위로