Temp table not running - sql

Whenever I run this temp table in google bigquery sql i get "use of create temporary table requires a script"?
CREATE TEMP TABLE products AS
SELECT product, product_color,
FROM `radiant-oven-328313.customer_data.customer_purchase`
WHERE product = "fan";
Does this mean I need to insert data into my table or is my syntax correct??
To insert into my table, I've tried the additional line of code below:
Select product_color as White_color
FROM products
WHERE product_color = "white"
FROM `radiant-oven-328313.customer_data.customer_purchase`
Any help would be much appreciated, thanks!

You will need to specify the name and types for the columns.
For example:
CREATE TEMP TABLE product (product STRING, product_color STRING)
AS
SELECT product, product_color
FROM `radiant-oven-328313.customer_data.customer_purchase`
WHERE product="fan"
;
This will let you create a temporary table and interact with it within your session. However if you are not using this as part of a script already and just need it as part of your SQL Query logic you may want to consider using a CTE or sub-query instead.
For more information on temp tables:
https://cloud.google.com/bigquery/docs/reference/standard-sql/scripting-concepts#temporary_tables
For more information on CTEs:
https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#with_clause

Related

Using a select statement to define a table name

I am trying to create snapshot tables as part of script I run regularly.
At the moment I have to manually enter table names, but I would like to call on a field in the base data table to create the snapshot table name.
For example:
Base Data Table = base_data and contains a field for the month it was created in.
Snapshot table = base_data_month
I have already tried to run this to create an automatically named table...
create table base_data_snapshot_||(select month from base_data) as
select * from base_data
But this gets a syntax error. For reference there is only one month included in the base data.
Has anyone had any success with this before?
Although we cant combine a running create statement with a select but can use select. So, I guess below should work
create table
base_data_snapshot_||t.month
as
( select * from base_data) t

Trying to append a query onto a temporary table

Using SQL Server 2008-R2
I have a csv of purchase IDs and in my database there is a table with these purchase IDs and there corresponding User IDs in our system. I need these to run a more complicated query after that using. I tried to bulk insert or run import wizard but I don't have permission. My new idea is to create a #temp using SELECT INTO and then have the query inside that like below.
SELECT *
INTO ##PurchaseIDs
FROM
(
SELECT PurchaseID, UserID, Added
FROM Users
WHERE PurchaseID IN (
/* These are the csv IDs just copied and pasted in */
'49397828',
'49397883',
etc.
What happens is that there are ~55,000 IDs so I get this error.
The query processor ran out of internal resources and could not
produce a query plan. This is a rare event and only expected for
extremely complex queries or queries that reference a very large
number of tables or partitions. Please simplify the query. If you
believe you have received this message in error, contact Customer
Support Services for more information.
It works if I upload about 30,000 so my new plan is to see if I can make a temp table, then append a new table to the end of that. I am also open to other ideas on how to accomplish what I am looking to do. I attached an idea of what I am thinking below.
INSERT *
INTO ##PurchaseIDs
FROM (
SELECT PurchaseID, UserID, Added
FROM Users
WHERE PurchaseID IN (
/* These are the OTHER csv IDS just copied and pasted in */
'57397828',
'57397883',
etc.
You need to create a temp table and insert the values in IN clause to the temp table and Join the temp table to get the result
Create table #PurchaseIDs (PurchaseID int)
insert into #PurchaseIDs (PurchaseID)
Select '57397828'
Union All
Select '57397828'
Union All
......
values from csv
Now use Exists to check the existence of PurchaseID in temp table instead of IN clause
SELECT PurchaseID,
UserID,
Added
FROM Users u
WHERE EXISTS (SELECT 1
FROM #PurchaseIDs p
WHERE u.PurchaseID = p.PurchaseID)

How can I create a partitioned table 'like' an unpartitioned table with Hive HQL?

I've got a table with two weeks worth of entries, and I would like to copy those entries into a table partitioned by date (creating it if it does not exist).
I'm writing a luigi task to do this, and I would love for it to be independent of the table schema--i.e. I wouldn't have to specify column names and types, and it would CREATE TABLE IF NOT EXISTS when necessary.
I was hoping I could use:
CREATE TABLE IF NOT EXISTS test_part
COMMENT 'This is a test table to see if partitioning works in this case'
PARTITIONED BY (event_date string)
AS select *, '2014-12-15' from source_db.source_table
where event_at <'2014-12-16' and event_at >='2014-12-15';
But this of course fails with: FAILED: SemanticException [Error 10068]: CREATE-TABLE-AS-SELECT does not support partitioning in the target table
I tried again with "like" with basically the same results. Is there a way to do this that I am missing? It doesn't have to be atomic. Multiple sequential commands are fine.
You do not do a create table as.
You create a table first using describe source_table and then you make an insert into table partition (event_date string)
2 steps it works better.

Is there an automated way to create a temp table in SQL Server

I have a rather complex SELECT statement in a stored procedure that I am updating to insert the rows from the select into a temp table. To define the temp table, I need to know the data type of each every item selected.
Is there a easy way (a script maybe) that I can use to determine the data types and the temp table structure instead of going to each table's definition in the select to find out what it is?
PS: I can't use a Common table expression as I need to use this temp table several times within the proc
SELECT
blah
INTO
#temp
FROM
wibble
blah and wibble are not secret syntax. Please replace these with your own SQL :)
SELECT * INTO #temp FROM TABLE1
All columns in TABLE 1 gets into your temp table now

Dynamically creating tables using information held in another table

I want to create a table in sql using the columns details (name, data type etc.) stored in anther table in the database.
Depending on the database you can use the information schema tables. They hold the information you are looking for. Look for the table that describes the columns.
Postgres: http://www.postgresql.org/docs/8.4/interactive/information-schema.html
MySQL: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
You can query these tables and use 'select into' to insert the results into your other table.
One opinion is to create CREATE TABLE query and execute it in ADO.NET like shown here this
Try out this code
CREATE TABLE new_table
AS
SELECT *
FROM old_table
WHERE 1 = 2;