create and add query result column to an existing table SQL BQ - sql

I have a table 'zzz' with GpsLatitude and GpsLongitude columns. I want to create a single GPS column (PointWKT) from GpsLatitude and GpsLongitude columns and add it to the existing table 'zzz'.
Query for creating a single column PointWKT:
SELECT
ST_GeogPoint(GpsLongitude, GpsLatitude) AS PointWKT,
FROM
`xxx.yyy.zzz`
I would like to add this query result column PointWKT to an existing table 'zzz'. Correct answer:
SELECT DateTime, SerialNumber, TotalWorkingHours, ...
ST_GeogPoint(GpsLongitude, GpsLatitude) AS PointWKT,
FROM
`login-eko.telematics_latest_K2020.tractor_reports_p` BoundaryWKT

This answer was provided by #Kuki.
You just use ST_GeogPoint(GpsLongitude, GpsLatitude) AS PointWKT
within you select statement, as follows:
SELECT DateTime, SerialNumber, TotalWorkingHours, ...
ST_GeogPoint(GpsLongitude, GpsLatitude) AS PointWKT,
FROM
`project.dataset.table` BoundaryWKT

Related

How to create a column in SQL containing using the AVG() function?

I have a table with one column and 46 rows and I want to create another column that contains the average of the first column in all 46 rows
e.g.
This is the table:
CREATE TABLE table2
SELECT column1
FROM table1
I want to add another column that contains for each row (46 rows) the value of AVG(column1)
How do can it be done?
You wouldn't use create table to add a column. In this case, a query using a window function is sufficient:
SELECT t1.*, AVG(column1) OVER () as avg_column1
FROM table1 t1;
This is Standard SQL and should work in any database.

How to create a new column in query then populate with a simple calc X*Y

I have two columns. One is MW and the other is Price. I want to create a sql query that creates a third imaginary column called AwardPrice, and within that it will be one column (MW) * another column (Price). These two columns already exist in the same fact table.
I would recommend a computed column:
alter table t add AwardPrice as (MW * Price);
This value is calculated when you query the table, so the value is always accurate and up-to-date.
How to create a new column in query . . .
I want to create a sql query that creates...
I suppose that you want to do that in a SELECT statement as
SELECT *, MW * Price As AwardPrice
FROM YourTableNameHere;

Split Hive table on subtables by field value

I have a Hive table foo. There are several fields in this table. One of them is some_id. Number of unique values in this fields in range 5,000-10,000. For each value (in example it 10385) I need to perform CTAS queries like
CREATE TABLE bar_10385 AS
SELECT * FROM foo WHERE some_id=10385 AND other_id=10385;
What is the best way to perform this bunch of queries?
You can store all these tables in the single partitioned one. This approach will allow you to load all the data in single query. Query performance will not be compromised.
Create table T (
... --columns here
)
partitioned by (id int); --new calculated partition key
Load data using one query, it will read source table only once:
insert overwrite table T partition(id)
select ..., --columns
case when some_id=10385 AND other_id=10385 then 10385
when some_id=10386 AND other_id=10386 then 10386
...
--and so on
else 0 --default partition for records not attributed
end as id --partition column
from foo
where some_id in (10385,10386) AND other_id in (10385,10386) --filter
Then you can use this table in queries specifying partition:
select from T where id = 10385; --you can create a view named bar_10385, it will act the same as your table. Partition pruning works fast

Renaming Column with name count( distinct( id )) in Hive Table

I forget to give alias to a derived column while creating a table in HIVE from SELECT Statement.
CREATE TABLE temp AS (SELECT date, count(distinct(id)) FROM some_table GROUP BY date);
Now I want to query on the newly created temp table with filters on the derived column, I don't want to drop the table and run the same query again with alias for the derived column.
How can I filter on the column count(distinct(id)), or rename that column?
You can see column names using DESCRIBE:
DESCRIBE temp;
col1 string
_c1 bigint
Then just rename the column:
ALTER TABLE temp CHANGE `_c1` `cnt` bigint;
Or use _c1 column name:
SELECT `_c1` FROM temp2;
Simply use:
ALTER TABLE yourtable CHANGE `columnname` `newname` BIGINT;

SQL how to create and import only specific columns from table A to a new table, table B

In Table A i have many fields like referenceid, amount, timestamp, remarks, status, balancebefore, balanceafter, frmsisdn, tomsisdn, id etc etc
I want to create a new table, Table B based of Table A(with column names, datatypes etc etc) but i only need specific columns that are in table A.
I tried select * into TableB from TableA where 1 = 2 but it says ORA-00905: missing keyword. I am using TOAD.
thank you
In Oracle, the correct syntax is create table as. SELECT INTO is used primarily in SQL Server and Sybase.
create table tableb as
select . . .
from tableA;
Only include the where clause if you don't actually want to insert any rows.
In MySQL the syntax is the same as Oracle's (see here).
Notice that the new table does not contain any constraints from the original table (indexes, keys, etc.)