using subquery factoring result in where clause - oracle

Why can't I use a subquery factoring clause result in the where clause of as depicted in the following sql:
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = rpt.id
and
tg.gene not in('TMB','MS')
The subquery is named rptand used in the select statement's where clause. When executed encountering the following error: ORA-00904: "RPT"."ID": invalid identifier
UPDATE:
In fact nested query for the same thing is also giving me the same issue. The nested subquery is only returning a single column value from a single row:
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = (select id from reports where caseid = :case_id and
rownum=1 order by created desc)
and
tg.gene not in('TMB','MS')

You missed to add the table rpt in your query, thus that error.
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
join
rpt on rt.reportid = rpt.id
where
tg.gene not in('TMB','MS')

Related

Rewriting query with table join containing GROUP BY clause

Is it possible to rewrite the following query
SELECT CT.GROUP, CT.EMP_ID, HT.EFF_DT
FROM CURR_TABLE CT
JOIN (SELECT GROUP, EMP_ID, MAX(EFF_DT) AS EFF_DT
FROM HIST_TABLE
WHERE STAT = 'A'
GROUP BY GROUP, EMP_ID) HT ON CT.GROUP = HT.GROUP AND
CT.EMPID = HT.EMP_ID
WHERE CT.GROUP = :1
AND CT.EMP_ID = :2
in a way that is similar to CROSS JOIN style?
SELECT table1.column1, table2.column2...
FROM table1, table2 [, table3 ]
The reason is that I want to create such query in Peoplesoft, and the above can only be achieved by creating a separate view for the selection with the group by clause. I want to do this just in one query without creating additional views.
You may try writing your query as a single level join with an aggregation:
SELECT
CT.GROUP,
CT.EMP_ID,
MAX(HT.EFF_DT) AS EFF_DT
FROM CURR_TABLE CT
LEFT JOIN HIST_TABLE HT
ON CT.GROUP = HT.GROUP AND
CT.EMPID = HT.EMP_ID AND
HT.STAT = 'A'
WHERE
CT.GROUP = :1 AND
CT.EMP_ID = :2
GROUP BY
CT.GROUP,
CT.EMP_ID;
Note that GROUP is a reserved SQL keyword, and you might have to escape it with double quotes to make this query (or the one in your question) work on Oracle.

can i set up an SSRS report where users input parameters to a table

I have an oracle query that uses a created table as part of the code. Every time I need to run a report I delete current data and import the new data I receive. This is one column of id's. I need to create a report on SSRS in which the user can input this data into said table as a parameter. I have designed a simple report that they can enter some of the id's into a parameter, but there may be times when they need to enter in a few thousand id's, and the report already runs long. Here is what the SSRS code currently says:
select distinct n.id, n.notes
from notes n
join (
select max(seq_num) as seqnum, id from notes group by id) maxresults
on n.id = maxresults.ID
where n.seq_num = maxresults.seqnum
and n.id in (#MyParam)
Is there a way to have MyParam insert data into a table I would join called My_ID, joining as Join My_Id id on n.id = id.id
I do not have permissions to create functions or procedures in the database.
Thank you
You may try the trick with MATERIALIZE hint which normally forces Oracle to create a temporary table :
WITH cte1 AS
( SELECT /*+ MATERIALIZE */ 1 as id FROM DUAL
UNION ALL
SELECT 2 DUAL
)
SELECT a.*
FROM table1 a
INNER JOIN cte1 b ON b.id = a.id

ORACLE - Update Multiple Columns with Values from Nested Join

I'm practically new in using oracle and I bumped into a blocker. Below is the query that I created based on what I have researched online to update multiple columns of a table with values from a nested join statement.
UPDATE
(
SELECT
A.COLUMN1 OLD_COLUMN1,
BC.COLUMN1 NEW_COLUMN1,
A.BALANCE OLD_COLUMN2,
BC.COLUMN2_MIN NEW_COLUMN2,
A.COLUMN3 OLD_COLUMN3,
BC.COLUMN3 NEW_COLUMN3
FROM TABLE_A A
INNER JOIN
(
SELECT B.TWWID,
B.ITEMDATE,
B.COLUMN2_MIN,
C.COLUMN3,
C.COUNTRYID,
C.COLUMN1
FROM TABLE_B B
LEFT OUTER JOIN TABLE_C C
ON TO_CHAR(B.ID) = TO_CHAR(C.ID)
) BC
ON A.ID = BC.ID
AND A.DATE = BC.DATE
)ABCUPDATE
SET ABCUPDATE.OLD_COLUMN1 = ABCUPDATE.NEW_COLUMN1,
ABCUPDATE.OLD_COLUMN2 = ABCUPDATE.NEW_COLUMN2,
ABCUPDATE.OLD_COLUMN3 = ABCUPDATE.NEW_COLUMN3;
Selecting the sub-query returns the expected results but when I run the update script as a whole an error is returned.
ORA-01779: cannot modify a column which maps to a non key-preserved
table
Can anyone please explain why I encounter this error and what adjustments can I do to the script to make it work?
Thanks in advance!

Nested subquery not supported in hive

We have tried the below query in hive. but getting the error. please help me to resolve this in any other way.
select count(1) as OpenItems from issues i , issue_statuses s
where s.id = i.status_id
and s.name NOT IN ('Closed','Passed','Rejected','On
Hold','Baselined','Completed')
and i.project_id IN
(select id from projects3 from
CASE WHEN ${projectname} = 'All' then id in
(select p.id from members m, projects3 p ,users_1 u
where m.project_id = p.id and u.id = m.user_id and u.status = '1'
and u.id IN
(select u1.id from users_1 u1, Supervisor_hierarchy s1 where u1.mail = s1.email and s1.name = ${Superisorname})
group by p.id)
WHEN (${projectname} <>'All' and ${SubProject projectname} ='All') then id
IN (select id from (select id from project_closure where parent_id in (select id from projects where name = ${projectname}) group by id)a)
WHEN (${SubProject projectname}<>'All' and ${projectname}<> 'All') then id
IN (select id from(select id from project_closure where id in (select id from projects where name = ${SubProject projectname}) group by id)a)
END
order by id)
error: 6:5 Unsupported SubQuery Expression 'id': SubQuery expression refers to both Parent and SubQuery expressions and is not a valid join condition.
I know it is late but posting for anyone who face this issue.
This issue occurs when we encounter one or more of the below limitations of Hive Subqueries.
In this scenario, the reference to the parent query is used in Group By clause which comes under the 4th limitation.
Hive Subquery Limitations
These subqueries are only supported on the right-hand side of an expression.
IN/NOT IN subqueries may only select a single column.
EXISTS/NOT EXISTS must have one or more correlated predicates.
References to the parent query are only supported in the WHERE clause of the subquery.
Source: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SubQueries

oracle with clause error in adodb

I am not able to use the with clause in oracle adodb connection. I have tried both wrapping the qry is a select * from (myquery) and adding a , before my with clause. All create errors.
Here is the query
`WITH TEMPa AS (select _nbr, sum(ending) as Cost from t1 group by _nbr),
TEMPB AS (select _nbr, cost,entity
from t2
where end_tms is null and sale_date is null and entity = 110)
SELECT
TEMPA._nbr
,TEMPA.cost
,TEMPb._nbr
,TEMPB._cost
,tempb.entity
,(tempb._cost - tempa.cost) as Difference
FROM TEMPA, TEMPB
WHERE TEMPA._nbr = TEMPB._nbr(+)
and tempa.cost <> tempb.cost`
Any help would be awesome!

Resources