How to make an alias to a query in Hive? - sql

I have a long query and I'm looking for a way to simplify it for the executer.
For example, I have this query:
select function_1(r_set) from (select collect_set(records) as r_set from (select function_2(<column_name>) as records from <table_name>) as record_t) as record_set_t;
function_1 and function_2 are custom UDFs.
Since everything besides the table name and column name are constant, Is it possible to define some kind of alias or procedure to a query, with column name and table name as a parameter?
or even wrap it somehow with shorter execution command?
I look for something like:
# set alias for long query somehow
set MyQueryAlias = select function_1(r_set) from (select collect_set(records) as r_set from (select function_2(<column_name>) as records from <table_name>) as record_t) as record_set_t;
# execute the query with table name and column name as a parameter
exec MyQueryAlias <table_name> <column_name>
My purpose is to make it easy for other users to use the saved query on different tables and columns.

Related

Big Query For-In not picking up table paths from a look-up table

I have a look-up table containing a list of fully qualified table paths in a Big Query table called all_tables. For example
|table_list|
|----------|
|project_name.dataset_name1.table_1|
|project_name.dataset_name2.table_1|
|project_name.dataset_name3.table_1|
|project_name.dataset_name4.table_1|
|project_name.dataset_name5.table_1|
I am trying to iterate through these tables to pull out elements I need for another procedure using the for-in syntax in Big Query. This is a simplified version of the query I am using
```
FOR table IN (select * from my_project.my_dataset.all_tables)
DO
select * from table;
END FOR;
```
This isn't working. It picks up the list of tables correctly, but when it substitutes the dataset name in the line 3 select statement, it says
**Invalid value: Table "table" must be qualified with a dataset (e.g. dataset.table)**
I know what the error is, but I am not sure how to make it 'see' the value of table as a table path.
All paths are correct, and I am doing it this way as I am querying multiple tables across multiple datasets for a table creation query.
You should a dynamic sql to refer the table name as a variable, so consider below query:
FOR table IN (select * from my_project.my_dataset.all_tables)
DO
EXECUTE IMMEDIATE FORMAT("""
SELECT * FROM %s;
""", table.table_list);
END FOR;

BigQuery CREATE TABLE with dynamic table name [duplicate]

I am trying to write a BigQuery script that I can store as a procedure, I would like one of the arguments I pass to be used in the table name that is written out by the script, for example:
DECLARE id STRING;
SET id = '123';
CREATE OR REPLACE TABLE test.id AS(
SELECT * FROM dataset.table
)
However, in this example the table is created with the name id rather than the value of the "id" variable, 123. Is there any way I can dynamically create a table using the value of a declared variable in the BigQuery UI?
Why not just use Execute Immediate with concat if you know the table schema?
EXECUTE IMMEDIATE CONCAT('CREATE TABLE `', id, '` (column_name STRING)');
So far we have officially announced BigQuery scripting document, which is still in Beta phase, leveraging usage of dynamic parameters (variables) as a placeholders for values in SQL queries . However, according to Parameterized queries in BigQuery documentation, query parameters can't be used for SQL object identifiers:
Parameters cannot be used as substitutes for identifiers, column
names, table names, or other parts of the query.
Maybe you can use a wildcard table. You would create a wildcard table with all subtables you want to query and use the WHERE clause to select any subtable you want. Just be careful, the tables must have the same schema.

Select columnName,* from table

I am a newbie in Oracle SQL, though I have experience in SQL Server.
In SQL Server, to select the rows from a table with a particular column in front:
select columnName,* from tableName
In Oracle:
select columnName,* from tableName
gives error ORA-00936: missing expression, as below:
Please guide.
I can't view images, but here's what I think you need:
select t.column_name, t.*
from table_name t
i.e. you should prefix that particular column name with a table alias ("t"), and then use the same alias with the asterisk ("t.*") to retrieve all table columns.
In Oracle, if you need to view a column but also all columns, you need to define an alias for the table.
Select columnName, A.*
from tableName A;
few things we need to keep it in mind
Alias name in sql - used to derive the individual column name via select query
When you are going to use *[select all] you don't have to worry about the alias name
But when you try to pull all the columns and some specific fields you want to filter then you should go for "Alias"
Alias its object key to refer the inter column
select stu.studentName,stu.* from student stu;

Selecting column names and table names of a select statement

How can I select the column name and table name from a SQL?
I tried something like this but it didn't work:
select column_name, table_name from (select * from users);
This might sound silly, but I have a list of different SQLs and I need to extract their columns and tables into a list. So some of the statements could me:
select username, password from users
select createdate from userlog
select * from dept
...
If I can select the column name and table name of a select statement, then I should get, say for the first statement, username and password for columns and users for table name. And createdate for column and userlog for table name in the second statement.
Then if it all works, I can then loop through the list of select statements and extract their column and table names.
The below query worked for Oracle database.
SELECT COLUMN_NAME,TABLE_NAME FROM ALL_TAB_COLUMNS
You can see more about information-schema
Edit:
You may try like this:
SELECT COLUMN_NAME,TABLE_NAME FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME IN (SELECT ColumnName FROM users)
You need to parse the SQL statement so the SQL engine figures out the columns and datatypes of the columns that the statement returns.
How you do it best depends on what environment you are using. In some programming languages when you create a SqlPreparedStatement or OraCommand or whatever the object may be called, that object may have a metadata collection populated with column information after parsing.
If you are doing it in the database itself, parsing your statement with DBMS_SQL can get you the information you need. See Example 8 in the documentation at this link:
http://docs.oracle.com/database/121/ARPLS/d_sql.htm#ARPLS68205
--
Oh, and this gives you column names of the select statement. The table names I do not know of any way to get easily.

Oracle, dynamically determine column name

Suppose I have a table of custom column names that all have the pattern COL##, where ## is any integer. So a typical query would be:
select COL12 from MyCustomTable;
So in another table, I have all those integers and I'd like to create a query using the table of integers to construct a dynamic query into MyCustomTable.
Something like:
select 'COL' || (select colId from IdTable where Id = 12) from MyCustomTable;
But instead of just returning the string 'COL12' for every row, return the actual values identified by the column name COL12.
Don't worry about my overall problem :) I'm just curious to know if I can do this from a sqldeveloper window directly without writing any code/procedures/functions, etc.
An obvious and absolutely insecure way of doing this would be usage of EXECUTE IMMEDIATE statement.
Another is to use SPOOL command to output results into temporary file, then set SPOOL OFF and execute this file with # directive.