Can we select count(query) from table - sql

SELECT COUNT(ANOTHER SELECT QUERY) FROM DUAL.
Can we get the results this way or is there any other way?

An example could help:
SQL> create table tabTest as (select 1 x from dual);
Table created.
SQL> select count( select * from tabTest ) from dual;
select count( select * from tabTest ) from dual
*
ERROR at line 1:
ORA-00936: missing expression
SQL> select count(*) from (select * from tabTest);
COUNT(*)
----------
1

You can use a derived table (aka "sub-query")
select count(*)
from (
.... your query here ...
);

Related

oracle sql - define date as variable in a with statement

in oracle - i am trying to figure out how to define variables in a with statement.
when i define a variable as a number it works fine:
with a as(
select 100 as query_rows
from dual
)
,b as (
select * from table1 where rownum=query_rows
)
select * from b --working great
however,if i want to define a date as a variable,i keep getting an error:
with a as(
select DATE '2020-10-01' as query_date
from dual
)
,b as (
select * from table1 where table1.date=query_date
)
select * from b -- ORA-00904 : "query_date" is not a valid identifier
from oracle :ORA-00904
You tried to execute a SQL statement that included an invalid column name or the column name is missing. This commonly occurs when you reference an invalid alias in a SELECT statement.
so,why does the first query work and the second one doesn't?
You need to use the CTE table in query as follows:
with a as(
select DATE '2020-10-01' as query_date
from dual
)
,b as (
select * from table1 cross join a where table1.date=a.query_date
)
Select * from b;
It is possible, but you need to refer to cte you are using(here using subquery):
with a as(
select DATE '2020-10-01' as query_date
from dual
) ,b as (
select *
from table1
where table1.date = (SELECT query_date FROM a) -- IN if more than one row is allowed
)
select * from b
Adding to #Tejash, you don't need to use b:
with a as(
select DATE '2020-10-01' as query_date
from dual
)
select * from table1 cross join a where table1.date=a.query_date

Can I nest "WITH" clause in Oracle SQL?

Following query gives me an error:
"ORA-32034: Unsupported use of WITH clause"
WITH table_B as
(
SELECT * FROM (
WITH table_A AS
(SELECT 'Akshay' as NAME FROM DUAL)
SELECT NAME FROM table_A
) WHERE NAME LIKE '%Aks%' ---<<< Note a filter here
)
SELECT * from table_B;
Is there a way out? Thanks
You should change your query to:
WITH table_a AS
(
SELECT 'Akshay' as name
FROM dual
)
,table_b AS
(
SELECT name
FROM table_a
WHERE name LIKE '%Aks%'
)
SELECT *
FROM table_b;
We can use like following:-
WITH
table_A AS
(SELECT 'Akshay' as NAME FROM DUAL),
table_B AS
(SELECT * FROM table_A where NAME like 'Aks%') --<< Adding filter here now
SELECT * FROM table_B;
Cheers!

Need help identifying dups in the table

What I have:
data_source_1 table
data_source_2 table
data_sources_view view
About tables:
data_source_1:
has no dups:
db=# select count(*) from (select distinct * from data_source_1);
count
--------
543243
(1 row)
db=# select count(*) from (select * from data_source_1);
count
--------
543243
(1 row)
data_source_2:
has no dups:
db=# select count(*) from (select * from data_source_2);
count
-------
5304
(1 row)
db=# select count(*) from (select distinct * from data_source_2);
count
-------
5304
(1 row)
data_sources_view:
has dups:
db=# select count(*) from (select distinct * from data_sources_vie);
count
--------
538714
(1 row)
db=# select count(*) from (select * from data_sources_view);
count
--------
548547
(1 row)
The view is simple as:
CREATE VIEW data_sources_view
AS SELECT *
FROM (
(
SELECT a, b, 'data_source_1' as source
FROM data_source_1
)
UNION ALL
(
SELECT a, b, 'data_source_2' as source
FROM data_source_2
)
);
What I want to know:
How is that possible to have dups in a view where source tables doesn't have dups + 'data_source_x' as source eliminates the possibility of overlapping data.
How to identify dups?
What I've tried:
db# create table t1 as select * from data_sources_view;
SELECT
db=#
db=# create table t2 as select distinct * from data_sources_view;
SELECT
db=# create table t3 as select * from t1 minus select * from t2;
SELECT
db=# select 't1' as table_name, count(*) from t1 UNION ALL
db-# select 't2' as table_name, count(*) from t2 UNION ALL
db-# select 't3' as table_name, count(*) from t3;
table_name | count
------------+--------
t1 | 548547
t3 | 0
t2 | 538714
(3 rows)
Database:
Redshift (PostgreSQL)
The reason is because your data sources have more than two columns. If you do these counts:
select count(*) from (select distinct a, b from data_source_1);
and
select count(*) from (select distinct a, b from data_source_2);
You should find that they are different from the count(*) you get on the same table.
UNION vs UNION ALL
UNION - If the data exist in the TOP Query it's suppressed in the bottom query.
OUTPUT
FOO
UNION ALL - The data repeats as the data exist in both tables (shows both records)
OUTPUT
FOO
FOO

Oracle SQL Query IN

I have following query, that's not working.
select * from table where id in (
1,2, (select id from another_table)
)
How i can rewrite it?
How about
select * from table
where id in (1,2)
or id in (select id from another_table)
Take care and use parentheses when adding additional WHERE-conditions using and!!!
select *
from table
where id in (1,2) OR id in(
select id from another_table
)
select * from table where id in (
select 1 as id from dual
union all
select 2 as id from dual
union all
select id from another_table
)
select * from table where id in (
select 1 from dual
union all
select 2 from dual
union all
select id from another_table);
I'm using union because this is faster than using an OR clause which also can be used.

Oracle create table using with clause

Can I create a table from a query formed using with clause?
Sure:
CREATE TABLE t
AS
WITH some_data AS (
SELECT 1 as some_value
FROM dual
UNION ALL
SELECT 2
FROM dual
)
SELECT *
FROM some_data
The CREATE TABLE table_name AS statement creates a table based on a select statement. The solution for a with clause will be :
CREATE TABLE t
AS
SELECT * FROM (
WITH some_data AS (
SELECT 1 as some_value
FROM dual
UNION ALL
SELECT 2
FROM dual
)
);
For multiple CTE (common table expressions; i.e. multiple WITH clause), I found the same syntax worked. i.e.
CREATE TABLE schema.table_name as
WITH table1 as (SELECT 1),
table2 as (SELECT 2)
select * from table2
will create the table_name in the schema from the select statement