how to manage Date interval in hive - hadoop

I'm new in Hive-Hadoop. I have some problem with Date interval management.
In Postgresql, I can get the "6 days" before a given date :
select max(datejour) + INTERVAL '-6 day' as maxdate from table
e.g : if max(datejour) = 2015-08-22 ==> my query returns 2015-08-15
Does somebody can help me on how could I do it in Hive?
thanks.

You can use Hive INTERVAL to achieve this.
select (max(datejour) - INTERVAL '6' DAY) as maxdate from table
Above query should return 2015-08-15
You can find more details -
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types

You can use Hive Date builtin function to achieve this
select date_sub('2015-08-22', 6) from table
Above query should return 2015-08-15
You can find more Hive built-in function here: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions
Hope his helps

You can use DATE_SUB function to get your requirement.
Query may look like this(in your case):
select DATE_SUB(from_unixtime(unix_timestamp(cast(MAX(t1.max_date) AS string) ,'yyyy-MM-dd'), 'yyyy-MM-dd'), 6) from (select MAX(datejour) as max_date from table) t1 group by t1.max_date;

Since updating records using UPDATE command is not possible in hive and adding columns through alter command is not recommended as you have to insert values in it through same table.
create external table test(
fields1 string,
field2 string)
create external table test(
fields1 string,
field2 string,
h01 string
)
Insert overwrite table table2
select
fields1,
field2,
case when fields1 = '' then 'OK' else 'KO' end as h01 from table1 where your_condition;

Related

Why doesn't this Oracle DATE comparison work

In Oracle 12, if I create a very simple table, TEST_TABLE, with a single varchar2(128) column 'name' and populate that column with lots of strings of '20170831', and my sysdate shows:
SELECT sysdate FROM dual;
29-SEP-17
then why does this SQL query return 0 rows:
SELECT TO_DATE(name,'YYYYMMDD'),
TO_DATE(TRUNC(SYSDATE),'DD-MM-YYYY')
FROM TEST_TABLE
WHERE TO_DATE(name,'YYYYMMDD') < TO_DATE(TRUNC(SYSDATE),'DD-MM-YYYY');
(This is a very simplified example of a problem I'm facing in my partition maintenance script and have not been able to solve for the last week).
Thank you in advance for any assistance related to the above query.
Midnight(time part is 00:00:00.000):
SELECT TO_DATE(name,'YYYYMMDD'), TRUNC(SYSDATE)
FROM TEST_TABLE
WHERE TO_DATE(name,'YYYYMMDD') <= TRUNC(SYSDATE);
You could also try:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Just don't apply a to_date() to an already date field, this because, it will implicitly convert that date into varchar and then apply the to_date() function to it, for example your query part TO_DATE(TRUNC(SYSDATE),'DD-MM-YYYY') is interpreted like this:
TO_DATE(TO_CHAR(TRUNC(SYSDATE)),'DD-MM-YYYY')
TO_CHAR(TRUNC(SYSDATE)) is getting a char something like: '31-AUG-17', and that is not in 'DD-MM-YYYY' format.
And because of that, TO_DATE(TRUNC(SYSDATE),'DD-MM-YYYY') gets something like this: 29/09/0017 and your filter goes FALSE and gets no results.

SQL to select employees those who have DOB for the day

I'm new to SQL,
I have field called DOB with 2014-11-07 00:00:00.0 date format in my Database.
I just wanted to select the records which has DOB equal to current date.
I want this to select employees who have DOB for the current day.
Regards
Shridhar
In Oracle you can do this:
select * from employees e where trunc(e.DOB) = trunc(sysdate)
Im assuming this is MySQl or Oracle
If you have a table called Employees, you should be able do use this.
SELECT A.*
FROM EMPLOYEES A
WHERE DATE(A.DOB) = CURDATE();
Here is a resource to learn all about different date functions used in MySQL and Oracle:
https://docs.oracle.com/cd/E17952_01/refman-5.1-en/date-and-time-functions.html
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_current-date
Since you have tagged MySQL. You can use EXTRACT() function to get the day of the specific date in mysql
select * from employee
where extract(day,DOB) = extract(day, now());
Below query worked fine for me.
SELECT * FROM $A$ WHERE TO_DATE(TO_CHAR(DOB,'MMDD'),'MMDD') =trunc(sysdate)
Regards
Shridhar

Range Partitioning in Hive

Does hive support range partitioning?
I mean does hive supports something like below:
insert overwrite table table2 PARTITION (employeeId BETWEEN 2001 and 3000)
select employeeName FROM emp10 where employeeId BETWEEN 2001 and 3000;
Where table2 & emp10 has two columns:
employeeName &
employeeId
When I run the above query i am facing an error:
FAILED: ParseException line 1:56 mismatched input 'BETWEEN' expecting ) near 'employeeId' in destination specification
Is not possible. Here is a quote from Hive documentation :
A table can have one or more partition columns and a separate data directory is created for each distinct value combination in the partition columns
No its not possible. Even I use separate calculated column like ,
insert overwrite table table2 PARTITION (employeeId_range)
select employeeName , employeeId/1000 FROM emp10 where employeeId BETWEEN 2000 and 2999;
which will make sure all values fall in same partition.
while querying the table since we already know the range calculator, we can
select employeeName , employeeId FROM table2 where employeeId_range=2;
Thus we can also parallelise the queries of given ranges.
Hope it helps.

Oracle Date Function with between operator is not giving proper result in oracle 10g?

I developed a query like
select accountname count(transactions)
from table1 group by accountname
where date between '27-mar-2012' and '27-jan-2013'
but it is giving all transactions without considering between and operator i.e it is considering all values from jan to dec. My table contain date column values like 27-mar-2012 ...
If i use
select accountname count(transactions) from table1
group by accountname
where date between cast('27-mar-2012' as date) and cast( '27-jan-2013' as date)
I am getting wrong result.
How can i fix it?
One possibility is that Oracle is running the query:
select accountname, count(transactions)
from table1
group by accountname
Because the where clause is not valid after the group by.
I would suggest:
select accountname, count(transactions)
from table1
where date between to_date('2012-03-27', 'yyyy-mm-dd') and to_date('2013-01-27', 'yyyy-mm-dd')
group by accountname
to_date() is safer than convert(). And it is a good idea (IMHO) to use ANSI standard date formats.

How to fetch records according to SYSTIMESTAMP using Hibernate/Oracle11g

I have a field named end_time (of type timestamp(6)) in my Oracle 11g DB. My requirement is to fetch records which are greater than current time stamp.As I work with remote DB, I need the current time of my oracle database server.
After some research I came to know that SYSTIMESTAMP returns current time stamp of machine where DB resides.
So I just put a condition like end_time > SYSTIMESTAMP, but it does not filter records. My end-time is of type timestamp(6).
Do I have to use any conversion function? How can I do it from Hibernate? Any idea?
Can you further explain on "does not filter records", are too many rows in your result or to few?
Your condition looks absolutely ok:
CREATE TABLE mytable (ts TIMESTAMP(6));
INSERT INTO mytable (ts) VALUES (TIMESTAMP '2012-12-06 17:00:00');
INSERT INTO mytable (ts) VALUES (TIMESTAMP '2012-12-06 18:00:00');
SELECT SYSTIMESTAMP FROM DUAL;
06.12.2012 17:10:38.347629000 +01:00
SELECT * FROM mytable WHERE ts > SYSTIMESTAMP;
06.12.2012 18:00:00.000000000
SELECT * FROM mytable WHERE ts < SYSTIMESTAMP;
06.12.2012 17:00:00.000000000

Resources