select stack() UDTF in Impala - impala

In Hive I can sample data using stack() UDTF like this:
with students as (
select stack(5,
1,'Vikrant',
2,'Abhishek',
3,'Ragesh',
4,'Valeriy',
5,'Swarna') as (id, name)
)
select * from students;
In Presto I can use values for the same.
How can I do it in Impala EXCEPT using multiple selects for each row and UNION ALL like in this answer?

As I studied, Impala does not support table generating functions yet. No stack, no lateral view explode, no UDTF are possible in Impala unfortunately.
So, the only way is this:
with students as (
select 1 as id,'Vikrant' as name union all
select 2 as id,'Abhishek' as name union all
select 3 as id,'Ragesh' as name union all
select 4 as id,'Valeriy' as name union all
select 5 as id,'Swarna' as name
)
select * from students;

Related

Big Query - String Function

I am very new in BigQuery platform, i want to take the following strings
SOCKETIOEXCEPTION##APS.COM, NULLPOINTEREXCEPTION##RSJAVA.COM, CLASSCASTEEXCEPTION##MPS.COM
And get this as a result: SOCKETIOEXCEPTION, NULLPOINTEREXCEPTION, CLASSCASTEEXCEPTION
Before ## characters I want to separate from a given string and then I want to group by number rows available in the above-mentioned tag like SOCKETIOEXCEPTION, NULLPOINTEREXCEPTION, CLASSCASTEEXCEPTION
Sample db details
How do I write this query?
Below is for BigQuery Standard SQL
#standardSQL
SELECT SPLIT(line, '##')[OFFSET(0)] type, COUNT(1) cnt
FROM `project.dataset.table`
GROUP BY type
You can test, play with above using sample data from your question as in example below
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'SOCKETIOEXCEPTION##111' line UNION ALL
SELECT 'SOCKETIOEXCEPTION##222' UNION ALL
SELECT 'SOCKETIOEXCEPTION##333' UNION ALL
SELECT 'NULLPOINTEREXCEPTION##444' UNION ALL
SELECT 'NULLPOINTEREXCEPTION##555' UNION ALL
SELECT 'CLASSCASTEEXCEPTION##666' UNION ALL
SELECT 'CLASSCASTEEXCEPTION##777' UNION ALL
SELECT 'CLASSCASTEEXCEPTION##888'
)
SELECT SPLIT(line, '##')[OFFSET(0)] type, COUNT(1) cnt
FROM `project.dataset.table`
GROUP BY type
with result
Row type cnt
1 SOCKETIOEXCEPTION 3
2 NULLPOINTEREXCEPTION 2
3 CLASSCASTEEXCEPTION 3

Creating a table in Oracle using a query that contains a WITH clause within it

I am basically trying to run a create table statement using a query that has a with clause within it, but I am getting an error. Is there a different way to run this? The query statement is something like this:
CREATE TABLE DATA_TABLE AS
(
WITH X AS
(.....)
SELECT * FROM X
)
I would appreciate any help. Thanks.
Here's what you want.
CREATE TABLE t
AS
WITH some_data AS (
SELECT 1 as some_value
FROM dual
UNION ALL
SELECT 2
FROM dual
)
SELECT *
FROM some_data

Simplest way to process multiple hardcoded values in SQL?

How would I do this in SQL Server? (I know it won't run as written but it illustrates the question better than I can explain)
SELECT SQRT(number) WHERE number IN (4,9,16,25)
It would return multiple rows of course
you can use table value constructor
select sqrt(number)
from (
values (4),(9),(16),(25)
) as T(number)
or use union all
select sqrt(number)
from (
select 4 union all
select 9 union all
select 16 union all
select 25
) as T(number)
sql fiddle demo
You could create a derived table:
SELECT SQRT(number)
FROM (
SELECT 4 AS number
UNION ALL SELECT 9
UNION ALL SELECT 16
UNION ALL SELECT 25
) A

SQL flat table query

I have imported some data from an excel table in to an SQL table and I now need to write a view over the data to combine it with some other fields.
My problem is that the table is in the following form with these columns;
Name Project_One_ID Project_Two_ID Project_Three_ID
Rather than the form I could use, which would be a link table with columns like this;
Name ProjectID
Is it possible to convert this type of table? Or use it as it is? I could do it in code but im struggling in SQL. I have two other tables that need to link on to either side of this link table to create my overall view.
Thanks for any pointers,
You could do it as a UNION and then join to other tables:
SELECT * FROM
(
SELECT Name, Project_One_ID ProjectID
FROM Projects
UNION ALL
SELECT Name, Project_Two_ID ProjectID
FROM Projects
UNION ALL
SELECT Name, Project_Three_ID ProjectID
FROM Projects
) AS P
INNER JOIN ProjectData PD
ON P.ProjectID = PD.ProjectID
Depending on your version of SQL Server you can use CROSS APPLY to UNPIVOT the data.
CROSS APPLY and VALUES will work in SQL Server 2008+:
select name,
ProjectId
from yourtable
cross apply
(
values
('Project_One_ID', Project_One_ID),
('Project_Two_ID', Project_Two_ID),
('Project_Three_ID', Project_Three_ID)
) c (col, ProjectId);
If you are using SQL Server 2005, then you can use CROSS APPLY with UNION ALL:
select name,
ProjectId
from yourtable
cross apply
(
select 'Project_One_ID', Project_One_ID union all
select 'Project_Two_ID', Project_Two_ID union all
select 'Project_Three_ID', Project_Three_ID
) c (col, ProjectId)
if name is a foreign key (unusual - but i suppose possible)
then you could union these things together..
select name, project_one_id as project_id
from newtable
union
select name, project_two_id
from newtable
union
select name, project_three_id
from newtable
The most efficient way to write this query is as an unpivot. This only scans the table once. Here is an example:
with t as (select 'a' as name, 1 as project_one, 2 as project_two, 3 as project_three)
select name, project
from t
unpivot (project for col in (project_one, project_two, project_three)) as unpvt
You can put the unpivot in a view for easy access or use it to create a new table with the columns you want.

input string to table

I am doing some debugging in SQL for oracle 10g. I have a big input string which is used in "IN Clause" i.e.
select * from table where col in ('str2','str3','str4','str5',...)
i want to convert the in clause to rows or table?
Is there a way to do this i.e.
select 'str2','str3','str4','str5', .. from dual
but this outputs multiple columns and i want multiple rows?
Edit:
Here is what i am trying to do. suppose i have an excel data in tmp_table1 (cant create in reality) and tmp_table1 is same as the IN clause, then the below statement will give the missing keys.
SELECT *
FROM tmp_table1
WHERE unique_id NOT IN (
SELECT unique_id
FROM table1
WHERE unique_id IN
('str1', 'str2', 'str3', 'str4'))
now #andriy-m solution works if the in string is less than 4000. but what if its greater?
You are probably looking for this solution.
You can UNION the values into multiple rows:
SELECT 'str2' AS col FROM dual
UNION
SELECT 'str3' FROM dual
UNION
SELECT 'str4' FROM dual
UNION
SELECT 'str5' FROM dual