I need the count of columns in hive ,So below is the example.
Table_name:emp
columns:
empno,
ename,
manager,
dept_id
Expected output:
4
Hive (now) supports the information_schema tables, so you could do:
select count(*)
from information_schema.columns
where table_name = 'emp'
Related
For example I was using oracle sql:
SELECT rpad(salary, 10 , '*')
from employees;
At the "10" part, I wish I can use a function to directly return the byte of the data type.
I tried
SELECT rpad(salary, salary, '*')
from employees;
and the result was scary.
is there any way to do it, or I have to create a function in plsql?
You can fill to max length of data in the column in the whole table using the below:
select rpad( salary,
max(length(salary)) over (partition by (select null from dual)),
'*')
from employees
Or to full possible size using data_length in user_tab_columns table
select rpad( salary,
(select data_length from user_tab_columns
where table_name = 'EMPLOYEES' and
column_name = 'SALARY' ),
'*') from employees
I have a table with 4 columns named StudentId,StudentName,SubjectNAme,SubjectMark.
I want write a Query to get a view with two Fields Named StudentName and Total. The Total is Obtained by Summing the subject marks of corresponding students.
SELECT StudentName,
sum(SubjectMark)
FROM table_name
GROUP BY StudentId,
StudentName
Syntax:
SELECT SUM(column_name) FROM table_name;
column_name is the name of coloumn you want to sum
table_name is table name.
i have a oracle table EMP columns are NAME,AGE,DEPT.
now i want to retrive data from EMP using "select statement";
Select Name, age, dept from Emp;
select Dept, age, emp from Emp;
Which one will take less time to retrieve data?
Or will the retrieve time not be different?
you can use
Select Name, Age, Dept From EMP
Or
if you have only Three columns in your EMP table than you also use
Select * from EMP.
both take same time..
in your question your second query is time consuming because you use, two time dept column in it.
for Better performance you can create index on EMP Table.
I am providing SQL that prompts the user for the name of a table. This SQL will provide a report of column names, data types, data lengths, data precision and indicate whether nulls are allowed for the specific table.
Here is the query I am running:
SELECT employee_id, first_name, last_name
FROM employees
GROUP BY employee_id;
select department_id where employee_id = :table_name;
set echo
(select column_name, data_type, data_length,
data_precision, data_scale, nullable
from all_tab_columns
where table_name = 'employees'
order by column_name);
I get a window where a user is supposed to enter the table name but the second select statement fails to run. It comes up with ORA-00933: sql command not properly ended.
If I run the second select statement separately I am getting a no data found message.
First, You're missing the table name in this query:
select department_id where employee_id = :table_name;
It's should be:
select department_id from employees where employee_id = :table_name;
Second
set echo
Will give you an error:
SP2-0265: echo must be set ON or OFF
You must add ON or OFF parameter for that command.
For example :
set echo ON;
More: set echo
Third:
select column_name, data_type, data_length,
data_precision, data_scale, nullable
from all_tab_columns
where table_name = 'employees'
order by column_name
Maybe the comparison in where clause is case sensitive.
Try this:
select column_name, data_type, data_length,
data_precision, data_scale, nullable
from all_tab_columns
where lower(table_name) = 'employees'
order by column_name
Your select department_id where employee_id = :table_name;
is wrong.
The table is to be specified in the from clause.
Thanks
How to select only one column values without using functions first , last , top , limit in a table ?
Want to use only where command:
ename age
hansen 25
hansen 25
hansen 25
How to select only one values hansen 25 ?
in the above table
Then use DISTINCT:
SELECT DISTINCT ename, age
FROM TableName;
Update: If don't need to use DISTINCT, then you can do the following if you are using sql server:
SELECT ename, age
FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY ename DESC) AS row, *
FROM RableName
) sub
WHERE sub.row = 1;
Demo