Oracle SQL Query based on a UNION - sql

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.

Related

"Adding" GROUP BY's and ORDER BY's in one query instead of separate

I have a base query that has a few variations, all of them are different from each other in the GROUP BY and ORDER BY clauses.
These are the variations:
SELECT SUM(col_a) AS "col_a", SUM(col_b), AS "col_b", SUM(col_c) as "col_c"
FROM my_table
GROUP BY col_a
SELECT SUM(col_a) AS "col_a", SUM(col_b), AS "col_b", SUM(col_c) as "col_c"
FROM my_table
GROUP BY col_a, col_b
ORDER BY col_a
SELECT SUM(col_a) AS "col_a", SUM(col_b), AS "col_b", SUM(col_c) as "col_c"
FROM my_table
GROUP BY col_a, col_b, col_c
ORDER BY col_a, col_b
Is it possible to do it in same query? Or I should get the base data and sum it server side/client side?
In MySQL (which was the original question) you can do it in the same query using WITH ROLLUP:
select sum(col_a) as col_a, sum(col_b) as col_b, sum(col_c) as col_c, count(*),
case (col_a is not null) + (col_b is not null) + (col_c is not null) when 3 then 'col_a,col_b,col_c' when 2 then 'col_a,col_b' when 1 then 'col_a' end grouped_by
from my_table
group by col_a, col_b, col_c with rollup
having grouped_by is not null
order by grouped_by,col_a, col_b, col_c
fiddle
Yes, you can use GROUPING SETS or ROLLUP for this. The former is more flexible, if a little more verbose.
SELECT
SUM(col_a) AS col_a,
SUM(col_b) AS col_b,
SUM(col_c) as col_c
FROM my_table
GROUP BY GROUPING SETS (
(col_a),
(col_a, col_b),
(col_a, col_b, col_c)
)
ORDER BY col_a, col_b, col_c;
To identify the rolled up rows, you can do something like this
SELECT
CASE WHEN GROUPING(col_b) = 0 AND GROUPING(col_c) = 0
THEN 'By A, B, C'
WHEN GROUPING(col_b) = 0
THEN 'By A, B'
ELSE 'By A'
END AS GroupingType,
SUM(col_a) AS col_a,
SUM(col_b) AS col_b,
SUM(col_c) as col_c
FROM my_table
GROUP BY GROUPING SETS (
(col_a),
(col_a, col_b),
(col_a, col_b, col_c)
)
ORDER BY col_a, col_b, col_c;

in oracle query, select only few columns with union

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

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.

Sybase insert statement with subqueries of select

I am trying to do an insert with subqueries but this insert fails:
insert into
TABLE_A(COL_A, COL_B, COL_C, COL_D, COL_E, COL_F)
values (
1,
(select COL_B from TABLE_B where user_name = 'foo'),
(select COL_C from TABLE_C where age = 25),
2,3,4);
I tried to write it different but it still fails.
You want insert . . . select syntax. The values is not needed:
insert into TABLE_A(COL_A, COL_B, COL_C, COL_D, COL_E, COL_F)
select 1, (select COL_B from TABLE_B where user_name = 'foo'),
(select COL_C from TABLE_C where age = 25),
2, 3, 4;
You could also write this as a cross join if you prefer:
insert into TABLE_A(COL_A, COL_B, COL_C, COL_D, COL_E, COL_F)
select 1, b.COL_B, c.COL_C,
2, 3, 4;
from (select COL_B from TABLE_B where user_name = 'foo') b cross join
(select COL_C from TABLE_C where age = 25) c

Microsoft Access 2003 SQL Question

I need to union several tables with the different structure in Microsoft Access.
For example I have table:
table1 (column_a,column_b,column_c),
table2 (column_a,column_c),
table3 (column_d)
SQL looks like follows:
SELECT table1 column_a,column_b,column_c, Null as column_d FROM Table1
UNION
SELECT table2 column_a,Null as column_b, column_c, Null as column_d FROM Table2
UNION
SELECT table3 column_a, Null as column_b, Null as column_c, Null as column_d
FROM Table3;
But sometimes MS Access shows the error message about incompatible types.
I think this because of generated columns with null values in one SELECT have the type incompatible with corresponding not autogenerated columns in another SELECT
Is there way to specify type of the autogenerated column with null?
What about replacing the Null's with empty strings ('')?
e.g.,
SELECT col_a, col_b, col_c, '' as col_d FROM Table1
UNION
SELECT col_a, '' as col_b, col_c, '' as col_d FROM Table2
UNION
SELECT col_a, '' as col_b, '' as col_c, '' as col_d FROM Table3;