I have two tables looking something like:
TABLE_1
COL_A (int), COL_B (float), COL_C (float)
TABLE_2
COL_A (int), COL_B (float)
My query is using a UNION to put the results of these tables together, but where TABLE_2 doesn't have a COL_C, I'm looking to put something like '0'.
But I just get a 'ORA-00918: column ambiguously defined' error
How can I get around this?
You can try this
SELECT COL_A, COL_B, COL_C FROM Table1
UNION
SELECT COL_A, COL_B, 0 As COL_C FROM Table2
SELECT COL_A,COL_B,COL_C FROM TABLE_1
UNION
SELECT COL_A,COL_B,'0' AS COL_C FROM TABLE_2
You may also be able to get away with
SELECT COL_A,COL_B,COL_C FROM TABLE_1
UNION
SELECT COL_A,COL_B,'0' FROM TABLE_2
The basic idea is that both must have same number of columns of the same corresponding data types.
Related
I have a function, which will get greatest of three dates from the table.
create or replace FUNCTION fn_max_date_val(
pi_user_id IN number)
RETURN DATE
IS
l_modified_dt DATE;
l_mod1_dt DATE;
l_mod2_dt DATE;
ret_user_id DATE;
BEGIN
SELECT MAX(last_modified_dt)
INTO l_modified_dt
FROM table1
WHERE id = pi_user_id;
-- this table contains a million records
SELECT nvl(MAX(last_modified_ts),sysdate-90)
INTO l_mod1_dt
FROM table2
WHERE table2_id=pi_user_id;
-- this table contains clob data, 800 000 records, the table 3 does not have user_id and has to fetched from table 2, as shown below
SELECT nvl(MAX(last_modified_dt),sysdate-90)
INTO l_mod2_dt
FROM table3
WHERE table2_id IN
(SELECT id FROM table2 WHERE table2_id=pi_user_id
);
execute immediate 'select greatest('''||l_modified_dt||''','''||l_mod1_dt||''','''||l_mod2_dt||''') from dual' into ret_user_id;
RETURN ret_user_id;
EXCEPTION
WHEN OTHERS THEN
return SYSDATE;
END;
this function works perfectly fine and executes within a second.
-- random user_id , just to test the functionality
SELECT fn_max_date_val(100) as max_date FROM DUAL
MAX_DATE
--------
27-02-14
For reference purpose i have used the table name as table1,table2 and table3 but my business case is similar to what i stated below.
I need to get the details of the table1 along with the highest modified date among the three tables.
I did something like this.
SELECT a.id,a.name,a.value,fn_max_date_val(id) as max_date
FROM table1 a where status_id ='Active';
The above query execute perfectly fine and got result in millisecods. But the problem came when i tried to use order by.
SELECT a.id,a.name,a.value,a.status_id,last_modified_dt,fn_max_date_val(id) as max_date
FROM table1 where status_id ='Active' a
order by status_id desc,last_modified_dt desc ;
-- It took almost 300 seconds to complete
I tried using index also all the values of the status_id and last_modified, but no luck. Can this be done in a right way?
How about if your query is like this?
select a.*, fn_max_date_val(id) as max_date
from
(SELECT a.id,a.name,a.value,a.status_id,last_modified_dt
FROM table1 where status_id ='Active' a
order by status_id desc,last_modified_dt desc) a;
What if you don't use the function and do something like this:
SELECT a.id,a.name,a.value,a.status_id,last_modified_dt x.max_date
FROM table1 a
(
select max(max_date) as max_date
from (
SELECT MAX(last_modified_dt) as max_date
FROM table1 t1
WHERE t1.id = a.id
union
SELECT nvl(MAX(last_modified_ts),sysdate-90) as max_date
FROM table2 t2
WHERE t2.table2_id=a.id
...
) y
) x
where a.status_id ='Active'
order by status_id desc,last_modified_dt desc;
Syntax might contain errors, but something like that + the third table in the derived table too.
I have a source table like below:
Emp_ID| Name| Manager_ID
001|abc|005
005|cde|010
010|xyz|050
050 | bcg| 100
100|sta|NULL
My requirement is to populate the target table like below:
Emp_ID| Name| Manager_1| Manager_2| Manager_3| Manager_4
005|cde|xyz|bcg|sta|NULL
050|bcg|sta| NULL|NULL|NULL
100|sta|NULL|NULL|NULL
001|abc|cde|xyz|bcg|sta
I am able to use recursive select through Connect by clause and populate the value for Manager_1 but not able to get through the logic to populate Manager_2, Manager_3 , Manager_4 values as different column values in a single row depending on how many level of hierarchy is present for a certain employee.
Please help.
I think the following query will help you. But to split the string to individual manager id, you need to know the max no of level of managers.
WITH data_set AS
(SELECT '001' emp_id, 'aaa' emp_name, '005' mgr_id
FROM DUAL
UNION
SELECT '005' emp_id, 'bbb' emp_name, '010' mgr_id
FROM DUAL
UNION
SELECT '010' emp_id, 'ccc' emp_name, '050' mgr_id
FROM DUAL
UNION
SELECT '020' emp_id, 'ddd' emp_name, '050' mgr_id
FROM DUAL
UNION
SELECT '050' emp_id, 'eee' emp_name, '100' mgr_id
FROM DUAL
UNION
SELECT '100' emp_id, 'fff' emp_name, '200' mgr_id
FROM DUAL
UNION
SELECT '200' emp_id, 'ggg' emp_name, NULL mgr_id
FROM DUAL)
SELECT emp_id, emp_name, mgr_id,
LTRIM (SYS_CONNECT_BY_PATH (emp_id, '-'), '-') chain
FROM data_set
START WITH mgr_id IS NULL
CONNECT BY mgr_id = PRIOR emp_id
ORDER SIBLINGS BY emp_id;
If your hierarchy only extends to 4 levels deep, the following query may be used:
select t1.Emp_ID,
t1.Name,
t2.Name as Manager_1,
t3.Name as Manager_2,
t4.Name as Manager_3,
t5.Name as Manager_4
from tmp t1
left join tmp t2 on t2.Emp_ID = t1.Manager_ID
left join tmp t3 on t3.Emp_ID = t2.Manager_ID
left join tmp t4 on t4.Emp_ID = t3.Manager_ID
left join tmp t5 on t5.Emp_ID = t4.Manager_ID;
Pivot option:
SELECT * FROM
(
SELECT emp_id, name, manager_id
FROM employees
)
PIVOT
(
COUNT(manager_id)
FOR manager_id IN ('005', '100', '050')
)
ORDER BY emp_id;
This question is wrong. I had some very big misunderstanding about how union works. I am reading about it now.
edit 04.12.2016
If you are still intersted, you can go here
Selecting the right column
I have something like this
with table3 as
(
select t1.c1, t1.c2...
from table1 t1
union all
select t2.c1, t2.c2...
from table2 t2
)select * from table3
I need to insert all rows from above in another table
insert into table4 t4
(
t4.c1, t4.c2...
)
select t3.c1, t3.c2...
from table3 t3
My question is, will this insert work. I have clumns in table 1 and 2 named the same, will I need to reference them somehow differently?
Do I need to write it like this?
insert into table4 t4
(
t4.c1, t4.c2...
)
select t3.t1.c1, t3.t1.c2, t3.t2.c1...
from table3 t3
with is part of select statement. You can insert result of select and you can use with in this select. Maybe syntax is not the most intuitive but this should work:
insert into table4
with table3 as
(
select t1.c1, t1.c2...
from table1 t1
union all
select t2.c1, t2.c2...
from table2 t2
) select * from table3;
And no you don't need (even can't) use double aliases.
No alias needed
if the column match you could simply use insert select
insert into table4
( select t1.c1, t1.c2...
from table1 t1
union all
select t2.c1, t2.c2...
from table2 t2)
otherwise you should declare the column name
insert insert into table4(c1, c2... )
( select t1.c1, t1.c2...
from table1 t1
union all
select t2.c1, t2.c2...
from table2 t2)
Assuming that you needto use that UNION ALL, instead of single insert-as-select statements to insert into another table, you can try to use different aliases for columns from different tables:
with table1 as
(
select t2.name as t2_name,
t2.address as t2_address,
t2.age as t2_age,
null as t3_name,
null as t3_address,
null as t3_age,
from table2 t2
union all
select null,
null,
null,
t3.name,
t3.address,
t3.age
from table3 t3
)
Apologies in advance, I am occasional Oracle user. I have put together a lookup table used by various functions/procedures and need to keep refresh this once a day with rows that either need removing or inserting. I have put together the following simply queries that return the columns against which I can determine the required action. Once I have returned my deletion data, I then need to delete from table A all records where the site_id and zone_ids match. I cant figure out the best way to achieve this, I have thought about running the select statements as cursors, but am not sure how I then delete the rows from table A using the site_id and zone_id from each record returned.
Query That returns records to be deleted from Table_A
SELECT site_id,zone_id,upper(ebts_switch_name)
FROM Table_A
minus
(SELECT site_id,zone_id, upper(ebts_switch_name)
FROM Table_B#remote_db
UNION
SELECT site_id,zone_id,upper(ebts_switch_name)
FROM Table_C);
Query That returns records to be Inserted into Table_A
SELECT cluster_id, site_id,zone_id, upper(trigram),upper(ebts_switch_name)
FROM Table_B#remote_db
WHERE site_id is NOT NULL
minus
SELECT cluster_name,site_id,zone_id,upper(trigram),upper(ebts_switch_name)
FROM Table_A
You can use your statements directly in the manner shown below:
DELETE FROM TABLE_A
WHERE (SITE_ID, ZONE_ID, UPPER(EBTS_SWITCH_NAME)) IN
(SELECT site_id, zone_id, upper(ebts_switch_name)
FROM Table_A
minus
(SELECT site_id, zone_id, upper(ebts_switch_name)
FROM Table_B#remote_db
UNION
SELECT site_id, zone_id, upper(ebts_switch_name)
FROM Table_C));
INSERT INTO TABLE_A (CLUSTER_NAME, SITE_ID, ZONE_ID, TRIGRAM, EBTS_SWITCH_NAME)
SELECT cluster_id, site_id, zone_id, upper(trigram), upper(ebts_switch_name)
FROM Table_B#remote_db
WHERE site_id is NOT NULL
minus
SELECT cluster_name, site_id, zone_id, upper(trigram), upper(ebts_switch_name)
FROM Table_A;
Best of luck.
I can't understand what do you mean by first query, cause it's almost same as
SELECT *
FROM table_a
MINUS
SELECT *
FROM table_a
means empty record set.
But generally, use DELETE syntax
DELETE
FROM table_a
WHERE (col1, col2) IN (SELECT col1, col2
FROM table_b);
And INSERT syntax
INSERT INTO table_a (col1, col2)
SELECT col1, col2
FROM table_b;
How can I get the count of 2 columns such that there are distinct combinations of two columns?
Select count(distinct cola, colb)
SELECT COUNT(*)
FROM (
SELECT DISTINCT a, b
FROM mytable
)
SELECT COUNT(1)
FROM (
SELECT DISTINCT COLA, COLB
FROM YOUR_TABLE
)
Another way of doing it
SELECT COUNT(DISTINCT COLA || COLB)
FROM THE_TABLE
http://www.sqlfiddle.com/#!4/c287e/2
SELECT (select count(cola) from ...), (select count(colb) from ...) from ...
You may want to look at this:
http://www.java2s.com/Code/Oracle/Aggregate-Functions/COUNTcolumnandCOUNTcountthenumberofrowspassedintothefunction.htm
You can put Distinct in the subqueries, if you desire.
In Oracle DB, you can concat the columns and then count on that concatenated String like below:
SELECT count(DISTINCT concat(ColumnA, ColumnB)) FROM TableX;
In MySql, you can just add the columns as parameters in count method.
SELECT count(DISTINCT ColumnA, ColumnB) FROM TableX;
SELECT COUNT(*)
FROM (
SELECT DISTINCT a, b
FROM mytable
) As Temp