I have the need to write a Hive query that has a subquery in the select statement. Im aware that Hive does not support this, therefore I'm looking out for my options.
select
a,
b,
(select max(tbl.c) from sample_table_a tbl where tbl.d like 'X012%') as d,
e,
f
from sample_table_b
How can I implement the above query in hive without using a cross join because sample_table_a contains about 40000 tuples and so does the sample_table_b.
This could be an option:
There is going to be a single row in table t so join should not be that much a problem
select
a,
b,
max_value,
e,
f
from
sample_table_b
inner join
(
select
max(tbl.c) as max_value
from
sample_table_a tbl
where
tbl.d like 'X012%'
)
t;
Related
Hay All,
is it possible to run more than 1 select statement after using with?
first select statement works fine, as soon as i add another select statement i got a error.
with
a as (select a,b,c from Table1 with(readuncommitted)),
b as (select d,e,f from Table2 with(readuncommitted))
select * from a
select * from b
expected output:
Table 1
a
Table 2
b
Well the way CTEs will behave is that they will only be in scope for the first query, but not the second. You could perhaps do a union query here:
SELECT a, b, c, 'Table1' AS src FROM a
UNION ALL
SELECT d, e, f, 'Table2' FROM b;
Or, you could move the b CTE to before the second query:
WITH a AS (
SELECT a, b, c
FROM Table1
WITH(readuncommitted)
)
SELECT * FROM a;
WITH b AS (
SELECT d, e, f
FROM Table2
WITH(readuncommitted)
)
SELECT * FROM b;
hay DasD,
You can not use multiple select for cte, but you can use more than one CTE like this.
with
a as (select a,b,c from Table1 with(readuncommitted)),
b as (select d,e,f from Table2 with(readuncommitted))
select * from a,b
You have to explain to the database, what you wantfrom bith tables.
as both have the same structure you can use UNION to join them vertically
with
a as (select a,b,c from Table1 with(readuncommitted)),
b as (select d,e,f from Table2 with(readuncommitted))
select * from a
UNION
select * from b
From the docs:
"A CTE must be followed by a single SELECT, INSERT, UPDATE, or DELETE statement that references some or all the CTE columns."
Source
I have this table:
and would like to convert it to the following:
Please help me, been stuck on it for way too long. Doesn't working for me using group by
WITH A as (SELECT id, a FROM XXX WHERE a is not null),
B as (SELECT id, b FROM XXX WHERE b is not null)
SELECT A.a, B.b, A.id FROM A
INNER JOIN B on A.id = B.id;
For this dataset, simple aggregation would do what you want:
select min(a) a, min(b) b, id
from mytable
group by id
This takes advantage of the fact that aggregate functions ignore null values; we could get the very same result with max() as we did with min().
Can a query which uses Group By/Having clauses, be modified to another query which uses just Select/From/Where clauses?
TABLE T(a, b, c)
SELECT a, sum(c)
FROM T
WHERE b>10
GROUP BY a
HAVING sum(c)>5
Would appreciate it if you could explain in detail why it can(not) be done.
You could, of course, resort to using window functions only, if your specific database supports those:
SELECT a, s
FROM (
SELECT DISTINCT a, sum(c) OVER (PARTITION BY a) s
FROM t1
WHERE b > 10
) t2
WHERE s > 5
Another option is to use correlated subqueries, which work on all databases:
SELECT a, s
FROM (
SELECT DISTINCT a, (SELECT sum(c) FROM t t3 WHERE t1.a = t3.a AND b > 10) s
FROM t t1
WHERE b > 10
) t2
WHERE s > 5
These alternatives would yield the same result without using GROUP BY or HAVING. But either of these would be (much) slower, and I don't really see the point...
(Note I am getting an error submitting to stackoverflow if i use "select", so have misspelled my queries. [Now Fixed])
Sorry this is a newbie question. I have one very long SQL query that is getting harder to manage. In fact there are some sub-queries that are being used multiple times. What is the best way to break up the query? I would prefer to keep it in the database, rather than take it out into the calling program. It goes something like this.
Select A, B, C
from (select D from Table_1 where ...)
Union Select E, F
from Table_2
Inner Join (Select D, E, from Table_1 where...)..
So what I would like to do is
Result1 = select D,E from Table_1 where....
Result2 = Select A,B,C from Result_1 Union Select E,F from Table_2 Inner Join Result_1 ...
What is the best way to do this? I can't use Views because I don't have privileges. How can I use the results from the first query in the second query? Can cursors be used in this case?
Using a CTE you can access the same subquery multiple times (this is the main difference to Derived Tables):
with CTE as
(Select D, E, from Table_1 where...)
Select A, B, C
from CTE
Union
Select E, F
from Table_2
Inner Join CTE ..
In an unpivoting operation, I would like the following:
SELECT A, B, C FROM [complex joins/where clause] As DerivedTable
UNION
SELECT A, B, D FROM DerivedTable
UNION
SELECT A, B, E FROM DerivedTable
...
but it complains that DerivedTable cannot be found (I use a derived table so that [complex joins/where clause] doesn't have to be evaluated again and again thereby slowing things).
I know I can simply create a new query called DerivedTable to represent [complex joins/where clause] but
The above SQL is passed from Excel - I'd rather not have to open the database to create a new query prior to running the above
The [complex joins/where clause] is generated dynamically, and changes from user to user, two of which may be running the above SQL at the same time.
Something like this. Use CASE and JOIN this table with (1,2,3,...) table. I'm not sure it is right syntax for Access but it will work on most SQL dialects.
SQLFiddle demo
SELECT A,B,
CASE WHEN CT.r=1 then C
WHEN CT.r=2 then D
WHEN CT.r=3 then E
END
FROM [complex joins/where clause] As DerivedTable
CROSS JOIN (select 1 as r
union all
select 2 as r
union all
select 3 as r
) as CT
order by A,B