10g Dev Preview -- sub query problem
I'm not sure if this is the place to report bugs or problems with the 10g developer preview. But here goes.
I have some code that used to work. The idea is to find "task" objects that are not assigned to anyone, ie, have no "task assignment" records pointing to them. This code used to work. It creates an expression that when queried returns all Task objects that are not in the subQuery which fetches task assignments.
ExpressionBuilder eb = new ExpressionBuilder();
ReportQuery queryAssigned = new ReportQuery(TaskAssignment.class, new ExpressionBuilder());
ReportQuery queryAssigned = new ReportQuery(TaskAssignment.class, new ExpressionBuilder());
queryAssigned.addAttribute("ID", eb.get("task").get("ID"))
return eb.get("ID").notIn(eb.subQuery(queryAssigned));
When I use TOPLink 10g, however, I get the following error:
[junit] Query: ReadAllQuery(com.marketsoft.workflow.Task)
[junit] Local Exception Stack:
[junit] Exception [TOPLINK-6015] (Oracle TopLink - 10g Developer Preview 3 (10.1.3.0 ) (Build 041116)): oracle.toplink.ex
ceptions.QueryException
[junit] Exception Description: Invalid query key [task] in expression.
[junit] Query: ReadAllQuery(com.marketsoft.workflow.Task)
[junit] at oracle.toplink.exceptions.QueryException.invalidQueryKeyInExpression(QueryException.java:491)
[junit] at oracle.toplink.internal.expressions.QueryKeyExpression.validateNode(QueryKeyExpression.java:555)
[junit] at oracle.toplink.expressions.Expression.normalize(Expression.java:2587)
[junit] at oracle.toplink.internal.expressions.DataExpression.normalize(DataExpression.java:349)
[junit] at oracle.toplink.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:369)
[junit] at oracle.toplink.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:356)
[junit] at oracle.toplink.internal.expressions.DataExpression.normalize(DataExpression.java:343)
[junit] at oracle.toplink.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:369)
[junit] at oracle.toplink.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:356)
[junit] at oracle.toplink.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:951)
[junit] at oracle.toplink.internal.expressions.SubSelectExpression.normalizeSubSelect(SubSelectExpression.java:123)
[junit] at oracle.toplink.internal.expressions.ExpressionNormalizer.normalizeSubSelects(ExpressionNormalizer.java:82)
[junit] at oracle.toplink.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:982)
[junit] at oracle.toplink.internal.queryframework.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQuery
Mechanism.java:223)
[junit] at oracle.toplink.internal.queryframework.ExpressionQueryMechanism.prepareCursorSelectAllRows(ExpressionQuery
Mechanism.java:587)
[junit] at oracle.toplink.queryframework.CursorPolicy.prepare(CursorPolicy.java:137)
[junit] at oracle.toplink.queryframework.CursoredStreamPolicy.prepare(CursoredStreamPolicy.java:93)
[junit] at oracle.toplink.queryframework.ReadAllQuery.prepare(ReadAllQuery.java:571)
[junit] at oracle.toplink.queryframework.DatabaseQuery.checkPrepare(DatabaseQuery.java:367)
[junit] at oracle.toplink.queryframework.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:506)
[junit] at oracle.toplink.queryframework.DatabaseQuery.execute(DatabaseQuery.java:537)
[junit] at oracle.toplink.queryframework.ReadAllQuery.execute(ReadAllQuery.java:408)
[junit] at oracle.toplink.publicinterface.Session.internalExecuteQuery(Session.java:1977)
[junit] at oracle.toplink.publicinterface.Session.executeQuery(Session.java:973)
This maybe isn't that surprising. I see that the unbound ExpressionBuilders might be causing problems. However, which I switch to something a little bit more like what I see in the examples:
ExpressionBuilder eb = new ExpressionBuilder();
ExpressionBuilder assignBuilder = new ExpressionBuilder();
ReportQuery queryAssigned = new ReportQuery(TaskAssignment.class, assignBuilder);
queryAssigned.addAttribute("ID", assignBuilder.get("task").get("ID"));
queryAssigned.useDistinct();
return eb.get("ID").notIn(eb.subQuery(queryAssigned));
In this case, it seems to generate messed up SQL where the table name goes missing:
Exception [TOPLINK-4002] (Oracle TopLink - 10g Developer Preview 3 (10.1.3.0 ) (Build 041116)): oracle.toplink.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-00903: invalid table name
Error Code: 903
Call:SELECT COUNT(*) FROM TASKS t0 WHERE ((t0.ID NOT IN (SELECT DISTINCT t1.ID FROM )) AND (t0.ITEMTYPE = ?))
bind => [Task]
Query:ReportQuery(com.marketsoft.workflow.Task)
Local Exception Stack:
Charles,
I couldn't reproduce your problem. I used an employee example in which Employee has 1:m relationship with PhoneNumber. My code and sql are as follows:
ReadAllQuery query = new ReadAllQuery(Employee.class);
ExpressionBuilder emp = new ExpressionBuilder(Employee.class);
ExpressionBuilder phone = new ExpressionBuilder();
ReportQuery subquery = new ReportQuery(PhoneNumber.class, phone);
subquery.addAttribute("id", phone.get("owner").get("id"));
subquery.useDistinct();
Expression expression = emp.get("id").notIn(subquery);
query.setSelectionCriteria(expression);
Vector employees = (Vector)getSession().executeQuery(query);
SELECT t0.VERSION, t1.EMP_ID, t0.L_NAME, t0.F_NAME, t1.SALARY, t0.EMP_ID, t0.GENDER, t0.END_DATE, t0.START_DATE, t0.MANAGER_ID, t0.START_TIME, t0.END_TIME, t0.ADDR_ID FROM EMPLOYEE t0, SALARY t1 WHERE ((t0.EMP_ID NOT IN (SELECT DISTINCT t2.EMP_ID FROM PHONE t4, SALARY t3, EMPLOYEE t2 WHERE ((t2.EMP_ID = t4.EMP_ID) AND (t3.EMP_ID = t2.EMP_ID)))) AND (t1.EMP_ID = t0.EMP_ID))
Shanno
Similar Messages
-
Sub-query problem on Oracle 10g
The following query works on Oracle 10.2.0.1.0 on windows,but doesn't work on Oracle 10.2.0.2.0 on Linux.
Error report: SQL Error: ORA-00904: "T"."AUDIT_USECS": invalid identifier 00904. 00000 - "%s: invalid identifier"
It works after i remove the sub-query. I found that if use fields of T in sub-query,then error occurs. Is it saying that sub-query can't access the fields in main query?
What's the problem?
Is there any grammar erros?If so,what's the right likes?
Thanks!
CREATE TABLE AUDITHISTORY(
CASENUM numeric(20, 0) NOT NULL,
AUDIT_DATE date NOT NULL,
USER_NAME varchar(255) NULL,
AUDIT_USECS numeric(6, 0) NOT NULL,
TYPE_ID INT NOT NULL )
Query:
SELECT T.CASENUM,
T.USER_NAME,
T.AUDIT_DATE AS STARTED,
(SELECT *
FROM (SELECT S.AUDIT_DATE
FROM AUDITHISTORY S
WHERE S.CASENUM=T.CASENUM AND TYPE_ID=2
AND S.USER_NAME=T.USER_NAME
AND (S.AUDIT_DATE > T.AUDIT_DATE OR (S.AUDIT_DATE = T.AUDIT_DATE AND S.AUDIT_USECS > T.AUDIT_USECS))
ORDER BY S.AUDIT_DATE ASC,S.AUDIT_USECS ASC
) WHERE rownum <= 1) AS ENDED
FROM AUDITHISTORY T WHERE TYPE_ID=1
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - Prod PL/SQL Release 10.2.0.2.0 - Production
CORE 10.2.0.2.0 Production
TNS for Linux: Version 10.2.0.2.0 - Production
NLSRTL Version 10.2.0.2.0 - ProductionTry this way:
SQL> select * from AUDITHISTORY;
CASENUM AUDIT_DAT USER_NAME AUDIT_USECS TYPE_ID
10 12-MAR-10 USER 100 1
10 14-MAR-10 USER 100 2
10 16-MAR-10 USER 100 2
SQL> SELECT T.CASENUM,
2 T.USER_NAME,
3 T.AUDIT_DATE AS STARTED,
4 (SELECT max(S.AUDIT_DATE) keep (dense_rank first order by S.AUDIT_DATE ASC,S.AUDIT_USECS ASC)
5 from AUDITHISTORY S WHERE S.CASENUM=T.CASENUM AND TYPE_ID=2
6 AND S.USER_NAME=T.USER_NAME
7 AND (S.AUDIT_DATE > T.AUDIT_DATE OR
8 (S.AUDIT_DATE = T.AUDIT_DATE AND S.AUDIT_USECS > T.AUDIT_USECS))
9 ) as ended
10 FROM AUDITHISTORY T WHERE TYPE_ID=1;
CASENUM USER_NAME STARTED ENDED
10 USER 12-MAR-10 14-MAR-10Max
http://oracleitalia.wordpress.com -
Oracle JDev 10g Dev Preview - More generics fun
Building off of the previous post about how imports don't clean up properly when you are using generics... My co-workers and I are having fun working with Generics and the Code Assist feature. As Soon as a collection with Generic information is used in a class, Code assist fails to work with any methods. It shows autocomplete on defining classes, but if, for example, you want to type a method to an instanciated object, lets say,
Collection aColl = new ArrayList();
aColl.ite <ctrl> - <space>
Nothing happens. Nothing to even indicate there is a code assist there.
Is this a known "feature" / "bug"?
Is there going to be another Dev Preview released anytime soon? Our company is leveraging the power of Java 5, and without the tools, it is getting more difficult to use the advanced features....See:
When is JDeveloper 10.1.3 going production?
Sascha -
Recursive Sub-query Factoring vs Hierarchical Query
Experts,
The below are two queries i am executing by using classical approach hierarchical and newest recursive sub-query.Problem is output was different for recursive sub-query factoring, as this is not showing parent-child approach, while connect by prior is showing parent-child fashion.Query 1, i am using hierarchical as it showing correct output parent-child approach, while Query 2 is displaying all the level 1 nodes and then level 2 nodes.I want query 2 output to be same as query 1.Please modify query 2 as required.
Note:The output of the both queries won't be in displayed as it was in sqlplus, or toad.Please copy and use in your command prompts.
QUERY 1:
with main as(select 1 id,'john' name,null mgrid from dual union all
select 2 id,'michael' name,null mgrid from dual union all
select 3 id,'peter' name,null mgrid from dual union all
select 4 id,'henry' name,1 mgrid from dual union all
select 5 id,'nick' name,2 mgrid from dual union all
select 6 id,'pao' name,3 mgrid from dual union all
select 7 id,'kumar' name,3 mgrid from dual union all
select 8 id,'parker' name,3 mgrid from dual union all
select 9 id,'mike' name,5 mgrid from dual),
select lpad(' ',2*(level-1))||name name,level from main start with mgrid is null connect by prior id=mgrid;
OUTPUT :
NAME LEVEL
john 1
henry 2
michael 1
nick 2
mike 3
peter 1
pao 2
kumar 2
parker 2
9 rows selected.
QUERY 2:
with main as(select 1 id,'john' name,null mgrid from dual union all
select 2 id,'michael' name,null mgrid from dual union all
select 3 id,'peter' name,null mgrid from dual union all
select 4 id,'henry' name,1 mgrid from dual union all
select 5 id,'nick' name,2 mgrid from dual union all
select 6 id,'pao' name,3 mgrid from dual union all
select 7 id,'kumar' name,3 mgrid from dual union all
select 8 id,'parker' name,3 mgrid from dual union all
select 9 id,'mike' name,5 mgrid from dual),
/*select lpad(' ',2*(level-1))||name name,level from main start with mgrid is null connect by prior id=mgrid;*/
secmain (id,name,mgrid,hierlevel) as(select id,name,mgrid,1 hierlevel from main where mgrid is null
union all
select m.id,m.name,m.mgrid,sm.hierlevel+1 from main m join secmain sm on(m.mgrid=sm.id))
cycle id set is_cycle to 1 default 0
select lpad(' ',2*(hierlevel-1))||name name,hierlevel from secmain;
OUTPUT :
NAME HIERLEVEL
john 1
michael 1
peter 1
henry 2
nick 2
parker 2
kumar 2
pao 2
mike 3
9 rows selected.Hi,
What's wrong with Query 1? If it's producing the results you want, then why not use it?
One of the nice features of CONNECT BY is that the output is automatically presented in hierachical order. The only way I know of to get the results of a recursive WITH clause in any kind or order is to do it yourself. Here's one way:
WITH secmain (id, name, mgrid, hierlevel, path) AS
SELECT id
, name
, mgrid
, 1 AS hierlevel
, RPAD (name, 10) AS path -- Assuming name is never longer than 10
FROM main
WHERE mgrid IS NULL
UNION ALL
SELECT m.id
, m.name
, m.mgrid
, sm.hierlevel + 1
, sm.path || RPAD (name, 10)
FROM main m
JOIN secmain sm ON (m.mgrid = sm.id)
CYCLE id -- Why do you need this if CONNECT BY NOCYCLE wasn't needed in Query 1?
SET is_cycle TO 1
DEFAULT 0
SELECT lpad ( ' '
, 2 * (hierlevel - 1)
) || name AS name
, hierlevel
FROM secmain
ORDER BY path
;Sorry, I don't have an Oracle 11.2 database right now, so I can't test it.
Edited by: Frank Kulash on Nov 23, 2011 9:15 AM
I didn't know about the SEARCH clause, Use Dom's solution, above. -
Correlated sub-query/outer join problem
Hi,
I have a problem similar to this one (Outer join with correlated subquery but I'm struggling to find a solution for mine!
The following query works OK unless there is no entry in room_prices, so I need to outer join somehow to that table otherwise it doesn't return any rows.
The main difference between the query in the other thread and mine is that mine has an extra table (bookings b) in the sub-query, which is complicating things a bit.
select b.book_from,
b.book_to,
b.flat_rate,
nvl(c.discount,0),
p.hourly,
p.half_day,
p.daily,
p.discountable,
p.surcharge,
l.evening_start,
nvl(b.full_day,'N')
from booking.bookings b,
booking.customer c,
booking.room_prices p,
booking.rooms r,
booking.location l
where b.id = 9272
and c.id = b.customer
and b.room = p.room
and b.room = r.id
and r.loc = l.id
and p.from_date =
select max(p2.from_date)
from booking.room_prices p2
where p2.room = p.room
and p2.from_date < b.book_from
Could anyone suggest a way to re-write the query so that it is outer joined to room_prices?
Thanks,
HazelThanks for both of your responses.
Unfortunately your suggestion didn't work Dmytro - still no rows are returned.
Here are some table creation scripts and test data to insert:
CREATE TABLE BOOKINGS
ID NUMBER NOT NULL,
ROOM NUMBER NOT NULL,
CUSTOMER NUMBER NOT NULL,
BOOK_FROM DATE NOT NULL,
BOOK_TO DATE NOT NULL,
CONFIG VARCHAR2(24) NOT NULL,
CREATED_DATE DATE NOT NULL,
CREATED_BY VARCHAR2(16) NOT NULL,
UPDATED_DATE DATE,
UPDATED_BY VARCHAR2(16),
DELEGATES NUMBER,
NARRATIVE VARCHAR2(256),
CONTACT_DETAILS VARCHAR2(400),
CONFIRMED VARCHAR2(1),
CANC_REASON NUMBER,
FULL_DAY VARCHAR2(1),
FLAT_RATE NUMBER,
STANDBY NUMBER,
TOTAL_STANDBY_TIME DATE,
PRE_STANDBY_TIME DATE,
PRE_STANDBY NUMBER,
GL_CODE VARCHAR2(20)
CREATE TABLE CUSTOMER
ID NUMBER NOT NULL,
CUSTOMER VARCHAR2(160) NOT NULL,
CONTACT_ADDRESS VARCHAR2(240),
CONTACT_TELEPHONE VARCHAR2(20),
CONTACT_EMAIL VARCHAR2(160),
CREATED_DATE DATE NOT NULL,
CREATED_BY VARCHAR2(16) NOT NULL,
UPDATED_DATE DATE,
UPDATED_BY VARCHAR2(16),
DISCOUNT NUMBER(5,2),
CUST_TYPE VARCHAR2(1),
CONTACT_FAX VARCHAR2(20),
DEBTOR_NO VARCHAR2(20),
BC_CONTACT VARCHAR2(64)
CREATE TABLE ROOMS
ID NUMBER NOT NULL,
LOC NUMBER NOT NULL,
NAME VARCHAR2(160) NOT NULL,
CREATED_DATE DATE NOT NULL,
CREATED_BY VARCHAR2(16) NOT NULL,
UPDATED_DATE DATE,
UPDATED_BY VARCHAR2(16),
ACTIVE VARCHAR2(1),
ROOM_DESC VARCHAR2(2000)
CREATE TABLE LOCATION
ID NUMBER NOT NULL,
NAME VARCHAR2(240) NOT NULL,
ADDRESS VARCHAR2(240),
PCODE VARCHAR2(8),
CREATED_DATE DATE NOT NULL,
CREATED_BY VARCHAR2(16) NOT NULL,
UPDATED_DATE DATE,
UPDATED_BY VARCHAR2(16),
CONF_RDF VARCHAR2(10),
CLOSING VARCHAR2(5),
EVENING_START VARCHAR2(5)
CREATE TABLE ROOM_PRICES
ROOM NUMBER NOT NULL,
FROM_DATE DATE NOT NULL,
HOURLY NUMBER(6,2),
HALF_DAY NUMBER(6,2),
DAILY NUMBER(6,2),
DISCOUNTABLE VARCHAR2(1),
CREATED_DATE DATE NOT NULL,
CREATED_BY VARCHAR2(16) NOT NULL,
UPDATED_DATE DATE,
UPDATED_BY VARCHAR2(16),
SURCHARGE NUMBER(6,2)
Insert into bookings
(ID, ROOM, CUSTOMER, BOOK_FROM, BOOK_TO, CONFIG, CREATED_DATE, CREATED_BY, UPDATED_DATE, UPDATED_BY, DELEGATES, NARRATIVE, CONTACT_DETAILS, CONFIRMED, FLAT_RATE, PRE_STANDBY_TIME, PRE_STANDBY)
Values
(9272, 7466, 4946, TO_DATE('10/25/2005 10:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('10/25/2005 13:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'Default', TO_DATE('10/27/2005 15:35:02', 'MM/DD/YYYY HH24:MI:SS'), 'HSIS0201', TO_DATE('10/27/2005 15:36:26', 'MM/DD/YYYY HH24:MI:SS'), 'HSIS0201', 1, 'another meeting', 'Hazel', 'Y', 40, TO_DATE('10/25/2005 09:30:00', 'MM/DD/YYYY HH24:MI:SS'), 1800);
Insert into customer
(ID, CUSTOMER, CONTACT_ADDRESS, CONTACT_TELEPHONE, CREATED_DATE, CREATED_BY, CUST_TYPE, BC_CONTACT)
Values
(4946, 'Association of Teachers', 'Address', '0191 8887777', TO_DATE('09/22/2003 08:05:47', 'MM/DD/YYYY HH24:MI:SS'), 'Dataload', 'B', 'Miss Jones');
Insert into rooms
(ID, LOC, NAME, CREATED_DATE, CREATED_BY, UPDATED_DATE, UPDATED_BY, ACTIVE)
Values
(7466, 308, ' Counselling Room 1', TO_DATE('04/11/2005 10:55:33', 'MM/DD/YYYY HH24:MI:SS'), 'BJAC1906', TO_DATE('06/22/2005 14:43:50', 'MM/DD/YYYY HH24:MI:SS'), 'HSIS0201', 'Y');
Insert into location
(ID, NAME, ADDRESS, PCODE, CREATED_DATE, CREATED_BY, UPDATED_DATE, UPDATED_BY, CONF_RDF, CLOSING, EVENING_START)
Values
(308, 'Business Centre', 'Address', 'NE30 1NT', TO_DATE('03/19/2003 13:07:35', 'MM/DD/YYYY HH24:MI:SS'), 'BJAC1906', TO_DATE('06/09/2005 11:17:09', 'MM/DD/YYYY HH24:MI:SS'), 'BJAC1906', 'BKCF_2', '22:30', '18:00');
Thanks,
Hazel -
Dear Sir/Madam
I'm a student who is interested in Oracle Database and
I have some problems with JOIN and Sub-query.
I hope so many of you could help me.
if i use JOIN without sub-query, may it be faster or not?
SELECT field1, field2 FROM tableA INNER JOIN tableB
if i use JOIN with sub-query, may it be faster or not?
SELECT field1,field2,field3 FROM tableA INNER JOIN (SELECT field1,field2 FROM tableB)
Thanks in advance!Hi,
fac30d8e-74d3-42aa-b643-e30a3780e00f wrote:
Dear Sir/Madam
I'm a student who is interested in Oracle Database and
I have some problems with JOIN and Sub-query.
I hope so many of you could help me.
if i use JOIN without sub-query, may it be faster or not?
SELECT field1, field2 FROM tableA INNER JOIN tableB
if i use JOIN with sub-query, may it be faster or not?
SELECT field1,field2,field3 FROM tableA INNER JOIN (SELECT field1,field2 FROM tableB)
Thanks in advance!
As the others have said, the execution plan will give you a better idea about which is faster.
If you're trying to see how using (or not using) a sub-query affects performance, make the rest of the queries as similar as possible. For example, include field3 in both queries, or ignore field3 in both queries.
In this particular case, I guess the optimizer would do the same thing either way, but that's just a guess. I can't see your execution plans.
In general, simpler code is faster, and better in other ways, too. In this case
tableB
is simpler than
(SELECT field1, field2 FROM tableB)
Why do you want a sub-query in this example? -
Hello, I am attempting to create a sub query to display certain information. On the form that the information is inputted, the user has the option of inputting a phone number extension if applicable. If this is the case I want the phone number to show as 555-867-5309 (x1234). However without the problem checks, the (x ) will show up regardless and looks sloppy. Here is the code I have, please advise.
There will be two spots this will be needed -- for the requester's contact number and the app manager (sme) contact information.
Thanks.
select
pm.pk_proj_master_id "Project Number",
pm.trackit_work_order "TrackIt Work Order",
pm.name "Project Name",
pm.status "Project Status",
req.last_name||', '||req.first_name||', '||req.middle_initial||'.' "Requestor Name",
rde.department_group_descr "Requestor Department Name",
req.department_descr "Requestor Division Name",
pm.requester_ext,
case
when pm.requester_ext in
select
pm.requester_phone|| '(x'||pm.requester_ext ||')' "Requesters Number"
from
protrac_master pm
where
pm.requester_ext is not null
else
select
pm.requester_phone "Requesters Number"
from
protrac_master pm
end as "Requester Number",
man.last_name||', '||man.first_name||', '||man.middle_initial||'.' "SME Name",
mdg.department_group_descr "SME Department Name",
man.department_descr "SME Division Name",
pm.app_manager_ext,
case
when pm.app_manager_ext in
select
pm.app_manager_phone|| '(x'||pm.app_manager_ext ||')' "SME Number"
from
protrac_master pm
where
pm.app_manager_phone is not null
else
select
pm.app_manager_phone "SME Number"
from
protrac_master pm
end as "Requester Number",
pm.createby_date "Date Entered",
pm.date_begin "Date Began",
pm.date_completed "Date Completed",
pm.estimated_date "Estimated Completion Date"
from
protrac_master pm,
cobr.vw_pps_payroll req, cobr.department_group rde,
cobr.vw_pps_payroll man, cobr.department_group mdg
where
pm.requester_id = req.emple_no and
pm.requester_dept_id = rde.pk_department_group_id and
pm.app_manager_id = man.emple_no and
pm.app_manager_dept_id = mdg.pk_department_group_id
order by
pm.pk_proj_master_idI think you can avoid the whole sub query thing just by using the NVL2 function:
select ...
, NVL2( pm.requester_ext
, pm.requester_phone|| '(x'||pm.requester_ext ||')'
, pm.requester_phone) "Requesters Number"
, NVL2( pm.app_manager_ext
, pm.app_manager_phone|| '(x'||pm.app_manager_ext ||')'
, pm.app_manager_phone) "Manager Number"
from protrac_master pm
, ... -
See my query problem for sub query
hi master
Sir see my query
select lpad(' ',2*(level-1)) || to_char(chartofacc.child),parent,accid as accounted ,title,
case when fstatus=1 then
(select sum(drbal) from accbal where parent = accounted)
when fstatus is null then
(select drbal from accbal where accid = accounted)
end subtotal
from chartofacc
start with chartofacc.parent is null
connect by prior chartofacc.child = chartofacc.parent order by chartofacc.accid
I try but not success how I pass main query value or code in sub query or inner query
My requirement is inner query get total from accbal table how have accid that in main query accid or accounted
How I pass main query value or accid code to inner query and inner query give result as per main query accid
How I get subtotal and total by one query
Please give me idea or code
Thanking you
aamirYou could write your main SELECT statement as inline view or use the WITH clause, like this:
WITH ctf AS (SELECT LPAD (' ', 2 * (LEVEL - 1)) || TO_CHAR (chartofacc.CHILD), PARENT,
accid AS accounted, title,
FROM chartofacc
START WITH chartofacc.PARENT IS NULL
CONNECT BY PRIOR chartofacc.CHILD = chartofacc.PARENT
ORDER BY chartofacc.accid
SELECT ctf.*,
CASE WHEN fstatus = 1
THEN (SELECT SUM (drbal)
FROM accbal
WHERE PARENT = accounted)
WHEN fstatus IS NULL
THEN (SELECT drbal
FROM accbal
WHERE accid = accounted)
END subtotal
FROM ctf
not tested
C. -
Dear All,
Based on below image show that the error message, As i know is aggregation problem in sub query, May i know how to solve it ? thank you.Please have enough Netiquette to post DDL and not pictures.
Think about how absurd "MIN(AVG(AGE)))" on two levels. No, really, you can see that this is absurd if you have any concept of sets.
No competent programmer would put age in a column. It is a dynamic value! The constant is birith_date. Age is computed in the presentation layers.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
I am using 10g dev. I am converting the reports from 6i. I have some values in DB using special characters ie. for deg C (instead of deg i use o ) . now when I display it on PDF it shows as a junk character. How can we solve it. while I was using Oracle 6i this was displaying properly.
Thanks
MGHi Con,
For the case of worksheet script, the special characters work fine for me in 3.2.20.09.87. What version are you on?
When running the query from the Reports view, here are a couple of hints
1. In the report's SQL Query definition, use Style: Script, not Table
2. Edit your SQL Query to use bind variables, not substitution variables, like so...
select ' Action:' || :V_PATCH || ' ' || :V_ACTION || chr(10) ||
'Database: '|| instance_name || chr(10) ||
' Date: '||to_char(sysdate,'DD-MON-YYYY HH24:MI') || chr(10)
from v$instance3. Finally, define them in the Binds section.
Hope this helps,
Gary -
Possibility of launch of a sub query in a query
Hi developers,
I have an urgent problem! I have created a query to calculate with a formula an indicator but if I effectue a drill down for customer I have a correct result while when I drill down for country the rows compact the value and the result change in wrong. Is There the possibility or a functionality who allows the launch of a query with selection on another query.
Have Someone a solution to this problem ?
Thanks,
Domeico
Message was edited by: Domenico MantiHi Domeico,
I'm not sure I understand your problem correctly, but instead of launching a sub query in a query I would recommend to check, whether a variable with replacement path (reference to characteristic (constant 1)) might solve your problem.
http://help.sap.com/saphelp_nw04/helpdata/en/03/6ba03cc24efd1de10000000a114084/frameset.htm
and
http://help.sap.com/saphelp_nw04/helpdata/en/ca/5f9ac61a205a459d0e7ef313d10321/frameset.htm
Heike -
I have a doubt with the use of sub-queries. I am using a query like as below & it is giving me expected result:-
SELECT ISS.item_type_id,count(1) + (select sum(invoiced_qty) from ppbs_invoice_detail where
trunc(created_date) between '01-jul-05' and '04-jul-05' and item_type_id=ISS.item_type_id) qty
FROM ppbs_INV_SIM_SERIAL ISS
WHERE INSTR('ROAAIT',STATUS)>0
AND INSTR('NU0NU1NU2NU3SC0',CITY_CODE) =0
and ISS.item_type_id in ('SM17')
group by ISS.item_type_id --
I will re-write above query & i have been using queries like :-
SELECT ISS.item_type_id,count(1) qty
FROM ppbs_INV_SIM_SERIAL ISS
WHERE INSTR('ROAAIT',STATUS)>0
AND INSTR('NU0NU1NU2NU3SC0',CITY_CODE) =0
and ISS.item_type_id in ('SM17') and item_type_id in
(select item_type_id from ppbs_invoice_detail where
trunc(created_date) between '01-jul-05' and '04-jul-05' and item_type_id=ISS.item_type_id)
group by ISS.item_type_id
my doubt here is that the sub-query in the first query is coming before i use the table 'ppbs_inv_sim_serial ISS' whereas in the second query, the table use 'ppbs_inv_sim_serial ISS' is coming before the sub-query use. I have been using queries where table name is defined before the sub-query.
I hope, my question is clear. Please help in solving the doubt.
Regards.I don't see a problem with the table aliasing that you seem to doubt, but your first query won't run. I boiled the thing down to basics: SQL>create table t1 (one number, val_1 number );
Table created.
SQL>create table t2 (one number, val_2 number );
Table created.
SQL>insert into t1 values (1,1);
1 row created.
SQL>insert into t2 values (1,2);
1 row created.
SQL>select a.one, count(1) + ( select sum(val_2) from t2 where t2.one = a.one) qty
2 from t1 a
3 group by one;
select a.one, count(1) + ( select sum(val_2) from t2 where t2.one = a.one) qty
ERROR at line 1:
ORA-00979: not a GROUP BY expression
SQL>
SQL>select a.one, count(1) qty
2 from t1 a
3 where a.one in ( select one from t2 where one = a.one)
4 group by one;
ONE QTY
1 1It's superfluous to "where a.one in ( select.. where = a.one) but that's your code basically. I would just do "=" -
How to make outer join in Sub Query?
Hi!
I'm facing one problem. Can anyone tell me - how to make outer join in sub query?
I'm pasting one sample code -
select e.empno, e.ename,e.job,e.sal,d.deptno,d.dname
from d_emp e, d_dept d
where e.deptno(+) = (
case
when d_dept.deptno = 10
then
select deptno
from d_dept
where dname = 'SALES'
else
d_dept.deptno
end
SQL>
ERROR at line 15:
ORA-01799: a column may not be outer-joined to a subqueryHow to resolve this issue?
Regards.And any luck with this?
SQL> with emp as
2 (select 100 empno, 'Abcd' ename, 1000 sal, 10 deptno from dual
3 union all
4 select 101 empno, 'RRR' ename, 2000 sal, 20 deptno from dual
5 union all
6 select 102 empno, 'KKK' ename, 3000 sal, 30 deptno from dual
7 union all
8 select 103 empno, 'PPP' ename, 4000 sal, 10 deptno from dual
9 )
10 ,dept as
11 (select 10 deptno, 'FINANCE' dname from dual
12 union all
13 select 20 deptno, 'SALES' dname from dual
14 union all
15 select 30 deptno, 'IT' dname from dual
16 union all
17 select 40 deptno, 'HR' dname from dual
18 )
19 select e.empno, e.ename, e.sal, d.deptno, d.dname
20 from emp e,
21 (select decode(a.deptno, 10, b.deptno, a.deptno) deptno, decode(a.deptno, 10, b.dname, a.dname) dname
22 from dept a, (select deptno, dname
23 from dept
24 where dname = 'SALES'
25 ) b
26 ) d
27 where e.deptno(+) = d.deptno
28 /
EMPNO ENAM SAL DEPTNO DNAME
101 RRR 2000 20 SALES
101 RRR 2000 20 SALES
102 KKK 3000 30 IT
40 HR
SQL> Cheers
Sarma. -
The issue with using the multiple columns sub-query in WHERE clause
Hi All,
my database version is 10.2.
the problem i am trying to deal with is that when I use multiple column sub-query in the WHERE clause of the SELECT statement, the actual row number returned from the sub-query is different from the whole statement.
And what I found is that, whenever there is NULL in any of those columns returned from the SUB-QUERY, the outer query will just return NULL for that whole row.
Here is an example:
select empno, ename, job, mgr, hiredate, sal, deptno from EMP
intersect
select empno, ename, job, mgr, hiredate, sal, deptno from t;
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT NULL 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
select * from EMP where (EMPNO, ENAME, job, MGR, HIREDATE, SAL, DEPTNO) in (
select empno, ename, job, mgr, hiredate, sal, deptno from EMP
intersect
select empno, ename, job, mgr, hiredate, sal, deptno from t);
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10 If I specifically deal with the NULL situations for the columns which might return NULL, I can get the result right.
select * from EMP where (EMPNO, ENAME, job, NVL(MGR,-1), HIREDATE, SAL, DEPTNO) in (
select empno, ename, job, nvl(mgr,-1), hiredate, sal, deptno from EMP
intersect
select empno, ename, job, nvl(mgr,-1), hiredate, sal, deptno from t);
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT null 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10 the problem is that, I feel this is a very lame way of handling it.
So, I wonder or am asking if there is any better or standard way to do it?
any help would be highly appreciated.
ThanksHi,
As you discovered, INTERSECT treats NULL as a value, but IN does not.
What you did with NVL is one way to handle the situation. If there was a chance that any of the columns could be NULL, then you might prefer something like this:
select *
from EMP
where ( EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
) in (
select EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
from emp
intersect
select EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
from t
);This assumes that you can identify some string (I used '~') that never occurs in the strings in these tables.
This is implicitly converting the NUMBERs. That's usually not a good thing to do. but explicitly converting them would make this even more tedious.
You should explicitly convert any DATEs to strings, however. Depending on your default format, and your data, you might get away with implicit conversions even for DATEs, but don't bet on it.
If you had to do this often, you might consider writing a user-defined function:
delimited_string (empno, ename, job, mgr, hiredate, sal, deptno) would return a string like
'7839~KING~PRESIDENT~~17-NOV-1981~5000~10'
This will make the coding easier, but beware: it will make the execution slower. -
Hi All,
I have a query..I have a table employee ..I also have an another table manager
I have two tables. employee and manager tables.
employee columns: emp_key, emp_name,parent_emp_id , level_no
manager columns: man_key,man_name,man_id
I should check the manager key equals emp key . if found i shud check whether emp key has parent emp key, if found i shud take emp name of that row and update in manager name in manager table.
I tried co related sub query but it works only in select statement but not update. can any one help me on this .Also can any one tell me whether we can use CONNECT BY PRIOR statement. any one tell me whether we can use CONNECT BY PRIOR statement.Hi,
Sorry, I still don't uderstand the problem well enough to help.
The INSERT statements look okay now. What did you do to make them appear? (It would be good to know, when other people have the same problem.)
Can you do something similar to make the CREATE TABLE statements appear?
You have 4 rows in one table and 1 row in another table. You want to write an UPDATE statement (or something) that changes them. Is that right?
What should be in the changed tble after that UPDATE statement? Use a text editor to show what "SELECT * FROM ..." should produce. Post it on this site, but type these 6 characters:
\(small letters only, inside curly brackets) before and after each the section of formatted text, to preserve spacing.
MERGE is often eaiser than UPDATE, especially if the UPDATE statement would need a complicated WHERE clause.
It might help to see the UPDATE statement you tried, even though it doesn't work.
Maybe you are looking for
-
Using ViewObject with a foreach tag in a jsp
Hi guys I have the following scenario: 1- I call a certain .jsp page 2- a controller class (extends servlet) is called, this class: takes a view object, executes it's query, forwards the result to the jsp page. 3- jsp page has a foreach tag that iter
-
when I'm looking at my recent text messages on my phone, and I'm on the screen where I see a list of people I have texted. I click on a name to open up the conversation, however it is blank and doesn't show the conversation even though we've been te
-
I'm importing some CDs into iTunes and the CDDB info is totally wrong. Does anyone know how to clear the cache so my Mac forgets it ever looked up the disc? I tried trashing iTunes prefs but it didn't work. Thanks in advance.
-
Konqueror + webkit + flashblock
Hi, while konqueror using webkit plays nice with sites that require flash, it cannot load plugins on demand. i can't get that "click to play" button to appear. Any clues?
-
Cannot find the hana output adapter, please help me.
I have downloaded the latest version of ESP in SAP software Download center, here is the screenshot However, when I try to install the hana output adapter in the custom installation process, I couldn't find it. Here is the screenshot: please help me