Join other table in SQL with dual table - sql

SQL Console
select trunc((:FromDate)+1)-rn as date_Val
from ( select rownum rn
from dual
connect by level <= ((:FromDate)-(:todate))+1)
order by trunc(:FromDate)-rn
I want to join this column with other tables. When I write in Sub Query return more than one row error show

Turn it into a cte and write the rest of your query under it:
with dateseq as
(
select trunc((:FromDate)+1)-rownum as date_val
from dual
connect by level <= ((:FromDate)-(:todate))+1)
)
select * from dateseq inner join ...
ps: simplified your query a bit- you don't need the subquery

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

Extract specific rows from a table in Oracle

I want to write a query which will fetch me the first and last 3 records from the table
Below is the table details
select * from employee_src
Now to get the above result i am using the below query
select fname,lname,ssn,salary,dno from employee_src where rownum <=3
union all
select fname,lname,ssn,salary,dno from (select fname,lname,ssn,salary,dno from employee_src order by rownum desc) where rownum <=3
On running this query I am getting the below result
Even though I am getting the first 3 and last 3 rows but the last 3 rows are not in the order as in the original table. How to get this fixed.
Try this.
select * from (select * from employee_src order by rownum Asc) where rownum <= 3
union all
select *, from ( select * from employee_src from dual order by rownum desc
) as employee_src_last3
where rownum <= 3
Try now...

Accesing parent identifier in an ordered nested subquery

I need a query to get the value of an item together with the value of the previous item if exists.
I am using the following query (a simplification of the actual):
select v1.value item_value,
nvl(
(
select * from (
select v2.value
from ITEMS v2
where v2.insert_date<v1.insert_date
order by v2.insert_date desc
) where rownum=1
), 0
) as previous_value
from ITEMS v1
where v1.item_id=1234
This query won't work (ORA-00904) because I am using v1.insert_date in an inner select with two levels of nesting.
How can I achieve this with Oracle 11?
I think you can achieve this with analytic function LAG. More info about analytic functions LAG LEAD
I created a sample query:
with items as (
select 1 as value, sysdate as insert_date from dual
union all
select 2 as value, sysdate-1 as insert_date from dual
union all
select 3 as value, sysdate+1 as insert_date from dual
)
select v1.value item_value,
lag(v1.value,1,0) over (order by v1.insert_date desc) as previous_value,insert_date
from ITEMS v1
order by insert_date desc

Union of two queries with the first query resultset getting the first ten rownums in Oracle

I want Union of two queries with the first query resultset getting the first ten rownums in Oracle.
Example:
Like if first query has 10 rows and max rownum is 10.I want second query rownum to be started from 11 in the result of union.
SELECT *
FROM (
SELECT *
FROM table1
ORDER BY
col1
)
WHERE rownum <= 10
UNION ALL
SELECT *
FROM (
SELECT *, rownum AS rn
FROM (
SELECT *
FROM table2
ORDER BY
col2
)
)
WHERE rn > 10

Join two sql queries side by side with no column common

I need to join the results of two queries horizontally.
Consider a query below which will return two rows:
Select *
from Salary
where sal > 10000
The result of the query above should be joined side by side with the result of the query below which will again return two rows.Or I need to just concatenate the two result sets:
Select 'xyz' from dual
union
Select 'abc' from dual
Please suggest how this can be done as I tried to do this with the query below but it returns a cartesian product:
Select *
from (Select *
from salary
where sal > 10000) TEMP1,
(Select 'xyz' from dual
union
Select 'abc' from dual) TEMP2
You can do that by joining on rownum like this:
SELECT *
FROM
(SELECT view_name, rownum AS r FROM all_views WHERE rownum <=10)
FULL OUTER JOIN (SELECT table_name, rownum AS r FROM all_tables WHERE rownum <=10) USING (r)
In your case this would look like (untested):
Select * from
(Select salary.*, rownum AS r from salary where sal>10000) TEMP1
FULL OUTER JOIN
(SELECT temp2.*, rownum r FROM
(Select 'xyz' from dual
union
Select 'abc' from dual) TEMP2)
USING (r)
you can introduce an artificial join column:
SELECT *
FROM (SELECT s.*, ROWNUM ID FROM Salary s WHERE sal > 10000) q1
JOIN (SELECT 'xyz' col1, 1 ID
FROM dual
UNION
SELECT 'abc' col1, 2 ID FROM dual) q2 ON q1.id = q2.id
Thank you very much for your help.
But what I need is a bit complicated. I have updated the previous query to be somewhat like below instead of simple query that I posted before (SELECT * FROM Salary WHERE sal > 10000):
SELECT name, sal, address
FROM (SELECT e1.name, s1.sal, s1.grade, s2.address FROM Emp e1, salary s1,
(Select empcode, address FROM Address WHERE empcode LIKE 'NY%') s2
WHERE e1.hiredate =201001
AND s1.sal>10000)
I know the above query does not make much of a relevance. However, this is similar to what I need actually. I am not posting the original one as that is very complicated but if this can be done for this query, then I will be able to replicate the same on the original one as well.
Thank you,
Sharon