1

I'm passing max(pay_date) to a variable Max_date in Shell from Hive table. The datatype of pay_date field is Date.

I want to extract 7 days of pay_date from Max_date of pay_date from the table.

I used below script to get...

#!/bin/bash
Max_date=$(hive -e "select max(pay_date) from dbname.tablename;")

hive -e "select pay_date from dbname.tablename where pay_date >= date_sub(\"$Max_date\",7);"

It's not giving me any output.

I'm stuck with passing a variable which has date value and use that in date_sub function for last 7 days of rows.

Please let me know if I'm missing some absolute basics.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Dhamu
  • 13
  • 4

1 Answers1

1

You can run query like this:

hive -e "select o1.order_date from orders o1 join (select max(order_date) order_date from orders) o2 on 1=1 where o1.order_date >= date_sub(o2.order_date, 7);

Also you can use this as part of shell script:

max_date=$(hive -e "select max(order_date) from orders" 2>/dev/null)

hive -e "select order_date from orders where order_date >= date_sub('$max_date', 7);"
Durga Viswanath Gadiraju
  • 3,896
  • 2
  • 14
  • 21