I need to assign a 'select value' in a parameter and use it in hive code instead of assigning constant to a parameter.
In Hive, set a = 10; //but instead of this how can we assign dynamic values, as follows:
I Need: set a = select max(x) from y; //which assign maximum value of 'x' from table 'y' to 'a' parameter.
Ayesha
You should use select instead of set, when you are assigning values directly from queries.
SELECT #variable1 = column1, variable2 = column2
FROM table1
WHERE column1 = 'unique value'
or
INSERT OVERWRITE LOCAL DIRECTORY '<directory>' SELECT * FROM table_name;
There's no way to do this from within Hive, but you can kind of do it from outside Hive, like in a bash script:
a=`hive -S -e 'select max(x) from y'`
hive --hiveconf "a=$a" -e '[next query here]'
Related
I would like to use something like:
set hivevar:average = select avg(nvl(val,0)) as average from table;
and Then use on another table like:
select * from table2 where value>=${average}
Is this possible in hive? if not how is the correct way on doing
How do I return data out of IF statements? I have a IF statement which is meant to return a different result dependent of the result of that statement.
IF :Value = 1 THEN
SELECT Name FROM TABLE_A
ELSEIF :Value = 2 THEN
SELECT Name FROM TABLE_B
ELSEIF :Value = 3 THEN
SELECT Name FROM TABLE_C
but this doesn't work. It expects an INTO statement in those selects. I suspect this is because Oracle can't return out of a block?
Is there a quicker way to return those select statements without creating table variables to store the data or messing around with functions?
You can do this by plain SQL:
SELECT
':Value' user_input,
CASE
WHEN ':Value' IN('a1','a2','a3')
THEN (select name from table_a)
WHEN ':Value' = 'i'
THEN (select name from table_b)
END AS output
FROM
dual
(good info about case)
If you want more than one result in your cases, then you may opt to an intelligent UNION:
SELECT t1_Col_1, t1_Col_2,'&VALUE' from TABLE_1
WHERE '&VALUE' = '1'
UNION ALL
SELECT t2_Col_1, t2_Col_2,'&VALUE' from TABLE_2
WHERE '&VALUE' = '2'
In this solution, types and number of tx_Coln must be the same.
How do I store the result of a query into a variable in HiveQL and then use it in another select statement?
For example, whenever I store a normal variable and use it in a select statement it works just fine.
SET a=1; SELECT CASE WHEN b > ${hiveconf:a} THEN NULL ELSE 1 from my_table
But when I try and put a query into the variable, its seems to store the query instead of running it and storing the result. This then results in an error.
SET a=SELECT MAX(num) FROM my_other_table; SELECT CASE WHEN b > ${hiveconf:a} THEN NULL ELSE 1 from my_table
The error being: cannot recognize input near 'select' 'max' '(' in select clause
Does anyone know a work around to this? I am using Hive 0.13
You cant do that only by hive.
If your hive query is controlled by outer script like shell or python.You can perform the first query, get the output and then put it in the next sql.
Or you can change your sql to use join.Your example code can be changed to
select case when b > t.a then NULL else 1 from my_table
join (select max(num) a from my_other_table) t
i tried it this way
set #v1 := (SELECT SUBSTR(IFSC,1,4) FROM rtgs_new MINUS SELECT SUBSTR(IFSC,1,4) FROM rtgs_temp);
or
set #v1 = (SELECT SUBSTR(IFSC,1,4) FROM rtgs_new MINUS SELECT SUBSTR(IFSC,1,4) FROM rtgs_temp);
but i get this error:
SP2-0158: unknown SET option "#v1"
i want to use this variable to compare with and insert into another oracle table.
any information will be helpful.
thank you guys!
If you just need to insert into another table, why don't you do that?
insert into another_table
SELECT SUBSTR(IFSC,1,4) FROM rtgs_new MINUS SELECT SUBSTR(IFSC,1,4) FROM rtgs_temp;
I have a an sqlite database with the table test. Several processes are accessing this database from bash. The table has the following fields:
CREATE TABLE mytable (id NUMERIC,
start JULIAN,
finish JULIAN)
I obtain an unique id by:
id=$(sqlite test.db <<EOF
BEGIN EXCLUSIVE;
SELECT id FROM mytable WHERE start IS NULL ORDER BY RANDOM() LIMIT 1;
COMMIT;
EOF
)
My question is, how can update the field start with:
UPDATE mytable set start=julianday('now') where id="SELECTED ID FROM ABOVE";
In the same statement?
Based on the comments that you supplied above, my solution would look something like follows (in perl with a raw DBI connection, also i didn't do a lot of error checking or anything either, something that you should probably do):
my $dbh = DBI->connect(...);
$dbh->do("BEGIN EXCLUSIVE");
my $stm = $dbh->prepare("SELECT id FROM mytable WHERE start IS NULL ORDER BY RANDOM() LIMIT 1");
$stm->execute();
my $row = $stm->fetchrow_hashref();
my $id = undef;
if ( $row ) {
$id = $row->{ID};
my $ustm = $dbh->prepare("UPDATE mytable set start=julianday('now') where id=?");
$ustm->execute($id);
}
$dbh->do("COMMIT");
# Still have the id at this point.