add all column names with a prefix in Hive - hive

I have a table with thousands of columns, need to merge with the other table with thousands of columns. I need to add pre-fix to avoid conflict, any quick way to change all columns in one table with a pre-fix?

If you have necessary permissions to update meta-store you can try this query which will update all the column names of your table with a prefix
UPDATE COLUMNS_V2 C JOIN TBLS T
ON C.CD_ID= T.TBL_ID AND
T.TBL_NAME='table_name' SET C.COLUMN_NAME =CONCAT('prefix_',C.COLUMN_NAME) ;
in the above query , replace the following :
'table_name': table name with columns for which you wanted to add prefix for.
'prefix_' : prefix of your choice , example 'tbl_'

Related

Fetch records from two tables of database along with table name

I have two tables in my database
dbo.Locations
dbo.Units
dbo.Units contains foreign key Location_Id
Now, I need to search a string which could be contains by any columns of above stated tables.
My requirement is that I need to fetch all columns along with "TABLE NAME".
Columns fetched successfully but how could I fetch table name whose column contains searchable text.
How could query for above problem made?
You can alias the column name in the select to include the table name:
select table1.somecolumn as Table1_somecolumn
from Table1
If you want to select a constant value, you can just select and name it:
select . . ., 'table name' as table_name
from . . .

Hive - Create Table statement with 'select query' and 'fields terminated by' commands

I want to create a table in Hive using a select statement which takes a subset of a data from another table. I used the following query to do so :
create table sample_db.out_table as
select * from sample_db.in_table where country = 'Canada';
When I looked into the HDFS location of this table, there are no field separators.
But I need to create a table with filtered data from another table along with a field separator. For example I am trying to do something like :
create table sample_db.out_table as
select * from sample_db.in_table where country = 'Canada'
ROW FORMAT SERDE
FIELDS TERMINATED BY '|';
This is not working though. I know the alternate way is to create a table structure with field names and the "FIELDS TERMINATED BY '|'" command and then load the data.
But is there any other way to combine the two into a single query that enables me to create a table with filtered data from another table and also with a field separator ?
Put row format delimited .. in front of AS select
do it like this
Change the query to yours
hive> CREATE TABLE ttt row format delimited fields terminated by '|' AS select *,count(1) from t1 group by id ,name ;
Query ID = root_20180702153737_37802c0e-525a-4b00-b8ec-9fac4a6d895b
here is the result
[root#hadoop1 ~]# hadoop fs -cat /user/hive/warehouse/ttt/**
2|\N|1
3|\N|1
4|\N|1
As you can see in the documentation, when using the CTAS (Create Table As Select) statement, the ROW FORMAT statement (in fact, all the settings related to the new table) goes before the SELECT statement.

BigQuery: Append to table from select with nested record ('Insert into tablename select')

Hi is there a way to append the selected result with a nested column into an existing table?
BigQuery seems not to support 'insert into tablename select...' so i tried it over the .net-api. It works fine but if my select contains a nested record i will get the error (with or without flatten-result-flag):
'Field Products from table oxidation.2016_91 is not a leaf field. '
The tableschema for this column in the destination table is the same.
It seems only to work if i write out the column Name in the nested column, but i want to have the destination table structure to stay the same.
If schemas the same, below should work

Sql Column had no values

I have a sql table that I am trying to add a column from another table to. Only when I execute the alter table query it does not pull the values out of the table to match the column where I am trying to make the connection.
For example I have column A from table 1 and column A from table 2, they are supposed to coincide. ColumnATable1 being an identification number and ColumnATable2 being the description.
I tried this but got an error...
alter table dbo.CommittedTbl
add V_VendorName nvarchar(200)
where v_venkey = v_vendorno
It tells me that I have incorrect syntax... Anyone know how to accomplish this?
alter table dbo.CommittedTbl
add V_VendorName nvarchar(200);
go
update c
set c.V_VendorName = a.V_VendorName
from CommittedTbl c
join TableA a
on c.v_venkey = a.v_vendorno;
go
I'm just guessing at your structure here.
alter table 2 add column A <some_type>;
update table2 set column A = (select column_A from table2 where v_venkey = v_vendorno);
Your names for tables and columns are a bit confusing but I think that should do it.
There is no WHERE clause for an ALTER TABLE statement. You will need to add the column (your first two lines), and then insert rows based upon a relationship you define between the two tables.
ALTER TABLE syntax:
http://msdn.microsoft.com/en-us/library/ms190273%28v=sql.90%29.aspx
There are several languages within SQL:
DDL: Data Definition Language - this defines the schema (the structure of tables, columns, data types) - adding a column to a table affects the table definitions and all rows will have that new column (not just some rows according to a criteria)
DML: Data Manipulation Language - this affects data within a table, and inserting, updating or other changes fall into this and you can update some data according to criteria (and this is where a WHERE clause would come in)
ALTER is a DDL statement, while INSERT and UPDATE are DML statements.
The two cannot really be mixed as you are doing.
You should ALTER your table to add the column, then INSERT or UPDATE the column to include appropriate data.
Is it possible that you want a JOIN query instead? If you want to join two tables or parts of two tables you should use JOIN.
have a look at this for a start if you need to know more LINK
hope that helps!

Can't get SQL statement to update from 2 Tables?

I currently have a database with two tables in it one hold lets say job details and the other company details. The "company_name" exists in the "Jobs" table which will have a matching entry in the "Companies" table under the field of "name". I want to basically set the field in the "Companies" table of "comp_id" to be the value of the field "id" in the "Jobs" table, WHERE the "name" in the "Companies" table is equals to the "company_name" in the "Jobs" table.
I have create the query below which i believed should work however it returns no rows affected?? can anyone please help me with this?
UPDATE `jobs`, `companies`
SET `comp_id` = 'companies.id'
WHERE ('companies.name' = 'jobs.company_name')
Thanks
This condition:
WHERE ('companies.name' = 'jobs.company_name')
is one problem (unless this is a copy & paste error during posting)
You are comparing two string literals there (and of course as they are not the same, you will never update anything).
The reason is you are using single quotes which denote a string literal. To quote column names you either need to use double quotes or the backticks you had used before (assuming you are on MySQL).
Reading your question you are only updating one table not two?
I want to basically set the field in
the "Companies" table of "comp_id" to
be the value of the field "id" in the
"Jobs" table, WHERE the "name" in the
"Companies" table is equals to the
"company_name" in the "Jobs" table.
All you want to do is set the comp_id column in the companies table to the value of the id column in the jobs table where the name and company_name columns are the same?
To do this using MSSQL you would do something like this:
Update c
Set c.comp_id = j.Id
From dbo.Companies c
Join dbo.Jobs j on c.Name = j.Company_Name
You can update only 1 table in an UPDATE statement in SQL Server. To update multiple tables at once you have the following options:
Use Stored procs
Create a View of the select statement and delete from the view
Use triggers.