오랜만에 임팔라 코딩을 하려고 하니까 정말 죽어라고 안된다. 거의 1시간을 헤매서 찾은 아래 코드 방식.

차분히 생각해 보면 아닌데 급박하게 코딩하려면 정말 기본도 생각이 안나더라. 어디나 날짜나 시간 계산하는 코드가 가장 어렵더라. 밑에꺼는 오라클에서는 정말 간단하게 코딩할 수 있는 건데 말이지.

 

SELET A.STND_MTH,

        --CONVERT STRING TO TIMESTEMP

        TO_TIMESTAMP(CONCAT(A.STND_MTH, '01'), 'YYYYMMDD') AS STND_MTH_TSTMP,

        --ADD 1 MONTH

        ADD_MONTHS(TO_TIMESTAMP(CONCAT(A.STND_MTH, '01'), 'YYYYMMDD'), 1) AS ADD_MTH1,

        FROM_TIMESTAMP(ADD_MONTHS(TO_TIMESTAMP(CONCAT(A.STND_MTH, '01'), 'YYYYMMDD'), 1), 'YYYYMMDD') AS ADD_MTH1_STRING

FROM BASI AS A

LIMIT 20;

반응형

'Hive, Impala, Spark' 카테고리의 다른 글

경우의 수를 생성하는 Oracle 쿼리  (0) 2022.11.03
Oracle connect by  (0) 2022.06.17
Oracle connect by  (0) 2022.05.25
Impala random sampling  (0) 2022.04.29
Calendar week# in the month(Hive Query)  (0) 2020.09.25

 This is for someone who wants to get the week# of specific date. When I searched this on google, there was 1 way to calcualte the day# as following variable "calcul_weekno". But what I'm trying to is the week# on calendar. Thus I've searched the other way and made the below variables "calendar_weekno"

 Anyway, below code would help you who find this blog.

-- Declare the date

SET dt = '2020-10-28';

-- See the date
SELECT ${hiveconf:dt};

-- Check the weekofyear() function
SELECT weekofyear(${hiveconf:dt});

-- Check the day() function
SELECT day(${hiveconf:dt});

SELECT weekofyear(${hiveconf:dt}) - weekofyear(last_day(add_months(${hiveconf:dt}, -1))) AS week_no;

SELECT ${hiveconf:dt} AS stnd_date,
                    last_day(add_months(${hiveconf:dt}, -1)) AS lmth_end,
                    date_add(last_day(add_months(${hiveconf:dt}, -1)), 1) AS thmth_begin,
                    weekofyear(${hiveconf:dt}) AS day_weekno,
                    weekofyear(last_day(add_months(${hiveconf:dt}, -1))) AS lmth_end_weekno,
                    weekofyear(date_add(last_day(add_months(${hiveconf:dt}, -1)), 1)) AS thmth_begin_weekno, -- Calculating week #
 CASE
     WHEN day(${hiveconf:dt})%7 = 0 THEN day(${hiveconf:dt})/7
     ELSE floor(day(${hiveconf:dt})/7)+1
 END AS calcul_weekno, -- Calendar base week #
 CASE
     WHEN weekofyear(last_day(add_months(${hiveconf:dt}, -1))) = weekofyear(last_day(add_months(${hiveconf:dt}, -1))) THEN weekofyear(${hiveconf:dt}) - weekofyear(last_day(add_months(${hiveconf:dt}, -1))) + 1
     ELSE weekofyear(${hiveconf:dt}) - weekofyear(last_day(add_months(${hiveconf:dt}, -1)))
 END AS calendar_weekno;

 

반응형

'Hive, Impala, Spark' 카테고리의 다른 글

경우의 수를 생성하는 Oracle 쿼리  (0) 2022.11.03
Oracle connect by  (0) 2022.06.17
Oracle connect by  (0) 2022.05.25
Impala random sampling  (0) 2022.04.29
Convert 'YYYYMM' to timestamp in Impala  (0) 2022.04.21

+ Recent posts