I am converting an Oracle query into Impala equivalent. I have a Oracle query like this:
select c1, c2 from t1
where rownum <= (select c3 from t2 where c4 = 'Some string' and c5 = 'some string')
and c2 in (1,2,3) order by c3 asc;
However Impala does not support rownum() that I came to know while researching. Please help me in implementing this in Impala.
Thank you in advance.
You dont have anything like rownum in oracle. However you can create a pseudo column using row_number() over (partition by col, order by col) function. You need to avoid partition by clause.
You can change your sql and add a subquery to calculate rownum column like below.
Also you need to change your query so it works in impala using join instead of the way you wrote above.
select c1, c2, c3 from
(select c1,c2, row_number() over (order by c1) as rownum from t1 ) t1
join (select c3 from t2 where c4 = 'Some string' and c5 = 'some string')
and c2 in (1,2,3)) t2 on
rownum<=t2.c3
order by c3 asc;
According to the documentation, you can use row_number of Impala as it is.
Which means your query would be executed successfully if you try this way:
select column from table
where row_number = 1;
Related
Title was tough to choose my wording.
I have 2 tables I want to join together via a lg_code. Both columns are VARCHAR2(4 byte). I am running into an issue where table1 lg_code = 0003 and table2 lg_code = 3. The three 0's are causing an issue with the join and not returning all the data needed. How would I go about writing the join clause to fix this issue?
Code:
select * from table1 t1 JOIN table2 t2 ON t1.LG_CODE = t2.LG_CODE
I would suggest to convert the value of the columnlg_code to number first then make the join:
SELECT * FROM table1 t1
JOIN table2 t2 ON to_number(t1.LG_CODE) = to_number(t2.LG_CODE)
you can also use ltrim() on them:
SELECT * FROM table1 t1
JOIN table2 t2 ON LTRIM(t1.LG_CODE, '0') = LTRIM(t2.LG_CODE, '0');
but in newer versions of oracle SQL*PLUS it trims automatically.
Can someone please explain me the difference between "NULL col_name" and "NULL as col_name" in hive table DDL. Examples below:
CREATE TABLE table_null AS
SELECT c1, c2, c3, c4 FROM(
'col1' as c1,
'col2' as c2,
NULL as c3,
NULL c4 ......
In Hive and MySQL, the select statement having <value/col_name> as <col_alias> and <value/col_name> <col_alias> gives the same output. as is optional keyword.
Below examples may clarify better:
Hive query
MySQL query
In other databases also it may follow the same rule.
Both are the same. They can be tested this way:
select *
from (
select NULL col1, NULL as col2
from test_nulls ) tabl
where col1 is NULL and col2 is NULL;
I have two tables, T1 and T2 with same set of columns. I need to issue a query which will return me value of columns from either table whichever is not null. If both columns are null return null as the value of that column.
The columns are c1,c2,c3,cond1.
I issued the following query. The problem is that if one subquery fails the whole query fails. Somebody please help me. Probably there is another simple way.
SELECT NVL(T1.c1, T2.c1) c1,NVL(T1.c2, T2.c2) c2,NVL(T1.c3, T2.c3) c3
FROM (SELECT c1,c2,c3
FROM T1
WHERE cond1 = 'T10') T1
,(SELECT c1,c2,c3
FROM T2
WHERE cond1 = 'T200') T2 ;
You need something like this:
SELECT NVL((SELECT T1.c1
FROM T1
WHERE T1.c2 = 'T10'),
(SELECT T2.c1
FROM T2
WHERE T2.c2 = 'T200')) AS c1
FROM dual
Or you may prefer a full outer join:
SELECT NVL(T1.c1, T2.c1) AS c1
FROM T1 FULL OUTER JOIN T2 ON 1=1
WHERE T1.c2 = 'T10'
AND T2.c2 = 'T200'
Your result is logical. If the first table is null no combination of values will exist in the natural join.
EDIT. After some new requirements we can use a hack to get the row. Lets get all three possibilities, T1, T2 or all nulls and select the first one:
SELECT *
FROM ( (SELECT T1.*
FROM T1
WHERE T1.c2 = 'T10')
UNION ALL
(SELECT T2.*
FROM T2
WHERE T2.c2 = 'T200')
UNION ALL
(SELECT T2.*
FROM dual
LEFT JOIN T1 ON 1 = 0 ) )
WHERE ROWNUM = 1
I have a table t1 and t2 which I join and order to form data set set1.
Two columns c1 and c2 form a unique identifier for the rows in set1.
I want to get all values from set1 after the first row with a specific c1 and c2.
I have a query like the one below which works, but it repeats the same subquery twice, which seems superfluous and overly complex even for Oracle:
SELECT * FROM
(
SELECT row_number() OVER (ORDER BY c1, c3) myOrder, c1, c2, c3
FROM t1, t2
WHERE condition
ORDER BY conditions
) sub1,
(
SELECT sub1_again.myOrder FROM
(
SELECT row_number() OVER (ORDER BY c1, c3) myOrder, c2, c3
FROM t1, t2
WHERE condition
ORDER BY conditions
) sub1_again
WHERE sub1_again.c2 = "foo" AND sub1_again.c3 = "bar"
) sub2
WHERE sub1.myOrder >= sub2.myOrder
ORDER BY sub1.myOrder
It seems like SQL would have a simple way to do this, but I am not sure what to search for.
Is there a cleaner way to do this?
SELECT * FROM (
SELECT row_number() OVER (ORDER BY c1, c3) myOrder, c2, c3
,CASE WHEN c2 = "foo" AND c3 = "bar"
THEN row_number() OVER (ORDER BY c1, c3)
END target_rn
FROM t1, t2
WHERE condition
ORDER BY conditions
) WHERE myOrder > target_rn;
I think there is something missing in the accepted solution. However, it helped me to come up with this:
SELECT * FROM (
SELECT row_number() OVER (ORDER BY c1, c3) myOrder, c1, c2, c3,
max(case when c2 = "foo" AND c3 = "bar" then 1 else 0 end) over (order by c1, c3) rowFound,
FROM t1, t2
WHERE condition
)
WHERE rowFound > 0
ORDER BY conditions
Basically the case is selecting which row is the one to start from, and the max "drags" the value from that row onwards. The last WHERE does the filtering.
Please try to be more specific,if i understood well you have just to add the parameter 'where',like:SELECT * FROM **where** element
Can you use something on the line of
Select * from table(cast(select * from tab1 inner join tab2)) inner join tab3
Take into account that what's inside the table(cast()) is something much more complex than a simple select involving a block like with test as (select) select *... etc.
I need a simple way to do this preferably without the need for a temporary table.
Thank you.
Database: Oracle 10g
LE:
I have something like
Select a.dummy1, a.dummy2, wm_concat(t2.dummy3)
from table1 a,
(with str as
(Select '1,2,3,4' from dual)
Select a.dummy1, t.dummy3
from table1 a
inner join
(Select regexp_substr (str, '[^,]+', 1, rownum) split
from str
connect by level <= length (regexp_replace (str, '[^,]+')) + 1) t
on instr(a.dummy2, t.split) > 0) t2
where a.dummy1='xyz'
group by a.dummy1, a.dummy2
The main idea is that column t2.dummy3 contains CSVs. Thats why i have select '1,2,3,4' from dual.
I need to find all rows that contain at least one of the values from str.
Using any kind of loop is out of the question because further i need to integrate this into a larger query used for a report in SSRS, and the tables needed for this are quite large (>1mil rows)
CAST seem completely irrelevant here. You use CAST to change the perceived datatype of an expression. Here, you're passing it a result set, not an expression, and you're not saying what datatype to cast to.
You should be able to simply remove the TABLE and CAST calls and do something like:
SELECT * FROM (SELECT * FROM tab1 INNER JOIN tab2 ON ...) INNER JOIN tab3 ON ...
e.g.
SELECT * FROM
(SELECT d1.dummy FROM dual d1 INNER JOIN dual d2 ON d1.dummy=d2.dummy) d12
INNER JOIN dual d3 ON d12.dummy = d3.dummy
Subquery factoring should work fine here as well.
WITH x AS (SELECT * FROM DUAL)
SELECT * FROM
(SELECT d1.dummy FROM x d1 INNER JOIN x d2 ON d1.dummy=d2.dummy) d12
INNER JOIN dual d3 ON d12.dummy = d3.dummy;
If you're having difficulty getting that kind of construct to work, try adding more detail to your question about specifically what you've tried and what error you're getting.
Yeah... i found the answer... i was just too SQL n00b to see it as it was right in front of me...
i just took the "with" statement outside of the query and it worked.
thank you so much for your help, it was your answer that led me to see my mistake :D
Something like:
with str as
(Select '1,2,3,4' from dual)
Select a.dummy1, a.dummy2, wm_concat(t2.dummy3)
from table1 a,
(
Select a.dummy1, t.dummy3
from table1 a
inner join
(Select regexp_substr (str, '[^,]+', 1, rownum) split
from str
connect by level <= length (regexp_replace (str, '[^,]+')) + 1) t
on instr(a.dummy2, t.split) > 0) t2
where a.dummy1='xyz'
group by a.dummy1, a.dummy2