Oracle sql derived table - optional aliasing - oracle

I came across a client's query yesterday, it is something like:
select count(*) as countall,
person,
location
from (select custid,
min("Date") as theDate,
person,
data.location
from data join
customer on customer.customerid = custid
where status = 1
and custid <> -1
group by custid,
person,
data.location) --[NO ALIAS]
group by person,location
I don't have a lot of hours in Oracle, but in MS SQL this would not fly, to my knowledge. Any time I have used a derived table it throws an error, so to encounter scenarios such as this piques my interest. I couldn't find anything to explain it on the interwebs, hopefully somebody can explain the scenarios where aliasing for derived tables is optional and when it is not.

You only need to alias, when you are referencing a column that is not uniquely defined. This means that the column exists in more than one table/derived table. A reference could be in the select statement, or a join. If all columns are unique, then you do not need an alias.
I prefer to alias all of the time for clarity, and because it helps with Intellisense in PL/SQL.
--ALIAS needed, because the 'a' column referenced is not unique
--this will throw an error
select a, a, b, c
from (select 'A1' as a, 'B1' as b, 'C1' as c from dual),
(select 'A2' as a from dual);
--this will not throw an error
select t1.a, t2.a, b,c
from (select 'A1' as a, 'B1' as b, 'C1' as c from dual) t1,
(select 'A2' as a from dual) t2;
;
--ALIAS not needed for join, because all referenced columns are unique
select a, b, c, d, e, f
from (select 'A' as a, 'B' as b, 'C' as c from dual)
join (select 'D' as d, 'E' as e, 'F' as f from dual)
on a = d;
--ALIAS needed for join, because the 'x' column referenced is not unique
--this will throw an error
select a
from (select 'A' as a, 'B' as b, 'C' as c, 'X' as x from dual)
join (select 'D' as d, 'E' as e, 'F' as f, 'X' as x from dual)
on x = x;
--this will not throw an error
select a
from (select 'A' as a, 'B' as b, 'C' as c, 'X' as x from dual) t1
join (select 'D' as d, 'E' as e, 'F' as f, 'X' as x from dual) t2
on t1.x = t2.x;

In this instance, you are selecting all columns from your subquery and hence the subquery was written with no alias .
If you were to join this subquery with another table or perhaps another subquery, you would want to alias so that you can reference the join columns using the defined alias.

Related

how to select specific columns from three different tables in Oracle SQL

I am trying to select values from three different tables.
When I select all columns it works well, but if I select specific column, the SQL Error [42000]: JDBC-8027:Column name is ambiguous. appear.
this is the query that selected all that works well
SELECT
*
FROM (SELECT x.*, B.*,C.* , COUNT(*) OVER (PARTITION BY x.POLICY_NO) policy_no_count
FROM YIP.YOUTH_POLICY x
LEFT JOIN
YIP.YOUTH_POLICY_AREA B
ON x.POLICY_NO = B.POLICY_NO
LEFT JOIN
YIP.YOUTH_SMALL_CATEGORY C
ON B.SMALL_CATEGORY_SID = C.SMALL_CATEGORY_SID
ORDER BY x.POLICY_NO);
and this is the error query
SELECT DISTINCT
x.POLICY_NO,
x.POLICY_TITLE,
policy_no_count ,
B.SMALL_CATEGORY_SID,
C.SMALL_CATEGORY_TITLE
FROM (SELECT x.*, B.*,C.* , COUNT(*) OVER (PARTITION BY x.POLICY_NO) policy_no_count
FROM YIP.YOUTH_POLICY x
LEFT JOIN
YIP.YOUTH_POLICY_AREA B
ON x.POLICY_NO = B.POLICY_NO
LEFT JOIN
YIP.YOUTH_SMALL_CATEGORY C
ON B.SMALL_CATEGORY_SID = C.SMALL_CATEGORY_SID
ORDER BY x.POLICY_NO);
I am trying to select if A.POLICY_NO values duplicate rows more than 18, want to change C.SMALL_CATEGORY_TITLE values to "ZZ" and also want to cahge B.SMALL_CATEGORY_SID values to null.
that is why make 2 select in query like this
SELECT DISTINCT
x.POLICY_NO,
CASE WHEN (policy_no_count > 17) THEN 'ZZ' ELSE C.SMALL_CATEGORY_TITLE END AS C.SMALL_CATEGORY_TITLE,
CASE WHEN (policy_no_count > 17) THEN NULL ELSE B.SMALL_CATEGORY_SID END AS B.SMALL_CATEGORY_SID,
x.POLICY_TITLE
FROM (SELECT x.*, B.*,C.* , COUNT(*) OVER (PARTITION BY x.POLICY_NO) policy_no_count
FROM YIP.YOUTH_POLICY x
LEFT JOIN
YIP.YOUTH_POLICY_AREA B
ON x.POLICY_NO = B.POLICY_NO
LEFT JOIN
YIP.YOUTH_SMALL_CATEGORY C
ON B.SMALL_CATEGORY_SID = C.SMALL_CATEGORY_SID
ORDER BY x.POLICY_NO);
If i use that query, I got SQL Error [42000]: JDBC-8006:Missing FROM keyword. ¶at line 3, column 80 of null error..
I know I should solve it step by step. Is there any way to select specific columns?
That's most probably because of SELECT x.*, B.*,C.* - avoid asterisks - explicitly name all columns you need, and then pay attention to possible duplicate column names; if you have them, use column aliases.
For example, if that select (which is in a subquery) evaluates to
select x.id, x.name, b.id, b.name
then outer query doesn't know which id you want as two columns are named id (and also two names), so you'd have to
select x.id as x_id,
x.name as x_name,
b.id as b_id,
b.name as b_name
from ...
and - in outer query - select not just id, but e.g. x_id.

Use result of upper query in union clause in lower query

I am using union operator to combine results of 2 queries. Now I want to use the result of first query in second query so that I can exclude some records from second query.
E.g.
select <some_columns> from tableA Union select <same_columns> from tableA where <one_column_val> != <some_val_from_first_query>
Below is my query
SELECT a.*, b.*, c.*
FROM tableA a,
tableB b,
tableC c
where b.field_1 = c.field_1
and a.field_2 = c.field_2
union
SELECT a.*, b.*, c.*
FROM tableA a,
tableB b,
tableC c
where b.field_1 = c.field_1
and a.field_3=c.field_3
and a.field_2 <> {a.field_2 from upper query}
Please suggest the required alteration.
Thanks in advance
Each select in a SQL union has no knowledge of the others. So, you simply have to explicitly write out the entire query again, in your case either as a NOT EXISTS or a NOT IN. As an example, you can see the entire query copied as a derived table in a NOT EXISTS clause:
SELECT a.*, b.*, c.*
FROM tableA a,
tableB b,
tableC c
where b.field_1 = c.field_1
and a.field_2 = c.field_2
union
SELECT a.*, b.*, c.*
FROM tableA a,
tableB b,
tableC c
where b.field_1 = c.field_1
and a.field_3=c.field_3
and not exists (
select 1
from (
SELECT a.*, b.*, c.*
FROM tableA a,
tableB b,
tableC c
where b.field_1 = c.field_1
and a.field_2 = c.field_2
) x
where a.field_2 = x.field_2
)
If your RDBMS supports CTEs you can abstract the query into a CTE and not repeat yourself. Note you should probably also be using JOIN syntax if your RDBMS supports it, and you may have column name collisions with the * selector that you'll have to resolve.

how to separate comma separated values in oracle 11G

I need to separate the value of a column by commas.
Example: BCDEY; I need to convert to a B, C, D, E, Y.
Follows the "select":
SELECT CDGRUPOCONDICAO FROM TBINTCLIENTE;
You can try also this:
with cad as
(select 'BCDEY' cad from dual)
select regexp_replace (regexp_replace(cad,'(.)','\1, '),', $','') cad_comma from cad;
Something like that maybe?
with testdata as (select 'BCDEY' str from dual)
select listagg(c, ', ') within group(order by lvl)
from (
select substr(str, level, 1) c,
level lvl
from testdata
connect by level <= length(str)
)
Producing:
B, C, D, E, Y
Here, the subquery split the string character by character. Then the outer listagg re-assemble the items by joining them with ', '.
An other solution, using recursive subquery factoring (hence, assuming oracle >= 11g release 2):
with testdata as (select 1 id, 'BCDEY' str from dual union all
select 2, 'ABC' from dual),
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- replace that subquery by your actual query
splited(c,r,id,lvl) as (
select '' c, str r, id, 0 lvl from testdata
union all
select substr(r, 1, 1) c,
substr(r, 2) r,
id,
lvl+1
from splited
where r is not null)
select listagg(c, ', ') within group(order by lvl)
from splited
group by (id)
Producing:
LISTAGG(C,',')WITHINGROUP(ORDERBYLVL)
B, C, D, E, Y
A, B, C
See http://sqlfiddle.com/#!4/d41d8/38971

Return non-null value from two tables in Oracle

I have two tables, T1 and T2 with same set of columns. I need to issue a query which will return me value of columns from either table whichever is not null. If both columns are null return null as the value of that column.
The columns are c1,c2,c3,cond1.
I issued the following query. The problem is that if one subquery fails the whole query fails. Somebody please help me. Probably there is another simple way.
SELECT NVL(T1.c1, T2.c1) c1,NVL(T1.c2, T2.c2) c2,NVL(T1.c3, T2.c3) c3
FROM (SELECT c1,c2,c3
FROM T1
WHERE cond1 = 'T10') T1
,(SELECT c1,c2,c3
FROM T2
WHERE cond1 = 'T200') T2 ;
You need something like this:
SELECT NVL((SELECT T1.c1
FROM T1
WHERE T1.c2 = 'T10'),
(SELECT T2.c1
FROM T2
WHERE T2.c2 = 'T200')) AS c1
FROM dual
Or you may prefer a full outer join:
SELECT NVL(T1.c1, T2.c1) AS c1
FROM T1 FULL OUTER JOIN T2 ON 1=1
WHERE T1.c2 = 'T10'
AND T2.c2 = 'T200'
Your result is logical. If the first table is null no combination of values will exist in the natural join.
EDIT. After some new requirements we can use a hack to get the row. Lets get all three possibilities, T1, T2 or all nulls and select the first one:
SELECT *
FROM ( (SELECT T1.*
FROM T1
WHERE T1.c2 = 'T10')
UNION ALL
(SELECT T2.*
FROM T2
WHERE T2.c2 = 'T200')
UNION ALL
(SELECT T2.*
FROM dual
LEFT JOIN T1 ON 1 = 0 ) )
WHERE ROWNUM = 1

HiveQL UNION ALL

I have table_A:
id var1 var2
1 a b
2 c d
Table_B:
id var1 var2
3 e f
4 g h
All I want is table, combined:
id var1 var2
1 a b
2 c d
3 e f
4 g h
This is my .hql:
CREATE TABLE combined AS
SELECT all.id, all.var1, all.var2
FROM (
SELECT a.id, a.var1, a.var2
FROM table_A a
UNION ALL
SELECT b.id, b.var1, b.var2
FROM table_B b
) all;
I'm coding straight from page 112 of Programming Hive by Edward Capriolo, et al.
The error I get, no matter what ostensibly reasonable variation of the above that I try, is
cannot recognize input near '.' 'id' ',' in select expression.
I have tried using AS between the table name and the alias, asterisks since I want everything from both tables. Same error. I've tried other things and gotten other errors... All I want to do is UNION two tables. (I've tried UNION instead of UNION ALL — same error).
Just replace all with another word. It seems to be a reserved keyword. E.g:
CREATE TABLE combined AS
SELECT unioned.id, unioned.var1, unioned.var2
FROM (
SELECT a.id, a.var1, a.var2
FROM table_A a
UNION ALL
SELECT b.id, b.var1, b.var2
from table_B b
) unioned;
I have similar query working. Just changing table name and column names. Try this. Hope this helps you.
create table new_table as
select
distinct
id, name
FROM
table1
union all
select
distinct
id,name
from
table2
;
Try this, it worked for me.
CREATE TABLE combined AS
SELECT id, var1, var2
FROM (
SELECT id, var1, var2
FROM table_A
UNION ALL
SELECT id, var1, var2
from table_B
) a;

Resources