in oracle query, select only few columns with union - sql

I have two tables something like this:
TABLE_1:
COL_A (int), COL_B (float), COL_C (float)
and
TABLE_2:
COL_A (int), COL_B (varchar), COL_C (varchar)
My query is using a UNION to get only COL_A(int) from table 2 like
SELECT COL_A, COL_B, COL_C FROM table1 UNION
SELECT COL_A FROM table2
It's throwing an error. How do we get the results?

All subquery members of a UNION must have the same number and types of columns. In your case the first subquery has three columns, but the second one has only one.
Solution: pad the second subquery with nulls.
For example:
select COL_A, COL_B, COL_C from table1
union
select COL_A, null, null from table2

Related

SQL cannot use IN with CTE but works with the same subquery

QUESTION
When I save my table into a CTE, then use it with IN in another query it is considered as error.
WITH id_list AS (
SELECT DISTINCT tracking_id
FROM table_a
)
SELECT col_a, col_b, col_c
FROM table_b
WHERE tracking_id IN (id_list)
But in the same time, if I put the same query as subquery with IN it works correctly.
SELECT col_a, col_b, col_c
FROM table_b
WHERE tracking_id IN (
SELECT DISTINCT tracking_id
FROM table_a
)
I want to know the reason why this situation occur. Why can't we use CTE table directly with IN?
Thanks.

Can I use column names as data

I have to check the data in each column in 7000 tables.
So I want to insert into Temp_Table has Columns ( Table_name, Column_name, Data)
eg.
SELECT * FROM TABLE_A;
COL_A
COL_B
COL_C
DATA_A1
DATA_B1
DATA_C1
DATA_A2
DATA_B2
DATA_C2
I want to insert into Temp_table
eg.
TABLE_NM
COL_NM
DATA
TABLE_A
COL_A
DATA_A1
TABLE_A
COL_A
DATA_A2
TABLE_A
COL_B
DATA_B1
TABLE_A
COL_B
DATA_B2
TABLE_A
COL_C
DATA_C1
TABLE_A
COL_C
DATA_C2
Is there any way?
you can do this using union all from metadata.
select 'TABLE_A' TABLE_NM, 'COL_A' COL_NM, COL_A DATA from table_a
union all
select 'TABLE_A' TABLE_NM, 'COL_B' COL_NM, COL_B DATA from table_a
union all
select 'TABLE_A' TABLE_NM, 'COL_C' COL_NM, COL_C DATA from table_a
...
Put the list of table and columns in XL and then create a formula to create the whole query. But list of tables and columns - you need to get from metadata.

Oracle SQL Query based on a UNION

env: Oracle 12c
I have a query in Oracle that is a UNION between two tables: TABLE_A and TABLE_B.
select ID as COL_A,
VAL_B as COL_B,
VAL_C as COL_C,
VAL_D as COL_D,
VAL_E as COL_E
from TABLE_A
where VAL_E = 'XYZ12'
union
select NULL as COL_A,
NULL_B as COL_B,
VAL_C as COL_C,
NULL as COL_D,
VAL_E as COL_E
from TABLE_B
where VAL_E = 'XYZ12'
and ?????
COL_A COL_B COL_C COL_D COL_E
------ ------ ------ ----- -----
123 REQ1 REQ2 REQ3 XYZ12
REQ2 XYZ12
Based on the query above and where I am stuck is that if the record with 'XYZ12' and 'REQ1' exists then I only want to return this one record otherwise return the second record only, i.e. where 'XYZ12' exists but COL_B IS NULL
The primary record is the first one if it exists in TABLE_A. Based on my criteria, if it doesn't exist then return the second record alone from TABLE_B.
Based on the data and query in your question, table_a would seem to have at most one row. Hence you can simply add:
from TABLE_B
where VAL_E = 'XYZ12' and
not exists (select 1 from table_a a)
If you want to check for specific values in table_a, then you can add a filter to the subquery.

Create fake indexing column in Teradata SQL

I have the statement:
SELECT col_a, col_b, col_c from database.table sample 50;
I want my output to look like this:
id col_a col_b col_c
1 data goes here
2 data goes here
3 data goes here
4 data goes here
5 data goes here
6 data goes here
Basically - I need to create a column for id that starts at 1 and auto-increments with each row that's retrieved.
How do I do this?
If you actually want the sampled rows numbered:
SELECT ROW_NUMBER() OVER (ORDER BY some_field),
dt.*
FROM
(
SELECT col_a, col_b, col_c
FROM database.table
SAMPLE 50
) AS dt
Instead of SAMPLE you might also use TOP, but then it's not random anymore.
You can use row_number():
select row_number() over (order by ??) as id, col_a, col_b, col_c
from t;
The ?? represents the column/expression the gives the ordering for the id.

Does Hive use NULL as a possible value to aggregate by?

When I aggregate data in hive, how does the group by statement treat NULL values in the aggregating column?
Say I launch the following query
select col_a, count(1) from mytable group by col_a ;
and that col_a contains 0, 1 and NULL values. Will the result have 2 rows (0 and 1)
or 3 (0,1 and NULL)?
Hive does group by on NULL values and you will get 3 values. Also please modify your query to this:
Select col_a, count(1) from mytable group by col_a ;