Convert ARRAY_UPPER() to Hive query - sql

I have to convert a postgres query to a hive query as :
Select
SUBSTRING(col_name[ARRAY_UPPER(col_name)][2],1,8)
from tab
As hive does not have anything called ARRAY_UPPER then how can we covert?
In my hive table corresponding col_name is of String type, so I tried as below but not getting desired output.
select substring(col_name,-10,8) from hive_tab
Input and output are:

with data_example as(
select
array(1476290200, 35525707293822) as a
)
select substr(a[size(a)-1],-7)
from data_example
Result:
7293822

Related

Json array Count in Oracle sql

I'm having below value in the column "c1" (which is of datatype clob) on table "t1" in oracle (version 19c )
Data
[{"ID":"1","DUPID":"1"},{"ID":"2","DUPID":"2"},{"ID":"3","DUPID":"3"},{"ID":"4","DUPID":"4"},{"ID":"5","DUPID":"5"},{"ID":"6","DUPID":"6"},{"ID":"7","DUPID":"7"}]
I would like to get the count of the array. I've queried like below. But would like to know if there is a better way to achieve this.
Query used:
select regexp_count ( c1,'},{' ) +1 from t1 ;
select
JSON_value('[0,1,2,3,4]', '$.size()')
from dual ;

pgp_sym_decrypt using with select query

I want to select all data from a table (all data in table were encrypted) in postgresql database. But can't get all data.Query only works with condition. This query works. select pgp_sym_decrypt(name::bytea,'code'), pgp_sym_decrypt(surname::bytea,'code'), pgp_sym_decrypt(context::bytea,'code') from schema.table_name where id=1;
But I want to use this query : select pgp_sym_decrypt(name::bytea,'code'), pgp_sym_decrypt(surname::bytea,'code'), pgp_sym_decrypt(context::bytea,'code') from schema.table_name;
How can I get all data?

query json data from oracle 12.1 having fields value with "."

I have a table that has JSON data stored and I'm using json_exists functions in the query. Below is my sample data from the column for one of the rows.
{"fields":["query.metrics.metric1.field1",
"query.metrics.metric1.field2",
"query.metrics.metric1.field3",
"query.metrics.metric2.field1",
"query.metrics.metric2.field2"]}
I want all those rows which have a particular field. So, I'm trying below.
SELECT COUNT(*)
FROM my_table
WHERE JSON_EXISTS(fields, '$.fields[*]."query.metrics.metric1.field1"');
It does not give me any results back. Not sure what I'm missing here. Please help.
Thanks
You can use # operator which refers to an occurrence of the array fields such as
SELECT *
FROM my_table
WHERE JSON_EXISTS(fields, '$.fields?(#=="query.metrics.metric1.field1")')
Demo
Edit : The above case works for 12R2+, considering that it doesn't work for your version(12R1), try to use JSON_TABLE() such as
SELECT fields
FROM my_table,
JSON_TABLE(fields, '$.fields[*]' COLUMNS ( js VARCHAR2(90) PATH '$' ))
WHERE js = 'query.metrics.metric1.field1'
Demo
I have no idea how to "pattern match" on the array element, but just parsing the whole thing and filtering does the job.
with t(x, json) as (
select 1, q'|{"fields":["a", "b"]}|' from dual union all
select 2, q'|{"fields":["query.metrics.metric1.field1","query.metrics.metric1.field2","query.metrics.metric1.field3","query.metrics.metric2.field1","query.metrics.metric2.field2"]}|' from dual
)
select t.*
from t
where exists (
select null
from json_table(
t.json,
'$.fields[*]'
columns (
array_element varchar2(100) path '$'
)
)
where array_element = 'query.metrics.metric1.field1'
);
In your code, you are accessing the field "query.metrics.metric1.field1" of an object in the fields array, and there is no such object (the elements are strings)...

add table_id to the result from multiple tables in BigQuery

Below is how I structured the data in BigQuery database.
test
-> sales
-> monthly-2015
-> monthly-2016
-> ...
I want to combine the data of all tables with the table name , monthly-*, and below is how I wrote the sql from examples I found.
Running this sql leads an error like following Scalar subquery produced more than one element. How could I fix it to error?
SELECT
*,
(
SELECT
table_id
FROM
`test.sales.__TABLES_SUMMARY__`
WHERE
table_id LIKE 'monthly-%')
FROM
`test.sales.monthly*`
I want to combine the data of all tables with the table name , monthly-*
Try below
SELECT *, 'monthly_' || _TABLE_SUFFIX as table_name
FROM `test.sales.monthly_*`

Using not equal symbol in hive query

I need to use '!=' symbol in my hive query with partitions.
I tried something like
from sample_table
insert overwrite table sample1
partition (src='a')
select * where act=10
insert overwrite table sample1
partition (src!='a')
select * where act=20
But it is showing error at '!=' symbol. How can i replace !=
Try to use rlike/regex function in hive to specify condition.
I think you can also use not operator <> not !=
partition (src!='a') - what do you expect Hive to do - to write "select *" result into any partition instead of "a"? You see, partition (src='a') means that you are writing result of aftergoing select statement into table's partition named "a". "PARTITION (a=b)" is not a conditional command like "WHERE a=b", you're just specifying how to name a partition.
You have just to specify another partition name, so your query should look like:
from sample_table insert overwrite table sample1 partition (src='a') select * where act=10 insert overwrite table sample1 partition (src='b') select * where act=20;
After that you should see 2 new partitions "a" and "b" in table "sample1" with some data from these select * where act=10 and select * where act=20 queries respectively.
may i know your hive version?
try using A <> B
Description from Hive DOCS:
NULL if A or B is NULL, TRUE if expression A is NOT equal to expression B, otherwise FALSE.