How can I select count(*) from two different databases(call them ZEOTA and SP) having as result:
Zeota SP
88 3
I have tried this:
SELECT COUNT(CONSTRAINT_TYPE) NumberOfPrimaryKeys_Zeota
FROM ALL_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'P'
AND
OWNER = 'ZEOTA';
SELECT COUNT(*) AS NumberOfAttributes_SP
FROM ALL_COL_COMMENTS
WHERE OWNER = 'SP';
But the output shows two separate query results:
Query result 1:
Zeota
88
Query Result 2:
SP
3
However I am trying to do it this way, but having issues:
SELECT
COUNT(DISTINCT TABLE_NAME) AS ZNumOfTables,
COUNT(DISTINCT TABLE_NAME) AS SNumOfTables,
COUNT(DISTINCT TABLE_NAME) AS PNumOfTables
FROM ALL_TAB_COLUMNS
WHERE OWNER = 'SP';
WHERE OWNER = 'ZEOTA';
One option is to use your current queries as CTEs and then just cross join them:
with
t1 as
(select count(constraint_type) num_pk_zeota
from all_constraints
where owner = 'ZEOTA'
and constraint_type = 'PK'
),
t2 as
(select count(*) num_attr_sp
from all_col_comments
where owner = 'SP'
)
select a.num_pk_zeota,
b.num_attr_sp
from t1 a cross join t2 b;
P.S. What you call "databases" are users (schemas) in Oracle.
Related
First I used the query:
select name
from tab1
where id in (select id
from (select id,count(id) as a
from tab2
group by id
order by a desc limit 1) ;
and I came to know that select inside select is not possible in hive.
So I modified it using variable.
set var1= select count(id) as a from tab2 group by id order by a desc limit 1;
select name from tab1 group by name having count(id)='${hiveconf:var1}';
But in the place of '${hiveconf:var1}', the query got substitued and again getting the same error.
Is there any way to do this?
select t1.name
from tab1 t1
join (select id
,count(*) as cnt
from tab2
group by id
order by cnt desc
limit 1
) t2
on t2.id = t1.id
I am selecting count of few tables and i have a table named table_counts which has two columns named table_name and table_count. I want to join the result of the query with the table_name column of table_counts table. Please see the example below.
select 'Table 1' as table_name, count(*) as table_count_from table_1
union
select 'Table 2' as table_name, count(*) as table_count_from table_2
union
select 'Table 3' as table_name, count(*) as table_count_from table_3
------------------
++table_counts++++
------------------
table_name table count
Table 1 10
Table 2 20
Table 3 30
I have to join the two things using the table_name. Could someone help me if iam missing few things?
you are searching something like this?
SELECT *
FROM table_counts cnt
LEFT OUTER JOIN (select 'Table 1' as table_name, count(*) as table_count_from table_1
union
select 'Table 2' as table_name, count(*) as table_count_from table_2
union
select 'Table 3' as table_name, count(*) as table_count_from table_3
) subcnt
ON cnt.table_name = subcnt.table_name
I am building an Oracle query which has a case statement involved.
SELECT
CASE WHEN
((SELECT agent_or_group_id from trans_slot where slot_id =
(SELECT slot_id from trans_slot where slot_alias = 'PP' and measure_expiration > sysdate)) > 0)
/*The below subquery returns 1 row*/
THEN (SELECT agent_or_group_id from trans_slot where slot_id =
(SELECT slot_id from trans_slot where slot_alias = 'PP' and measure_expiration > sysdate))
ELSE
/* The below subquery returns 2 rows*/
(SELECT child_agent_id FROM agent_object_group_member WHERE parent_agent_id IN
(SELECT agent_or_group_id FROM trans_slot WHERE slot_id IN
(SELECT slot_id FROM trans_slot WHERE slot_alias = 'PP' AND measure_expiration > sysdate)
)
)
END
"Agent_ID" from DUAL;
When the run the subqueries independent they run fine. But running the whole query returns
ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
*Cause:
*Action:
If I understand what you're after, you can't do it like that. You'll need to use a left outer join and then chose the column value you want to display.
Here's a simplified example based on the SQL you provided:
WITH t1 AS (SELECT 1 ID FROM dual UNION ALL
SELECT 0 ID FROM dual UNION ALL
SELECT 2 ID FROM dual),
t2 AS (SELECT 10 child_id, 0 parent_id FROM dual UNION ALL
SELECT 20 child_id, 0 parent_id FROM dual UNION ALL
SELECT 30 child_id, 1 parent_id FROM dual UNION ALL
SELECT 40 child_id, 2 parent_id FROM dual)
---- end of mimicking two tables with the sample data in them. See the query below:
SELECT COALESCE(t2.child_id, t1.id) ID
FROM t1
LEFT OUTER JOIN t2 ON (t1.id = t2.parent_id AND t1.id = 0);
ID
----------
10
20
2
1
Here, I have used the t1 and t2 subqueries to mimic the output you'd get from your main subqueries in your original query.
Then we outer join t2 to t1 only where the t1.id = 0. By doing this, you can then simply choose the t2.child_id value if it exists, otherwise use the t1.id value.
(I realise that in your example, the t1 equivalent subquery would only generate 1 row, based on what you said, but I've included 3 rows so that you can see what the results would be based on the different ids.)
ETA:
In your case, the t1 subquery in my example above would be:
SELECT agent_or_group_id
from trans_slot
where slot_id = (SELECT slot_id
from trans_slot
where slot_alias = 'PP'
and measure_expiration > sysdate)
and the t2 subquery would be:
SELECT child_agent_id
FROM agent_object_group_member
WHERE parent_agent_id IN (SELECT agent_or_group_id
FROM trans_slot
WHERE slot_id IN (SELECT slot_id
FROM trans_slot
WHERE slot_alias = 'PP'
AND measure_expiration > sysdate))
Actually I have two table and each having column name, I just want the result which are not there in Table2
Table1
----
Name
---
|A|
|B|
|C|
|D|
Table2
------
|Name|
-----
|A|
|B|
Answer
|C|
|D|
I am able to do it by using minus
select name from table1
minus
select name from table2
select name from table1 where name
not in (
select name from table2)
But my Manager ask me to do it with other alternate solution without using minus,except,not in.
Is there a way to do that, It will be great if someone can help me on it.
I need to do it with oracle pl/sql
The one option left with you is using NOT EXISTS
SELECT t1.name
FROM table1 t1
WHERE NOT EXISTS (SELECT 'X'
FROM table2 t2
WHERE t2.name = t1.name);
Update: Using Join
with table_ as
(
select t1.name t1_name, t2.name t2_name
from table1 t1
left join table2 t2
on t1.name = t2.name)
select t1_name
from table_
where t2_name is null;
Or just
select t1.name
from table1 t1
left join table2 t2
on t1.name = t2.name
where t2.name is null;
Another alternative is to use an outer join and then filter rows that don't have a value in the 2nd table:
with t1 as (select 'A' name from dual union all
select 'B' name from dual union all
select 'C' name from dual union all
select 'D' name from dual),
t2 as (select 'A' name from dual union all
select 'B' name from dual)
select t1.name
from t1
left outer join t2 on (t1.name = t2.name)
where t2.name is null;
NAME
----
D
C
Need to get the list of tables name from a schema which doesn't have a primary key. I tried the following query to get this, but it will list all other keys except primary keys.
SELECT a.constraint_name,a.table_name
FROM ALL_CONS_COLUMNS A
JOIN ALL_CONSTRAINTS C
ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME
WHERE
C.CONSTRAINT_TYPE not in('P')
and a.owner ='my_schema';
If you only want this for the current user, it's better to user the user_xxx views instead of the all_xxx views.
The following should do what you want:
select ut.table_name
from user_tables ut
where not exists (select 1
from user_constraints ac
where ac.table_name = ut.table_name
and ac.constraint_type = 'P'
);
If you do need this for a different user, then you can use the following.
select at.*
from all_tables at
where not exists (select 1
from all_constraints ac
where ac.owner = at.owner
and ac.table_name = at.table_name
and ac.constraint_type = 'P'
)
and at.owner = 'MY_SCHEMA';
Don't forget that Oracle is case sensitive and that user names are stored in uppercase, so a.owner ='my_schema' will most probably not return anything.
Another way:
select owner,table_name
from all_tables
where owner = 'my_schema'
MINUS
select owner,table_name
from all_constraints
where owner = 'my_schema'
and constraint_type = 'P'
Try like this,
SELECT table_name
FROM all_tables A
WHERE table_name NOT IN
(
SELECT table_name
FROM all_constraints WHERE constraint_type ='P'
)
AND a.owner = 'my_schema';
Select tables.owner || ‘.’ ||
tables.table_name
as table_owner
From all_tables tables,
(Select
owner,
TABLE_NAME,
constraint_type,
CONSTRAINT_NAME
FROM ALL_CONSTRAINTS
WHERE CONSTRAINT_TYPE = ‘P’ /* list of all tables with PK */
) constr
Where tables.owner = constr.owner (+)
And tables.table_name = constr.table_name (+)
and tables.owner <> ‘SYS’
and tables.owner <> ‘SYSTEM’
And constr.owner IS NULL
And constr.table_name IS NULL
ORDER BY 1