Changing order of columns for a table - google-bigquery

I have a bigquery table with schema as :
CREATE TABLE `abc`
(
col2 STRING,
col1 DATE,
col3 STRING,
);
and after creating and loading months worth of data in it, I realised I want the DDL to look like,
CREATE TABLE `abc`
(
col1 DATE,
col2 STRING,
col3 STRING,
);
I want this change because the upstream ETL code expects it in this way.
Is there a way to achieve this?
PS: drop and create the table isn't an option as it has important data.
Thanks :)

You won't miss any data. Try this.
create or replace table <SCHEMA.NEW_TABLE_NAME> as
select col1,col2,col3 from <SCHEMA.OLD_TABLE_NAME>;

When you make the select you can pass the order you want for your columns.
Instead of selecting it like SELECT * FROM ...
just do it as SELECT col1, col2, col3 FROM ...

Related

How do i append query results to create new columns in dataset on bigquery?

Would like to alter my original dataset to include the results from the query. Currently year and month are connected, would like to split the string and append the results to the original dataframe
I see that you are sourcing the value for your new column from your own source table.
You can do it two ways:
Idea1:
Let's say you have a table with columns:
col1, col2
and you want to add col3, you can always do something like:
CREATE OR REPLACE table your_source_table as
select col1, col2, (your_calculation_for_col3) as col3 from your_source_table
Idea 2:
Add a new column to your table and update value of it like below:
ALTER TABLE your_source_table
ADD COLUMN COL3 DATA_TYPE_FOR_COL3;
UPDATE your_source_table
SET col3 = your_new_calculated_value
WHERE TRUE;
see if any of this helps.

apache hive column comment with CTAS

Sorry for all the setup. This is a hive datatype and comment question.
I have a single file in HDFS which combines 4 sets of table data. Breaking the data out ahead of time is not my preferred option. The first 4 rows specify the column headers:
*1 col1, col2, col3
*2 cola, colb, colc, cold, col5e
etc....
data rows begin with matching number at position 1 of the header.
1 data, data, data,
2 data, data, data, data, data,
etc...
The base hive table is just col0 - col60 for the raw file. I've tried creating a CTAS table to hold all of the "1" columns and one for the "2" columns where I can specify data type, and comments. Since the column names vary, I cannot give the columns names on the base table nor can I comment them with column based metadata.
This DDL didn't work but giving an example of what I'm hoping to do. Any thoughts ?
CREATE TABLE foo (
col1 as meaningful_name string comment 'meaningful comment')
as
SELECT col1
FROM base_hive table
WHERE col1 = 1;
CREATE TABLE foo
as
SELECT col1 string comment 'meaningful comment'
FROM base_hive table
WHERE col1 = 1;
thanks TD
I dont understand much what you are trying to achieve here, but looking at your DDL, I can see some errors. For the correct CREATE TABLE AS SELECT implementation, pl use the below DDL:
CREATE TABLE foo (
col1 STRING COMMENT 'meaningful comment')
AS
SELECT col1 AS meaningful_name
FROM base_hive table
WHERE col1 = 1;

Exporting Result Set to Another Table?

How can you export a result set given by a query to another table using SQL Server 2005?
I'd like to accomplish this without exporting to CSV?
INSERT INTO TargetTable(Col1, Col2, Col3)
SELECT Col1, Col2, Col3
FROM SourceTable
insert into table(column1, columns, etc) select columns from sourcetable
You can omit column list in insert if columns returned by select matches table definition. Column names in select are ignored, but recommended for readability.
Select into is possible too, but it creates new table. It is sometimes useful for selecting into temporary table, but be aware of tempdb locking by select into.
SELECT col1, col2, ...
INTO dbo.newtable
FROM (SELECT ...query...) AS x;

Can I set a formula for a particular column in SQL?

I want to implement something like Col3 = Col2 + Col1 in SQL.
This is somewhat similar to Excel, where every value in column 3 is sum of corresponding values from column 2 and column 1.
Have a look at Computed Columns
A computed column is computed from an
expression that can use other columns
in the same table. The expression can
be a noncomputed column name,
constant, function, and any
combination of these connected by one
or more operators.
Also from CREATE TABLE point J
Something like
CREATE TABLE dbo.mytable
( low int, high int, myavg AS (low + high)/2 ) ;
Yes, you can do it in SQL using the UPDATE command:
UPDATE TABLE table_name
SET col3=col1+col2
WHERE <SOME CONDITION>
This assumes that you already have a table with populated col1 and col2 and you want to populate col3.
Yes. Provided it is not aggregating data across rows.
assume that col1 and col2 are integers.
SELECT col1, col2, (col1 + col2) as col3 FROM mytable

Need SQL help - How can I select rows to perform an insert?

I tried to make the title as clear as possible... here is my scenario:
I have 2 tables (let's call them table A and table B) that have a similar schema. I would like write a stored procedure that would select specific columns of data out of table A, and insert this data as a new record in table B.
Can someone point me in the write direction to make such a query? I am unsure how to "Hold" the values from the first query, so that I may then perform the insert.
I am trying to avoid making a query, processing it with C# and then making another query...
Thanks.
INSERT INTO B (Col1, Col2) SELECT Col1, Col2 FROM A
Is this what you mean?
You can do this as a single query from C# like this:
Insert into tableB (col1, col2, col3) select col1, col2, col3 from tableA where ...
The trick is that column names need to be in the same order and compatible types.
use a SELECT INTO
SELECT
[Col1],
[COl2]
INTO TableA
FROM TableB