Error while executing 'select convert' on hive table - sql

I want to execute this sql query on a hive table:
select * from sampleDB.sampleTable
where sampleDate>=(select convert(DATE, dateadd(MONTH,-6,getdate())));
But I am getting this error: Error while compiling statement: FAILED: parse exception line 1:64 cannot recognize input near 'select' 'convert' '(' in expression specification (state=42000,code=40000)
Can someone help me understand how this can be achieved? Basically I want to filter on date say 6 months from current date.
Thanks!

Hive do not supports only >= in sub query it support only below type of sub query
Scalar subqueries
IN subqueries
EXISTS subqueries
you can achieve the same with daenter link description hereteformate
select * from sampleDB.sampleTable
where sampleDate>= date_format(current_date - interval '7' day,'%Y-%m-%d') ;

Related

ORA-12801: error signaled in parallel query server P01H, ORA-01722: invalid number

as i am using below the condition to change the data_type TO_NUMBER but facing the error while TESTING
ORA-12801: error signaled in parallel query server P01H,
ORA-01722: invalid number
to_number(trunc(CASE WHEN $Aggregation.TP_SYS_NM='CT' THEN $BCRS_TCDR_CT.rwa WHEN $Aggregation.TP_SYS_NM in ('CT1','CT2') THEN $BCRS_TCDR_CT1_CT2.rwa ELSE NULL END))
please help on this issue. thanks in advance.
As long as you are on 12c or above, you can use VALIDATE_CONVERSION or extensions to TO_NUMBER to guard against this, eg
select to_date(created_date, 'dd-mon-yyyy'), ...
from STAGING_SALES
where validate_conversion(
created_date as date, 'dd-mon-yyyy'
) = 1;
which returns 0 or 1 depending on validity, or you can choose a default, eg
select SALES_AMT,
TO_NUMBER(SALES_AMT
DEFAULT -1 ON CONVERSION ERROR) conv_sale
from STAGING_SALES;
More examples and details at this article
https://asktom.oracle.com/Misc/oramag/better-tools-for-better-data.html

FETCH the latest partition from HIVE table

Hi I am very much new to this.
I have three columns YEAR, MONTH,DAY in INTEGER format.
I want to load the script and combine YEAR,MONTH,DAY as single column and fetch the maximum.
I tried like,
Load year,month,date from HIVE.`abc`.`abc1';
SELECT max(cast(year as String) || '_' || cast(month as string) || '_' || cast(day as string)) as result FROM HIVE.`abc`.`abc1';
By doing this I will get the result as 2020_5_21. But I should use the separator and find the max of the date.
The following error occurred: Connector reply error: SQL##f -
SqlState: S1000, ErrorCode: 35, ErrorMsg: [Cloudera][Hardy] (35) Error
from server: error code: '1' error message: 'Error while compiling
statement: FAILED: Execution Error, return code 1 from
org.apache.hadoop.hive.ql.exec.tez.TezTask'.
I want to use the result in WHERE clause. But I don't know the statement.
SQL select * from HIVE.abc.`abc1' where ---- ;
Please help.
If month and day are stored as integers, you need to use lpad() to add zero if it is single digit month or day. For example month 5 should become 05. Without this max may work incorrectly. Also use dash as a separator to have date in compatible format.
max(concat(year,'-',lpad(month, 2,0),'-',lpad(day, 2,0)))
And to use it in the WHERE use WHERE date in (select max ...):
SELECT * from your_table
WHERE concat(year,'-',lpad(month, 2,0),'-',lpad(day, 2,0)) in (select max(concat(year,'-',lpad(month, 2,0),'-',lpad(day, 2,0))) from your_table)
Also you may need to quote names like year, month, day in backticks everywhere in sql:
max(concat(`year`,'-',lpad(`month`, 2,0),'-',lpad(`day`, 2,0)))

Presto SQL + Athena: Cast date fail with error message: INVALID_FUNCTION_ARGUMENT

I have a table with datehourminute column with string datatype and formatted as 202012062302. I want to select the date and cast it to date, so I made a query that looks like this:
select cast(date_parse(created_date, '%Y-%m-%d') as date) created_date,
daily_user
from
(
select concat(substr(datehourminute,1,4),'-',
substr(datehourminute,5,2),'-',substr(datehourminute,7,2))
as created_date, sum(users) as daily_user
from "a-schema".a-table
group by 1
)
order by 1;
However, that query returns this error:
SQL Error [100071] [HY000]: [Simba]AthenaJDBC
An error has been thrown from the AWS Athena client.
INVALID_FUNCTION_ARGUMENT: Invalid format: "(oth-er-)"
Any idea how to fix this? Thanks!

SQL Server gives syntax error

I have following SQL for Microsoft SQL Server
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN DATE'2015-09-12' AND DATE'2015-09-15'
But this throws a syntax error:
Error: Incorrect syntax near '2015-09-12'. SQLState: S0001 ErrorCode:
102
What is wrong? I want to use ANSI literal code.
This is not how you cast in MS SQL Server. Instead, you should try the cast syntax:
SELECT *
FROM tblA
WHERE creation_date BETWEEN CAST('2015-09-12' AS DATE) AND
CAST('2015-09-15' AS DATE)
You can simply use this query, without adding date:
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN '2015-09-12' AND '2015-09-15'
Date literals are tricky... besides the problem with conversion (often depending on your system's culture) you must think of the day's end if you use between!
Here you find a system independent way to literally write dates: https://msdn.microsoft.com/en-us/library/ms187819.aspx?f=255&MSPPError=-2147217396 (look at "ODBC)
So you could write
BETWEEN {d'2015-09-12'} AND {ts'2015-09-12 23:59:59'}
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN CAST('2015-09-12' AS DATE)
AND CAST('2015-09-15' AS DATE)

ERROR: function date_trunc(timestamp without time zone) does not exist

I have this problem. I have an sql query am trying to make to my postgres db. These queries work fine in oracle but am in the process of converting it to a postgres query but it complains. This is the query:
select to_char(calldate,'Day') as Day, date_trunc(calldate) as transdate,
Onnet' as destination,ceil(sum(callduration::integer/60) )as total_minutes,round(sum(alltaxcost::integer) ,2)as revenue
from cdr_data
where callclass ='008' and callsubclass='001'
and callduration::integer >0
and regexp_like(identifiant,'^73')
and bundleunits = 'Money'
and inserviceresultindicator in (0,5)
and regexp_like(regexp_replace(callednumber,'^256','') ,'^73')
group by to_char(calldate,'Day') ,trunc(calldate),'Onnet' order by 2
And the error am getting is this:
Err] ERROR: function date_trunc(timestamp without time zone) does not exist
LINE 4: select to_char(calldate,'Day') as Day, date_trunc(calldate)...
What am I doing wrong, or what is the solution to this error?
Try:
... date_trunc('day',calldate) ...
For PostgreSQL date_trunc() function you must always specify precision as the first argument.
Details here.