指定された時間枠に基づいてカウントを計算する必要があります 現在の日付と過去5年間の日付を考慮する必要があります
select count(*) from table where (year(current_date) -year('2015-12-01')) < 5 ;
上記のクエリは過去5年間のカウントを提供しますが、年の部分のみを考慮しますが、日を考慮した正確なカウントが必要なので、
select count(*) from table where datediff(current_date,final_dt) <= 1825 ;
過去5年間にうるう年があったとしても、それは考慮されません
le年などのシナリオを考慮して、2つの日付の正確な差を計算するハイブの関数はありますか?
回答 2 件
あなたは
count(*)
を計算しようとしていると思いますcurrent_date
間のすべてのレコードcurrent_date
から5年前の日付 、この場合、次のようなことができます:SELECT count(*) FROM table_1 WHERE date_column BETWEEN current_date AND to_date(CONCAT(YEAR(current_date) - 5, '-', MONTH(current_date), '-', DAY(current_date)));
そして、
SELECT datediff( current_date() ,to_date(CONCAT(YEAR(current_date) - 5, '-', MONTH(current_date), '-', DAY(current_date))));
あなたに
1826
を与えます (2016年はle年であるという事実を考慮して)。
add_months
を使用する 関数(日付が2013-05-25
に戻ると仮定します 現在の日付は2018-05-25
です )。