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

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;

Related

How to create partitions (year,month,day) in hive from date column which have MM/dd/yyyy format

Data loaded on a daily basis.
Need to create a partition with the date column.
Date
3/15/2021 8:02:32 AM
12/21/2020 12:20:41 PM
You need to convert the table into a partition to the table. Then change the loading sql so that it inserts into the table properly.
Create a new table identical to original table and make sure the exclude partition column from list of columns and add it in partitioned by like below.
create table new_tab() partitioned by ( partition_dt string );
Load data into new_tab from original table. Make sure last column in your select clause is the partitioned col.
set hive.exec.dynamic.partition.mode=nonstrict;
insert into new_table partition(partition_dt )
select src.*, from_unixtime(unix_timestamp(dttm_column),'MM/dd/yyyy') as partition_dt from original_table src;
Drop original table and rename new_table as original table.
drop table original_table ;
alter table new_table rename to original_table ;

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

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

SQL: How to create new table with both data from old table and new empty columns

I wish to make a new table with some data from the old, but also with new empty columns I can edit.
I would start with:
CREATE TABLE new_table AS SELECT ID, title, summary FROM old_table;
and then alter the table with new columns:
ALTER TABLE new_table ADD note datatype;
But I need my new empty column to be between title & summary, and not 'in the end' as ALTER gives me.
I would like to combine CREATE TABLE and CREATE TABLE AS SELECT - but is that possible?
What about:
CREATE TABLE new_table AS SELECT ID, title, '' AS note, summary FROM old_table;
If you wanted to have a specific datatype, you could try CAST. For example, CAST(1 as INTEGER) AS note

Need to add a constant value in a column while loading a hive table

I created a table named table1 in hive and i need to insert data from table2 into table1. I used the below statemnt to get the output.
Also i need to add a new column with some constant value -- colx = 'colval' along with the columns in table2 but am not sure how to add it.. Thanks!
INSERT INTO TABLE table1 select * FROM table2;
If you are willing to drop table1 and recreate it from scratch, you could do this:
-- I'm using Hive 0.13.0
DROP TABLE IF EXISTS table1;
CREATE TABLE table1 AS SELECT *, 'colval' AS colx FROM TABLE2;
If that is not an option for some reason, you can use INSERT OVERWRITE:
ALTER TABLE table1 ADD COLUMNS (colx STRING); -- Assuming you haven't created the column already
INSERT OVERWRITE TABLE table1 SELECT *, 'colval' FROM table2;

Change column names that start with 'C' letter using sql?

Assuming I have this table:
What I want to do is change all column names that starts with letter C to another letter or another word.
I'm not very good in SQL, I have ability to do this using program languages like Java or any other.
But my purpose is using only SQL.
Is it possible?
You need to drop and recreate the table.
So:
1)Create a temp table with all the data and structure of the original table. This allows you to drop the original, then recreate it using the same name.
Create Table temp as select * from [Table_Name]
2)Drop the original table.
Drop table [Table_Name]
3)Create the new table, with the new column names.
Create Table Table_Name
(Name varchar(20),
Age int,
[New_C1] int,
[New_C2] int),
[New_C3] int),
)
4)Insert the data from the temp table into the new table:
Insert into Table_Name (Name, Age, New_C1, New_C2, New_C3)
Select (Name, Age, C1, C2, C3) FROM temp
5)Drop the temp table:
Drop table temp
Note
I highly recommend wrapping this in a transaction, that way you can rollback.