I wish to write two nested queries of create and select statements. My motive is to
create a new table which includes columns and entries from two other tables.
I wrote a query but it is giving me an error.
create table table_3(select * from table_1,table_2)
For SQL Server you can use:
SELECT *
INTO table_3
FROM table_1, table_2
If you want to join the two tables based on some key then:
SELECT *
INTO table_3
FROM table_1 JOIN table_2 on table_1.ID = table_2.FKID
You may see: SQL SERVER – CTAS – Create Table As SELECT – What is CTAS?
This can be used to create another table of same type
CREATE TABLE new_table
AS (SELECT * FROM old_table);
Related
I have 2 bigquery tables with nested columns, I need to update all the columns in table1 whenever table1.value1=table2.value, also those tables having a huge amount of data.
I could update a single nested column with static column like below,
#standardSQL
UPDATE `ck.table1`
SET promotion_id = ARRAY(
SELECT AS STRUCT * REPLACE (100 AS PromotionId ) FROM UNNEST(promotion_id)
)
But when I try to reuse the same to update multiple columns based on table2 data I am getting exceptions.
I am trying to update table1 with table2 data whenever the table1.value1=table2.value with all the nested columns.
As of now, both tables are having a similar schema.
I need to update all the columns in table1 whenever table1.value1=table2.value
... both tables are having a similar schema
I assume by similar you meant same
Below is for BigQuery Standard SQL
You can use below query to get combining result and save it back to table1 either using destination table or CREATE OR REPLACE TABLE syntax
#standardSQL
SELECT AS VALUE IF(value IS NULL, t1, t2)
FROM `project.dataset.table1` t1
LEFT JOIN `project.dataset.table2` t2
ON value1 = value
I have not tried this approach with UPDATE syntax - but you can try and let us know :o)
I'd like to create 3-4 separate temporary tables within a single BigQuery query (all of the tables are based on different data sources) and then join them in various ways later on in the query.
I'm trying to do this by using multiple WITH statements, but it appears that you're only able to use one WITH statement in a query if you're not nesting them. Each time I've tried, I get an error saying that a 'SELECT' statement is expected.
Am I missing something? I'd prefer to do this all in one query if at all possible.
I don't know what you mean by "temporary tables", but I suspect you mean common table expressions (CTEs).
Of course you can have a query with multiple CTEs. You just need the right syntax:
with t1 as (
select . . .
),
t2 as (
select . . .
),
t3 as (
select . . .
)
select *
from t1 cross join t2 cross join t3;
bq mk --table --expiration [INTEGER] --description "[DESCRIPTION]"
--label [KEY:VALUE, KEY:VALUE] [PROJECT_ID]:[DATASET].[TABLE]
Expiration date will make your table temporary. You can create one table at the time with "bq mk" but you can use this in a script.
You can use the DDL, but here also you can only create one table at the time.
{CREATE TABLE | CREATE TABLE IF NOT EXISTS | CREATE OR REPLACE TABLE}
table_name [( column_name column_schema[, ...] )]
[PARTITION BY partition_expression] [OPTIONS(options_clause[, ...])]
[AS query_statement]
If by "temporary table" you meant "subqueries" this is the syntax you have to use:
WITH
subQ1 AS (
SELECT * FROM Roster
WHERE SchoolID = 52),
subQ2 AS (
SELECT SchoolID FROM subQ1)
SELECT DISTINCT * FROM subQ2;
You should be able to use common table expressions without issue. However, if you have large queries with a significant amount of common table expressions/subqueries you may run into resource issues in BQ specifically regarding the ability for BQ to create an execution plan. Temporary tables have helped me in these scenarios, but there are likely better practices.
CREATE TEMP TABLE T1 AS (
WITH
X as (query),
Y as (query),
Z as (query)
SELECT * FROM
(combination_of_above_queries)
);
CREATE TEMP TABLE T2 AS (
WITH
A as (query),
B as (query),
C as (query)
SELECT * FROM
(combination_of_above_queries)
);
CREATE TABLE new_table AS (
SELECT *
FROM (combination of T1 and T2)
)
Apologies, I just saw happened to come across this question and wanted to share what helped me... hope it is helpful for you.
https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#temporary_tables
I have a PostgreSQL query, where I create temp table by joining another temp table, and table from my database.
DROP TABLE IF EXISTS other_temp_table;
CREATE TEMP TABLE other_temp_table AS
SELECT *
FROM base.main_data
WHERE period_start_time::DATE >= '2017-06-20' AND period_start_time::DATE <= '2017-07-26';
------------------------------------------------------------------------------------------------------------------------
DROP TABLE IF EXISTS first_temp_table;
CREATE TEMP TABLE first_temp_table AS
SELECT *
FROM _temp_table
LEFT JOIN base."UL_parameters"
ON temp_table.base_col::INT = base."UL_parameters".base_col::INT
and temp_table.sec_col::INT= base."UL_parameters".sec_col::INT;
Now, the problem is `ERROR: column "sec_col" specified more than once.
But when I delete sec_col join condition and do just base_col, everything is ok. I think that I need to create an alias, but not sure how.
I think that the problem is that there is a sec_col column in both joined tables. Try to replace select * with select column1, column2, ....
It's a good practice to avoid select * in general, because it can cause errors when the table definition changes.
New SAS user here: I have a dataset (table) created by a PROC SQL statement:
proc sql;
create table work.dm_1 as
select distinct a.subject, c.dsyn as DSYN_DS_1, d.dsyn as DSYN_DS_2
from s_raw.dm_1 a
left join work.edt_eg b
on a.subject=b.subjid
left join s_raw.ds_1 c
on a.subject=c.subject
left join s_raw.ds_2 d
on a.subject=d.subject
where c.DSYN='NO' and d.DSYN='NO';
quit;
Using the results from this table (work.dm_1), I want to modify another (existing) table (work.edt_ecg, created from a previous procedure) to select matching records (using subject in dm_1 and subjid in edt_eg) from the table in the proc sql above, and to update the table work.edt_ecg. I have tried the following:
proc sql;
update table work.edt_eg as
select *
from work.edt_eg where subjid in (select distinct subject from work.dm_1);
quit;
But its not working for me! Any ideas welcome
You should either create a new table or view with the subset of the data you need
create view work.edt_eg_wanted as
select *
from work.edt_eg
where subjid in (select distinct subject from work.dm_1);
or delete all unwanted observations (1) from the existing table
delete *
from work.edt_eg
where subjid not in (select distinct subject from work.dm_1);
(As you did not supply sample data, this code is not tested)
(1) Observation is the word SAS uses for row in a table. This because SAS was originally written by statisticians.
I am trying to update one table from another, im able to update fine as long as the customer record exists, but there are some entries that dont.
To solve this i've tried running the following insert
SELECT *
INTO SalBudgetCust
FROM SalBudgetCust_temp
WHERE NOT EXISTS (
SELECT Customer
FROM SalBudgetCust
WHERE Customer = SalBudgetCust_temp.Customer
)
but im prompted with
There is already an object named 'SalBudgetCust' in the database.
Im stuck at this point... could anyone offer a little guideance?
SELECT INTO implicitly creates the table you name. You should instead use INSERT INTO ... SELECT * FROM ..., so that the existing table is used.
It should be INSERT INTO instead of SELECT * INTO ... like
INSERT INTO SalBudgetCust SELECT * FROM SalBudgetCust_temp
WHERE NOT EXISTS
(
SELECT Customer FROM SalBudgetCust WHERE Customer = SalBudgetCust_temp.Customer
)
The general syntax to insert data of one table into another is :
INSERT INTO new_table
SELECT * FROM old_table
WHERE some_condition;
Where, new_table is the table where you want to insert data, old_table is table from where you are fetching data and some_condition is the expression / condition based upon which you want to fetch data from old table.
You may use other clauses like order by, group by, and even sub queries after where clause.
May refer this SQL INSERT INTO and it's subsequent pages.