I am new to HiveQL and I need to create a temp table from the results of following query:
SELECT * FROM `database`.`table` LIMIT 0,100;
....Then run query against this temp table. How could I accomplish this and what is the best practice?
So i guess the SQL Anywhere version would be something like:
create table #temp (foo int)
insert into #temp (foo)
select top (100) 1
from dbo.table t
select count(*) from #temp as c
drop table #temp
Any help would be greatly appreciated. Thanks in advance!
In hive you can create similar functionality using CREATE TEMPORARY TABLE . Table can me managed or external and will stay as long as your hive session is active. Also, pls note, if you are running it as part of batch process, this may not be a good idea.
CREATE TEMPORARY TABLE etl.tmp1 as select * from tab limit 10;
select count(*) from etl.tmp1 ;
Related
In Sql Server, we can create temp table in sql editor like :
select * into #temp_sales_data from sales_data; -- this will create a temp table and insert data.
Please let us know what is the equivalent option in Postgres.
You just create one using create temp table
create temp table temp_sales_data
as
select *
from sales_data;
I was trying to create a series of tables in a single SQL query in Oracle Cloud under the ADMIN account. In the minimum script below, RAW_TABLE refers to an existing table.
CREATE TABLE BASE1 AS SELECT * FROM RAW_TABLE;
CREATE TABLE BASE2 AS SELECT * FROM BASE1;
CREATE TABLE BASE3 AS SELECT * FROM BASE2;
SELECT * FROM BASE3
This returns a view of the first 100 rows in BASE3, but it doesn't create the three tables along the way. Did I miss something or is there something peculiar about create table statements in Oracle SQL?
EDIT: The environment is Oracle Database Actions in Oracle Cloud. The three tables would not be available in the list of tables in the database, and doing something like select * from BASE3 in a subsequent query would fail.
CREATE TABLE BASE1 AS SELECT * FROM RAW_TABLE;
CREATE TABLE BASE2 AS SELECT * FROM BASE1;
CREATE TABLE BASE3 AS SELECT * FROM BASE2;
SELECT * FROM BASE3
Above is a valid query sequence for Oracle database. It should have been created three new tables in database. Since it's not happening please do the work in few steps to find out what's wrong.
First please check whether RAW_TABLE is available in database or not. Then try to select data from RAW_TABLE
select * from RAW_TABLE;
If all those are successful then try to create single table with below query:
CREATE TABLE BASE1 AS SELECT * FROM RAW_TABLE;
Hope you would find the problem by then.
DB-Fiddle:
Creating RAW_TABLE and populating data
create table RAW_TABLE (id int, name varchar(50));
insert into RAW_TABLE values (1,'A');
Query to create three more tables ans selecting from the last table:
CREATE TABLE BASE1 AS SELECT * FROM RAW_TABLE;
CREATE TABLE BASE2 AS SELECT * FROM BASE1;
CREATE TABLE BASE3 AS SELECT * FROM BASE2;
SELECT * FROM BASE3
Output:
ID
NAME
1
A
db<>fiddle here
your query fails because you are executing the whole script as one batch and each line is depends on another one , the transactional DBMS's work with blocks of code as one transaction , and that block of code doesn't commit until sql engine can parse and validate the whole block, and since in your block, BASE1 and BASE2 tables doesn't exists just yet , It fails.
so you need to run each statement as a separate batch. either by executing them one by one or in Oracle you can use / as batch separator, like in sql server you can use GO. these commands are not SQL or Oracle commands and are not sent to the database server , they are just break block of code in batches on your client ( like SQL*Plus or shell or SSMS (for Microsoft sql server), so It would look like this:
CREATE TABLE BASE1 AS SELECT * FROM RAW_TABLE;
/
CREATE TABLE BASE2 AS SELECT * FROM BASE1;
/
CREATE TABLE BASE3 AS SELECT * FROM BASE2;
/
SELECT * FROM BASE3
if your client doesn't support that then you only have to run them one by one in separate batches.
I have som hql statements running every day like
drop table if exists table1;
create table table1 as
select ....
from A;
But some times create table fails and then my original table is¨deleted without a new one is created
I would like to do this a smarter way
like this pseudo code
if ( create table table1_tmp as
select ....
from A; )
then ( drop table table1 ;
rename table1_tmp to table1 ;
)
Is it possible to do that in hql or do i have to at
pig, spark, python etc.
INSERT OVERWRITE is the best fit for what you are trying to do.
You might want to keep an eye on your HDFS Trash folder because I think the underlying files always move to /user/<username>/.Trash when you perform an INSERT OVERWRITE
This question already has answers here:
How to save select query results within temporary table?
(3 answers)
Closed 4 years ago.
I have a SQL query (SQL Server) and it generate reports, I want to store that exact report in temp table so I can play with it later. Now question is do I need to create temp table first and then store SQL query result into it, or is there any way to dynamically create table and store query result?
Look at SELECT INTO. This will create a new table for you, which can be temporary if you want by prefixing the table name with a pound sign (#).
For example, you can do:
SELECT *
INTO #YourTempTable
FROM YourReportQuery
You can use select ... into ... to create and populate a temp table and then query the temp table to return the result.
select *
into #TempTable
from YourTable
select *
from #TempTable
In MySQL:
create table temp as select * from original_table
Try:
exec('drop table #tab') -- you can add condition 'if table exists'
exec('select * into #tab from tab')
Suppose your existing reporting query is
Select EmployeeId,EmployeeName
from Employee
Where EmployeeId>101 order by EmployeeName
and you have to save this data into temparory table then you query goes to
Select EmployeeId,EmployeeName
into #MyTempTable
from Employee
Where EmployeeId>101 order by EmployeeName
how to dynamically create a table with same columns as that of previous table. in sql
select * into new_table from table where 1 = 0
select * into new_table from table
Thats works in SQL2005
I believe that both of the above answers will work, but since you don't need the data and you just need the format, I would do the following:
select * into new_table from table
TRUNCATE new_table; -- I'm sure you know this, but just in case someone is new and doesn't, truncate leaves the table structure and removes all of the data.