How to simplify this query in sql simple select stmt
HelloPlease simplify the query
I want to convert this query in a single select statement. Is this possible?
If uarserq_choice_ind is not null then
Select ubbwbst_cust_code
From ubbwbst,utrchoi
Where utrchoi_prop_code=ubbwbst_cancel_prod
Else
Select max(utvsrvc_ranking)
From utvsrvc,ubbwbst
Where utvsrvc_code=ubbwbst_cancel_prod
End if
Select ubbwbst_cust_code as val
From ubbwbst,utrchoi
Where utrchoi_prop_code=ubbwbst_cancel_prod
AND uarserq_choice_ind is not null
union all
Select max(utvsrvc_ranking) as val
From utvsrvc,ubbwbst
Where utvsrvc_code=ubbwbst_cancel_prod
and uarserq_choice_ind is null
Without more information, we are unable to combine the two queries in 1 without a union.
Looks like you select values totally disperate of totally different tables
Tags: Database
Similar Questions
-
Can anyone simplify this query
Here's a DOF from two tables
1. CREATE TABLE (EMPL)
NUMBER OF SNO
ENAME VARCHAR2 (25).
USE VARCHAR2 (25).
KEY ELEMENTARY SCHOOL (SNO)
);
2. CREATE TABLE EMPL_DET)
NUMBER OF SNO
SAL VARCHAR2 (25)
);
Here are tables LMD
INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (1, 'SMITH', 'CLERK');
INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (2, 'SMITH', 'MANAGER');
INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (3, 'TOM', 'CLK');
INSERT INTO EMPL_DET (SNO, SAL) VALUES (1, '1000');
INSERT INTO EMPL_DET (SNO, SAL) VALUES (2, "10000");
INSERT INTO EMPL_DET (SNO, SAL) VALUES (3, '900');
I want to calculate TotalSAL (column: empl_det.) SAL) of each employee (empl.ename) with job-description (empl.job).
Means I want following the lines of output
1.(Job,TotalSAL,Ename)-> (CLERK, 11000, SMITH)
2->.(Job,TotalSAL,Ename) (MANAGER, 11000, SMITH)
3->.(Job,TotalSAL,Ename) (CLK, 900, TOM)
I tried to write down to unique ename
Select JOB, x.sal, ename in empl,
(
Select sum (sal) sal empl_det where sno in
(select sno to empl where ename = 'SMITH')
) x
where ename = 'SMITH '.
order by ename
each ename, I draw from this query. How can I make the ename list (TOM SMITH) to this request?
Or can anyone simplify this query?Hello
in this case, you need to use is analytical functions:
that is to say:
SELECT e.job, SUM (d.sal) OVER (PARTITION BY e.ename) AS "TotalSal" , e.ename FROM empl e, empl_det d WHERE d.sno = e.sno AND e.ename IN ('SMITH', 'TOM') ORDER BY e.ename; JOB TotalSal ENAME ------------------------- ---------- ------------------------- CLERK 11000 SMITH MANAGER 11000 SMITH CLK 900 TOM
Kind regards.
AlPublished by: Alberto Faenza on 27 November 2012 15:34
Corrected query -
No idea how to write this query
Hi, My Data is as below
DocNum doc_date type of amount
1154 15 November 11 232501.5 invoice
200206 4 November 11 - 243672.64 credit memo
Note flow 111 5 November 555.22 11
Output must be
DocNum doc_date amount Type AmountDR AmountCR
1154 232501.5 15 November 11 Bill 232501.5
Note credit 200206 4 November 11 - 243672.64 - 243672.64
Note flow 111 5 November 555.22 11 555.22
If the amount is > 0, then it must be displayed in the value of the amount to be AmountDR
If amount < 0 then it must be displayed in the value of the sum amount CR
Can help how to write this querywith sample_table as ( select 1154 Docnum,date '2011-11-15' doc_date,232501.5 Amount,'Invoice' type from dual union all select 200206,date '2011-11-04',-243672.64,'Credit Memo' from dual union all select 111,date '2011-11-05',555.22,'Debit Memo' from dual ) select Docnum, doc_date, Amount, type, case when Amount >= 0 then Amount end AmountDR, case when Amount < 0 then Amount end AmountCR from sample_table / DOCNUM DOC_DATE AMOUNT TYPE AMOUNTDR AMOUNTCR ---------- --------- ---------- ----------- ---------- ---------- 1154 15-NOV-11 232501.5 Invoice 232501.5 200206 04-NOV-11 -243672.64 Credit Memo -243672.64 111 05-NOV-11 555.22 Debit Memo 555.22 SQL>
SY.
-
How to achieve this using the sql query?
Hello gurus,
I have a table like this
now my result should be like thisid name 1 a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 10 j 11 k 12 l 13 m
How to achieve by sql query?id name id name id name 1 a 6 f 11 k 2 b 7 g 12 l 3 c 8 h 13 m 4 d 9 i 5 e 10 j
Thank you and best regards,
friend
Edited by: most wanted! February 22, 2012 05:55Hello
Did you mean this:
with a as (select 1 id ,'a' name from dual union all select 2 id ,'b' name from dual union all select 3 id ,'c' name from dual union all select 4 id ,'d' name from dual union all select 5 id ,'e' name from dual union all select 6 id ,'f' name from dual union all select 7 id ,'g' name from dual union all select 8 id ,'h' name from dual union all select 9 id ,'i' name from dual union all select 10 id ,'j' name from dual union all select 11 id ,'k' name from dual union all select 12 id ,'l' name from dual union all select 13 id ,'m' name from dual ) select id_1 ,name_1 ,id_2 ,name_2 ,id_3 ,name_3 from ( select id id_1 ,name name_1 ,lead(id,5) over (order by id) id_2 ,lead(name,5) over (order by id) name_2 ,lead(id,10) over (order by id) id_3 ,lead(name,10) over (order by id) name_3 ,rownum r from a ) where r <=5 D_1 NAME_1 ID_2 NAME_2 ID_3 NAME_3 ---------------------- ------ ---------------------- ------ ---------------------- ------ 1 a 6 f 11 k 2 b 7 g 12 l 3 c 8 h 13 m 4 d 9 i 5 e 10 j
Kind regards
Peter
-
How to write this query in the hierarchy
Hi gurus,
Really need your help on this query. Thank you very much in advance.
SELECT t1.key as root_key , (SELECT t2.unit_id AS unit_id level-1 AS level , t2.name, t2.creator FROM tab t2 START WITH t2.unit_id = t1.unit_id -----check each node as root CONNECT BY prior t2.unit_id = t2.parent_unit_id ) t1.name as parent_unit_name FROM tab t1
I'll write a query of the hierarchy as above, and that EACH line (node, totally more than 10200) is checked as root node to see how many sheets are accessible for her... It must be implemented in a single query.
I know inline query should NOT return multiple rows or multiple columns, but the inline elements are necessary and can certainly be made in a correct solution.
(env):
Database Oracle 12 c Enterprise Edition Release 12.1.0.2.0 - 64 bit Production
PL/SQL Release 12.1.0.2.0
)
Test data:
select 1 as unit_id, null as parent_organization_unit_id, 'U1' as name from dual union all select 2, 1, 'U2' FROM DUAL UNION ALL SELECT 3, NULL, 'U3' FROM DUAL UNION ALL SELECT 4, 3, 'U4' FROM DUAL UNION ALL SELECT 5, 2, 'U5' FROM DUAL UNION ALL SELECT 6, 5, 'U6' FROM DUAL UNION ALL SELECT 7, 6, 'U7' FROM DUAL UNION ALL SELECT 8, 5, 'U8' FROM DUAL UNION ALL SELECT 9, 5, 'U9' FROM DUAL;
Final result should be like this
key unit_id, level, name, parent_name 1 1 0 u1 u1 1 2 1 u2 u1 1 5 2 u5 u1 1 6 3 u6 u1 1 7 4 u7 u1 1 8 3 u8 u1 1 9 3 u9 u1 2 2 0 u2 u2 2 5 1 u5 u2 2 6 2 u6 u2 2 7 3 u7 u2 2 8 2 u8 u2 2 9 2 u9 u2
Don't know how get you your output, it does not match your data...
with tab as)
Select 1 as unit_id, null as parent_organization_unit_id 'U1' as the name of double
Union of all the
Select 2, 1, 'U2' FROM DUAL
UNION ALL
SELECT 3, NULL, 'U3' FROM DUAL
UNION ALL
SELECT 4, 3, 'U4' FROM DUAL
UNION ALL
SELECT 5, 2, 'U5' OF THE DOUBLE
UNION ALL
SELECT 6, 5, 'U6' OF THE DOUBLE
UNION ALL
SELECT 7, 6, "U7" OF THE DOUBLE
UNION ALL
SELECT 8, 5, 'U8' FROM DUAL
UNION ALL
9. SELECT, 5, 'U9' FROM DUAL
)
Select dense_rank() key (order by connect_by_root unit_id), unit_id, level - 1 as 'LEVEL', connect_by_root name root_parent_name
t tab
Start with parent_organization_unit_id is null
Connect prior unit_id = parent_organization_unit_id
KEY UNIT_ID LEVEL ROOT_PARENT_NAME 1 1 0 "U1". 1 2 1 "U1". 1 5 2 "U1". 1 6 3 "U1". 1 7 4 "U1". 1 8 3 "U1". 1 9 3 "U1". 2 3 0 "U3". 2 4 1 "U3". -
How to optimize this query?
Hello
I have a query like this:
Merge into the table st1
using (select * from (select pk, value, diff_value, m_date, row_number () over (PARTITION pk ORDER BY diff_value) rnk)
from (select distinct / * + Full (t1) full (t2) * / t1.pk, t2.m_date)
, Case when (t1.m_date = t2.m_date) then "CORRESPONDENCE".
When (t2.m_date BETWEEN t1.m_date-1 and t1.m_date + 1) then ' MATCHED WITH +/-1gg.
When (t2.m_date BETWEEN t1.m_date-2 and t1.m_date + 2) then "MATCHED WITH +/-2 days.
else "
end value_match
Case when (t1.m_date = t2.m_date) then 0
Where (t2.m_date BETWEEN t1.m_date + 1 and t1.m_date - 1) then 1
Where (t2.m_date BETWEEN t1.m_date + 1 and t1.m_date - 1) then 2
else "
end diff_value
of table t2, t1 table
where t1.value is null
and t1.id = t2.id)
where value_match is not null)
where rnk = 1) s
on (st1.pk = s.pk)
WHEN MATCHED THEN
Update set st1.value = s.value_match, st1.diff_value = s.diff_value, st1.up_date = s.m_date
where st1.value is null.
Explain the plan:
Table1 a record 3Million and table 2 has 1 million records.
I used gather stats before you run this query and 'Full' trick, even in this case, he is running for 45 minutes.
Please suggest the best solution to optimize this query.
Thanks in advance.
Remove the tips.
No need for the separate.
Get the diff by ceil (abs(t2.m_date-t1.m_date)) and the filter for that where value_diff<>
Assing the statement ".. MATCHED" lately in the update clause.
Maybe give exactly to your needs with a small example may be the query may be getting more simplified or not what you want it to do.
-
How to write this query?
Hi people,
I need to get a query in which a set of records, I get ONLY those which previous registry has a field with a value to this topic. Other values, the field can contain are not necessary.
I know that sounds easy but... I can't get it.
So, for Oracle 10 g 2... Here's my query:
SELECT a.person_id, a.person_status, a.message_id, a.order_id
OF t_HR one
WHERE a.person_status = "rejected".
AND a.id >
(SELECT max (b.id)
OF t_HR b
WHERE b.person_id = a.person_id
and b.order_id = a.order_id
AND b.person_status! "revised =".
B.ID AND < a.id)
ORDER BY desc a.id
Let me explain:
1 - HR table is a table of people. These people has serveral STATUS.
2 - ID is a sequential (each www.voyages-sncf.com has a different identification number).
3 - the application must get THAT all people "rejected".
4. - However, (subquery) I need ONLY those that previous register (the second register) holds a status of "OK". If the person holds a "revised" status he's not, he should be the next register (the third)
5.-L' ORDER ID DESC, so is the first register must have a STATUS = "rejected" and the second a 'OK '. IF the second register = "revised", then the third register must be 'OK '. And I need this query.
HOW DO?
My problem: the subquery gives you previous register of the same guy, but... it does not give you the value of the State, I need, which is 'OK '.
I tried to add to the subquery...
SELECT max (b.id)
OF mod_human_resource b
WHERE b.person_id = a.person_id
and b.order_id = a.order_id
AND b.person_status = 'OK '.
AND b.id < a.id
... but if I have 5 records of that person, the first is "rejected", the second is "accepted", the third is 'new' and the fourth is 'OK'... the subquery gives you the 4th register and which is not correct for me, it must be only the second one (prior to the first State registry).
I need to be a query, because I need to use it on a MERGER for a DWH.
If there is another way (function, or even a procedure) to make the MERGER rather than with a request, which would be ok too. I am poor DWH knowledge.
Thanks in advance.
Hello
So, you need to know if a line is the 'first' line, and you should also know what is the 'next' status, (even the 'first' and 'next' are already defined). This sounds like a job for analytical functions. ROW_NUMBER can tell you if a line is first or not, and LEAD can tell you what a value on the next row.
Since you post CREATE TABLE and INSERT statements for your own table, I'll use the table scott.emp to illustrate.
Consider these data from scott.emp:
SELECT DeptNo
ename
work
FROM scott.emp
ORDER BY deptno
ename DESC
;
Output:
DEPTNO ENAME JOB
---------- ---------- ---------
10 MILLER CLERK
PRESIDENT OF KING 10
MANAGER 10 CLARK
20 SMITH CLERK
ANALYST SCOTT 20
20 JONES MANAGER
20 FORD ANALYST
20 ADAMS CLERK
30 WARD SALESMAN
SELLER OF 30 TURNER
30 MARTIN SALESMAN
30 JAMES CLERK
MANAGER BLAKE 30
30 ALLEN SALESMAN
Now, let's say we want only who know the departments where the forefront (in order descending ename) a job = 'CLERK', and the following line (also in descending by ename order) = "ANALYST" job, and we want to know the ename of the first row. In other words, the correct output is:
DEPTNO ENAME
---------- ----------
20 SMITH
Note that deptno = 10 is not included, even if the first task is to "CLERK." that was because the second job in deptno = 10 is the "PRESIDENT", not "ANALYST."
Here's a way to get these results:
WITH got_analytics AS
(
SELECT ename, deptno, job
ROW_NUMBER () OVER (PARTITION BY deptno
ORDER BY ename DESC
) AS r_num
LEAD (employment) OVER (PARTITION BY deptno
ORDER BY ename DESC
) AS next_job
FROM scott.emp
)
SELECT deptno, ename
OF got_analytics
WHERE r_num = 1
AND job = 'CLERK '.
AND next_job = 'ANALYST '.
;
I hope that answers your question.
If this isn't the case, then, as Dan (and the FAQ forum) said, post CREATE TABLE and INSERT statements for some sample data and the exact results you want from these data.
Post your query, based on the one I have posted more top and ponit out where he gets results.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum: https://forums.oracle.com/message/9362002#9362002
-
How to rewrite this query to get the correct results?
Friends,
DB: 9iR2
I need to get the name of the employee and the employee number that are not in the table of presence.
but this query is not the right answer.
Thank youselect e.eno,e.ename from empl e where e.eno not in (select a.eno from attendance a)
Depending on your data
SQL> create table attendance( 2 ENO VARCHAR2(5), 3 TDATE VARCHAR2(10), 4 IN_TIME VARCHAR2(6), 5 OUT_TIME VARCHAR2(6), 6 SHIFT_NO NUMBER(1)); Table created. SQL> create table empl( 2 ENO VARCHAR2(5), 3 ENAME VARCHAR2(75)); Table created. SQL> insert into empl values('11','AA'); 1 row created. SQL> insert into empl values('12','AB'); 1 row created. SQL> insert into empl values('13','AC'); 1 row created. SQL> insert into empl values('14','AD'); 1 row created. SQL> insert into empl values('15','AF'); 1 row created. SQL> insert into attendance values('11','23-3-2009','9.00','6.00',1); 1 row created. SQL> insert into attendance values('14','24-3-2009','9.00','6.00',1); 1 row created. SQL> insert into attendance values('11','25-3-2009','9.00','6.00',1); 1 row created. SQL> insert into attendance values('13','23-3-2009','9.00','6.00',1); 1 row created. SQL> insert into attendance values('15','23-3-2009','9.00','6.00',1); 1 row created. SQL> commit; Commit complete. select e.eno,e.ename from empl e where not exists(select 1 from attendance a where a.eno=e.eno); ENO ENAME 12 AB
Twinkle
-
How to convert my query in SQL procedure
Hi all
I run a SQL query in my process of page to set a few articles on my page.
Start
declare
l_vc_arr2 APEX_APPLICATION_GLOBAL. VC_ARR2;
Start
IF APEX_APPLICATION. G_F01. COUNT = 0 THEN
RAISE_APPLICATION_ERROR (-20001, 'Please select a model of e-mail to change!');
END IF;
IF APEX_APPLICATION. G_F01. COUNT > 1 THEN
RAISE_APPLICATION_ERROR (-20001, 'Please select a model unique to change both message!');
END IF;
BECAUSE me in 1.APEX_APPLICATION. G_F01. Count
LOOP
l_vc_arr2: = APEX_UTIL. STRING_TO_TABLE (APEX_APPLICATION. G_F01 (i),' $');
: P32_SUBJECT: = l_vc_arr2 (1);
: P32_TYPE: = l_vc_arr2 (2);
: P32_BODY: = l_vc_arr2 (3);
END LOOP;
end;
end;
It works perfectly fine. Now, I want to turn this query in custom procedure. I write the following code to create the SQL procedure,
create or replace
procedure Edit_EmailTemplate as
Start
declare
l_vc_arr2 APEX_APPLICATION_GLOBAL. VC_ARR2;
Start
IF APEX_APPLICATION. G_F01. COUNT = 0 THEN
RAISE_APPLICATION_ERROR (-20001, 'Please select a model of e-mail to change!');
END IF;
IF APEX_APPLICATION. G_F01. COUNT > 1 THEN
RAISE_APPLICATION_ERROR (-20001, 'Please select a model unique to change both message!');
END IF;
BECAUSE me in 1.APEX_APPLICATION. G_F01. Count
LOOP
l_vc_arr2: = APEX_UTIL. STRING_TO_TABLE (APEX_APPLICATION. G_F01 (i),' $');
v (P32_SUBJECT): = l_vc_arr2 (1);
v (P32_TYPE): = l_vc_arr2 (2);
v (P32_BODY): = l_vc_arr2 (3);
END LOOP;
end;
end;
but it is not compiling. Someone knows what's the problem?
With respect,
Sunil BhatiaHi Sunil,
You cannot use the function v ('P32_BODY') to assign values.
you will need to use:
apex_util.set_session_state ('P32_BODY', l_vc_arr2 (3));Concerning
Michael
-
some of you that it won't be easy, but for a rookie plsql is not form. Can U help?
want to find out who has max (salary) and record that information in a variable
Table: used
Select max (salary) in employee == > back 2500id salary 1 2000 2 2500 3 1800
But how to write this in a plsql and find the id is 3 and save it in a variable
my incomplete statement:
Select max (salary), in myvar to employee where id =?
can you help me?Oh and save it in a variable plsql:
declare my_var emp.empno%TYPE; begin select e1.empno into my_var from emp e1 where e1.sal = (select max(e2.sal) from emp e2) and rownum = 1; end; /
-
When I run this query, it will be 1 hour to run it. can we change this query to do this?
SELECT /*+ CHOOSE */a.*, b.*
FROM (SELECT ( TO_DATE (ucraudt_new_value, 'DD-MON-YYYY')
- TO_DATE (ucraudt_old_value, 'DD-MON-YYYY')
) AS diff,
ucraudt.*
FROM ucraudt
WHERE ucraudt_column_code = '1GED'
AND ucraudt_new_value IS NOT NULL
AND ucraudt_old_value IS NOT NULL
AND ucraudt_user_id LIKE 'UCPVCRV%') a,
ucrserv b
WHERE a.diff > 74
AND a.ucraudt_cust_code = b.ucrserv_cust_code
AND a.ucraudt_prem_code = b.ucrserv_prem_code
AND b.ucrserv_status_ind = 'A'Try following
SELECT a.*, b.* FROM ucraudt a, ucrserv b WHERE (TO_DATE (a.ucraudt_new_value, 'DD-MON-YYYY') - TO_DATE (a.ucraudt_old_value, 'DD-MON-YYYY')) > 74 AND a.ucraudt_cust_code = b.ucrserv_cust_code AND a.ucraudt_prem_code = b.ucrserv_prem_code AND b.ucrserv_status_ind = 'A' and a.ucraudt_column_code = '1GED' AND a.ucraudt_new_value IS NOT NULL AND a.ucraudt_old_value IS NOT NULL AND a.ucraudt_user_id LIKE 'UCPVCRV%'
Also post the query execution plan
-
How to solve this query in database using sqlite phone gap
am facing a problem in recent days. The problem is database. In fact, I create the mane database 'casepad '. Then I created a table because the database 'table of cases' have (ID, name, date). Now I insert the value on this table. When inserting I also create a name ("casename") .mean table if I insert the value of the case table (1, 'AB', 2/13). Then I create table AB. Now I need to get the value of the case table (I am) but I need to count the number of items in another table (AB). Here, I had to try it.
function onDeviceReady() { db = window.openDatabase("Casepad", "1.0", "Casepad", 200000); db.transaction(getallTableData, errorCB); } function insertData() { db.transaction(createTable, errorCB, afterSuccessTableCreation); } //createtableandinsertsome record function createTable(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS CaseTable (id INTEGER PRIMARY KEY AUTOINCREMENT, CaseName TEXT unique NOT NULL ,CaseDate INTEGER ,TextArea TEXT NOT NULL)'); tx.executeSql('INSERT OR IGNORE INTO CaseTable(CaseName,CaseDate,TextArea) VALUES ("' + $('.caseName_h').val() + '", "' + $('.caseDate_h').val() + '","' + $('.caseTextArea_h').val() + '")'); } //function will be called when an error occurred function errorCB(err) { navigator.notification.alert("Error processing SQL: " + err.code); } //function will be called when process succeed function afterSuccessTableCreation() { console.log("success!"); db.transaction(getallTableData, errorCB); } //select all from SoccerPlayer function getallTableData(tx) { tx.executeSql('SELECT * FROM CaseTable', [], querySuccess, errorCB); } function querySuccess(tx, result) { var len = result.rows.length; var t; $('#folderData').empty(); for (var i = 0; i < len; i++) { $('#folderData').append( '
- ' + '' + '' + '
' + result.rows.item(i).CaseName + t+'
' + '' + result.rows.item(i).TextArea + '
' + '' + result.rows.item(i).CaseDate + '
' + '' + i + '' + ''+' '
);
}
$('#folderData').listview('refresh');
}
-
[8i] not to simplify this query?
In my application, I need to pick up three possible prices for each part number in an array.
The three possible prices that I need to recover are:
(1) the price paid on the order more recently closed,
(2) the price mentioned on the first pending order, and
(3) the price on the furthest on pending order.
However, there is a complication in that. Orders include the part number, but may also include a prefix of 4 characters (always the same) on a reference number which should be treated the same as the reference database. For example, "DSB-part1" should be considered "part1", "DSB-part2" should be regarded as 'part 2', etc.
In addition, it is quite possible for several orders expected or closed on the same day, and I want to only return the price of one of these lines for each of the 3 methods of pricing.
(Technically, there is another level of complication to this, but if I have problems with it later, I'll create a new job for him...)
Some examples of data:
And here's my ugly query to get the results I want. It can be simplified?CREATE TABLE part ( part_no VARCHAR2(9) NOT NULL, part_desc VARCHAR2(25), qty_instock NUMBER, CONSTRAINT part_pk PRIMARY KEY (part_no) ); INSERT INTO part VALUES ('part1 ','description 1 here',5); INSERT INTO part VALUES ('part2 ','description 2 here',10); INSERT INTO part VALUES ('part3 ','description 3 here',0); CREATE TABLE ords ( ord_no NUMBER NOT NULL, ord_part_no VARCHAR2(9), date_closed DATE, orig_dock_date DATE, date_due_instock DATE, unit_price NUMBER, qty_order NUMBER, ord_stat VARCHAR2(2), CONSTRAINT ords_pk PRIMARY KEY (ord_no) ); INSERT INTO ords VALUES (1,'part1 ',To_Date('01/01/2009','mm/dd/yyyy'),To_Date('12/01/2008','mm/dd/yyyy'),To_Date('12/15/2008','mm/dd/yyyy'),100,10,'CL'); INSERT INTO ords VALUES (2,'part1 ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/01/2009','mm/dd/yyyy'),To_Date('01/05/2009','mm/dd/yyyy'),105,15,'CL'); INSERT INTO ords VALUES (3,'part1 ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),100,20,'CL'); INSERT INTO ords VALUES (4,'part1 ',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/25/2009','mm/dd/yyyy'),103,10,'OP'); INSERT INTO ords VALUES (5,'ORD-part1',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),To_Date('01/31/2009','mm/dd/yyyy'),101,10,'OP'); INSERT INTO ords VALUES (6,'ORD-part2',To_Date('01/01/2009','mm/dd/yyyy'),To_Date('12/01/2008','mm/dd/yyyy'),To_Date('12/15/2008','mm/dd/yyyy'),100,10,'CL'); INSERT INTO ords VALUES (7,'part2 ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/01/2009','mm/dd/yyyy'),To_Date('01/05/2009','mm/dd/yyyy'),105,15,'CL'); INSERT INTO ords VALUES (8,'ORD-part2',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),100,20,'CL'); INSERT INTO ords VALUES (9,'part2 ',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/25/2009','mm/dd/yyyy'),103,10,'OP'); INSERT INTO ords VALUES (10,'ORD-part2',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),To_Date('01/31/2009','mm/dd/yyyy'),101,10,'OP'); INSERT INTO ords VALUES (11,'part3 ',To_Date('01/01/2009','mm/dd/yyyy'),To_Date('12/01/2008','mm/dd/yyyy'),To_Date('12/15/2008','mm/dd/yyyy'),100,10,'CL'); INSERT INTO ords VALUES (12,'part3 ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/01/2009','mm/dd/yyyy'),To_Date('01/05/2009','mm/dd/yyyy'),105,15,'CL'); INSERT INTO ords VALUES (13,'ORD-part3',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),100,20,'CL'); INSERT INTO ords VALUES (14,'ORD-part3',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/25/2009','mm/dd/yyyy'),103,10,'OP'); INSERT INTO ords VALUES (15,'part3 ',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),To_Date('01/31/2009','mm/dd/yyyy'),101,10,'OP');
And here are the results, I expect to get, according to data from the sample:SELECT p.part_no , p.part_desc , p.qty_instock , a2.unit_price AS last_closed_price , a2.qty_order AS last_closed_qty , a2.date_closed AS last_closed_date , b2.unit_price AS first_open_price , b2.qty_order AS first_open_qty , b2.date_due_instock AS first_open_date , c2.unit_price AS last_open_date , c2.qty_order AS last_open_qty , c2.date_due_instock AS last_open_date FROM part p , ( SELECT lc.part_no , lc.unit_price , lc.qty_order , lc.date_closed , ROW_NUMBER() OVER(PARTITION BY lc.part_no ORDER BY lc.orig_dock_date DESC) AS rnk_nbr FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_closed , o.orig_dock_date , o.unit_price , o.qty_order FROM ords o ) lc --for last closed , ( SELECT a.part_no , MAX(a.date_closed) AS last_closed_date FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_closed FROM ords o ) a GROUP BY a.part_no ) a1 WHERE lc.part_no = a1.part_no AND lc.date_closed = a1.last_closed_date ) a2 , ( SELECT fo.part_no , fo.unit_price , fo.qty_order , fo.date_due_instock , ROW_NUMBER() OVER(PARTITION BY fo.part_no ORDER BY fo.orig_dock_date DESC) AS rnk_nbr FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_due_instock , o.orig_dock_date , o.unit_price , o.qty_order FROM ords o WHERE o.ord_stat = 'OP' ) fo --for first open , ( SELECT b.part_no , MIN(b.date_due_instock) AS first_open_date FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_due_instock FROM ords o WHERE o.ord_stat = 'OP' ) b GROUP BY b.part_no ) b1 WHERE fo.part_no = b1.part_no AND fo.date_due_instock = b1.first_open_date ) b2 , ( SELECT lo.part_no , lo.unit_price , lo.qty_order , lo.date_due_instock , ROW_NUMBER() OVER(PARTITION BY lo.part_no ORDER BY lo.orig_dock_date DESC) AS rnk_nbr FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_due_instock , o.orig_dock_date , o.unit_price , o.qty_order FROM ords o WHERE o.ord_stat = 'OP' ) lo --for last open , ( SELECT c.part_no , MAX(c.date_due_instock) AS last_open_date FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_due_instock FROM ords o WHERE o.ord_stat = 'OP' ) c GROUP BY c.part_no ) c1 WHERE lo.part_no = c1.part_no --EDIT: changed from l1 to c1 AND lo.date_due_instock = c1.last_open_date --EDIT: changed from l1 to c1 ) c2 WHERE p.part_no = a2.part_no AND a2.part_no = b2.part_no AND b2.part_no = c2.part_no AND a2.rnk_nbr = 1 AND b2.rnk_nbr = 1 AND c2.rnk_nbr = 1
Published by: user11033437 on February 5, 2010 08:48. LAST_ LAST_ LAST_ FIRST_ FIRST_ FIRST_ LAST_ LAST_ LAST_ . QTY_ CLOSED_ CLOSED_ CLOSED_ OPEN_ OPEN_ OPEN_ OPEN_ OPEN_ OPEN_ PART_NO PART_DESC INSTOCK PRICE QTY DATE PRICE QTY DATE PRICE QTY_ DATE_ --------------------------------------------------------------------------------------------------------------------------------- part1 description 1 here 5 100 20 1/31/2009 103 10 1/25/2009 101 10 1/31/2009 part2 description 2 here 10 100 20 1/31/2009 103 10 1/25/2009 101 10 1/31/2009 part3 description 3 here 0 100 20 1/31/2009 103 10 1/25/2009 101 10 1/31/2009
Correction of errors (see the comments in the request above for changes)Hello
It is a little bit shorter than what you have posted, probably more effective and (in my opinion) much easier to debug and maintain:
SELECT p.part_no , p.part_desc , p.qty_instock , o.last_closed_price , o.last_closed_qty , o.last_closed_date , o.first_open_price , o.first_open_qty , o.first_open_date , o.last_open_price , o.last_open_qty , o.last_open_date FROM part p , ( -- Begin in-line view o for pivoted order data SELECT part_no , MAX (CASE WHEN lc_num = 1 THEN unit_price END) AS last_closed_price , MAX (CASE WHEN lc_num = 1 THEN qty_order END) AS last_closed_qty , MAX (CASE WHEN lc_num = 1 THEN date_closed END) AS last_closed_date , MAX (CASE WHEN fo_num = 1 AND ord_stat = 'OP' THEN unit_price END) AS first_open_price , MAX (CASE WHEN fo_num = 1 AND ord_stat = 'OP' THEN qty_order END) AS first_open_qty , MAX (CASE WHEN fo_num = 1 AND ord_stat = 'OP' THEN date_due_instock END) AS first_open_date , MAX (CASE WHEN lo_num = 1 AND ord_stat = 'OP' THEN unit_price END) AS last_open_price , MAX (CASE WHEN lo_num = 1 AND ord_stat = 'OP' THEN qty_order END) AS last_open_qty , MAX (CASE WHEN lo_num = 1 AND ord_stat = 'OP' THEN date_due_instock END) AS last_open_date FROM ( -- Begin in-line view to get lc_, fo_, lo_num SELECT gpo.* , ROW_NUMBER () OVER ( PARTITION BY part_no ORDER BY date_closed DESC , ord_no DESC ) AS lc_num , ROW_NUMBER () OVER ( PARTITION BY part_no ORDER BY CASE WHEN ord_stat = 'OP' THEN 1 ELSE 2 END , orig_dock_date , ord_no ) AS fo_num , ROW_NUMBER () OVER ( PARTITION BY part_no ORDER BY CASE WHEN ord_stat = 'OP' THEN 1 ELSE 2 END , orig_dock_date DESC , ord_no DESC ) AS lo_num FROM ( -- Begin in-line view gpo to get part_no SELECT ords.* , RTRIM ( CASE WHEN ord_part_no LIKE 'ORD-%' THEN SUBSTR (ord_part_no, 5) ELSE ord_part_no END ) AS part_no FROM ords ) gpo -- End in-line view gpo to get part_no ) -- End in-line view to get lc_, fo_, lo_num GROUP BY part_no ) o -- End in-line view o for pivoted order data WHERE RTRIM (p.part_no) = o.part_no ORDER BY p.part_no ;
Whenever you want to have a WHERE clause only applies to certain columns, think about a pivot and expression BOX.
Is there a reason to have raw triling part_no or ord_part_no?
Published by: Frank Kulash, February 5, 2010 13:26
After this announcement, I saw that Max had posted essentially the same query.
In general, where the solution of Max seems more simple, I like her way better.
The only possible exception is tested for "OP" at the derivation of the values first_open and last_open. If none of the lines to an ord_stat part_no = "OP", solution of Max will be used a the lines with another ord_stat. That does not occur in the sample data, and I don't know if this is still possible in your application, but if this is the case, I don't know this isn't what you want. -
How do to this chain operation looks simple but causing a lot of time to do :)
Hi all
I have a very large chain, i.e. the soap xml response send by server that I read once connected to the server using http, now the problem is that I need to create a Document out of it and analyze what is simple but the answer I get server requires many replacements of string as example 1) replacing "<" with "<" 2)="" replacing="" ">"="" with="" "="">" etc before creating a document out of it now there are many such replacements in the sending of the response from server that takes a lot of time. 5 minutes on the emulator. So my question is how do? Is there a native method to replaceAll occurrences of a substring in a string or while I can use or is there another good way? Please let me know thx.
Thank you
Sagar
We took over a project done by someone else, and they were doing exactly what you do. Here is their source, which I think is more effective that you give it a try if you want.
But be aware that I have never used or tested this code.
I guess you pull that string to the center of your SOAP response. It has been encoded XML. So you can do what we did in this case and wrap the string as a data element in an XML element and then use the XML parser to get the actual data, which feed us an XML parser again to get the actual data. XML is already set up to deal with these characters, so it was more effective than the approach to replace. Actually to be honest, it wasn't the reason we exchanged, that as part of the parser XML meant we were convinced it was done correctly.
public String replaceAll(String source, String pattern, String replace) { if ((source != null) && (pattern.length() > 0)) { final int len = pattern.length(); StringBuffer sb = new StringBuffer(); int found = -1; int start = 0; while ((found = source.indexOf(pattern, start)) != -1) { sb.append(source.substring(start, found)); sb.append(replace); start = found + len; } sb.append(source.substring(start)); return sb.toString(); } else return ""; }
" > -
How to optimize this query XMLDB
Hello dear community, we have two XMLType table they are very similar but not identical and we do not have the XSD for validation for this exercise.
We need to make a join between these tables and the data code example go like that
create table xmltst ( xmldata xmltype); create table xmltst2 ( xmldata xmltype); declare idata varchar2(4000); idata2 varchar2(4000); begin idata := '<?xml version="1.0" encoding="UTF-8"?> <SWs> <SW s_ID="T6B890.00-01" t_ID="T6B890.00"> <Ds> <De sX="59" sY="-57" rX="7" rY="22" m_ID="L" eTime_s="2014-12-12T02:22:11+08:00" eTime_e="2014-12-12T02:22:42+08:00" mst="0.631"/> <De sX="70" sY="-57" rX="7" rY="23" m_ID="L" eTime_s="2014-12-12T02:22:12+08:00" eTime_e="2014-12-12T02:22:33+08:00" mst="0.217"/> <De sX="69" sY="-57" rX="47" rY="1" m_ID="R" eTime_s="2014-12-12T02:22:16+08:00" eTime_e="2014-12-12T02:22:56+08:00" mst="0.974"/> </Ds> </SW> <SW s_ID="T6B890.00-02" t_ID="T6B890.00"> <Ds> <De sX="56" sY="-1" rX="72" rY="19" m_ID="R" eTime_s="2014-12-12T02:36:01+08:00" eTime_e="2014-12-12T02:36:29+08:00" mst="0.541"/> <De sX="57" sY="-1" rX="39" rY="42" m_ID="L" eTime_s="2014-12-12T02:22:12+08:00" eTime_e="2014-12-12T02:23:01+08:00" mst="0.426"/> <De sX="58" sY="-1" rX="72" rY="20" m_ID="R" eTime_s="2014-12-12T02:36:07+08:00" eTime_e="2014-12-12T02:36:18+08:00" mst="0.716"/> </Ds> </SW> </SWs>'; idata2 := '<?xml version="1.0" encoding="UTF-8"?> <SWs> <SW s_ID="T6B890.00-01" t_ID="T6B890.00"> <Ds> <De sX="59" sY="-57" rX="7" rY="22" m_ID="L" eTime_s="2014-12-12T02:22:11+08:00" eTime_e="2014-12-12T02:22:42+08:00"/> <De sX="70" sY="-57" rX="7" rY="23" m_ID="L" eTime_s="2014-12-12T02:22:12+08:00" eTime_e="2014-12-12T02:22:33+08:00"/> <De sX="69" sY="-57" rX="47" rY="1" m_ID="R" eTime_s="2014-12-12T02:22:16+08:00" eTime_e="2014-12-12T02:22:56+08:00"/> <De sX="72" sY="-57" rX="47" rY="2" armID="R" eTime_s="2014-12-12T02:22:18+08:00" eTime_e="2014-12-12T02:23:28+08:00"/> <De sX="82" sY="-57" rX="7" rY="25" armID="L" eTime_s="2014-12-12T02:22:19+08:00" eTime_e="2014-12-12T02:22:58+08:00"/> </Ds> </SW> <SW s_ID="T6B890.00-02" t_ID="T6B890.00"> <Ds> <De sX="56" sY="-1" rX="72" rY="19" m_ID="R" eTime_s="2014-12-12T02:36:01+08:00" eTime_e="2014-12-12T02:36:29+08:00"/> <De sX="57" sY="-1" rX="39" rY="42" m_ID="L" eTime_s="2014-12-12T02:22:12+08:00" eTime_e="2014-12-12T02:23:01+08:00"/> <De sX="58" sY="-1" rX="72" rY="20" m_ID="R" eTime_s="2014-12-12T02:36:07+08:00" eTime_e="2014-12-12T02:36:18+08:00"/> </Ds> </SW> </SWs>'; insert into xmltst values (idata); insert into xmltst2 values (idata2); end; commit;
The SQL code, we try to optimize:
with tt as ( SELECT /*+ materialize */ x.* FROM xmltst t, XMLTABLE ('/SWs/SW[@s_ID="T6B890.00-01"]/Ds/De' PASSING t.xmldata COLUMNS sX number PATH '@sX', sY number PATH '@sY', rX number PATH '@rX', rY number PATH '@rY', eTime_s varchar2(30) PATH '@eTime_s', eTime_e varchar2(30) PATH '@eTime_e', mst number PATH '@mst' ) x ) ,tt2 as ( SELECT /*+ materialize */ x.* FROM xmltst2 t, XMLTABLE ('/SWs/SW[@s_ID="T6B890.00-01"]/Ds/De' PASSING t.xmldata COLUMNS sX number PATH '@sX', sY number PATH '@sY', rX number PATH '@rX', rY number PATH '@rY', eTime_s varchar2(30) PATH '@eTime_s', eTime_e varchar2(30) PATH '@eTime_e' ) x ) select tt2.*,tt.mst from tt2 left outer join tt on (tt2.sX = tt.sX and tt2.sY = tt.sY and tt2.rX = tt.rX and tt.rY=tt.rY)
CREATE INDEX xmltst_idx ON xmltst (xmldata) INDEXTYPE IS XDB.XMLIndex PARAMETERS ( 'XMLTable SW_tab ''/SWs/Sw'' COLUMNS s_ID VARCHAR2(100) PATH ''@s_ID'', sX NUMBER PATH ''Ds/De/@sX'', sY NUMBER PATH ''Ds/De/@sY'', rX NUMBER PATH ''Ds/De/@rX'', rY NUMBER PATH ''Ds/De/@rY''');
Create an index as above, but it does not seem to be used to explain the plan for the part of XML query.
and a lot of time, I also get this error but I cannot now re - produce for some reason.
I thought that its because I can't index after branch out according to s_ID
SQL Error: ORA-29879: cannot create multiple domain indexes on a column list using same indextype 29879. 00000 - "cannot create multiple domain indexes on a column list using same indextype" *Cause: An attempt was made to define multiple domain indexes on the same column list using identical indextypes. *Action: Check to see if a different indextype can be used or if the index can be defined on another column list.
and this index below seems to have choice as shown explain plan.
Why can't I see the index above in the plan to explain it?
CREATE INDEX "OE"."XMLTST_INDX01" ON "OE"."XMLTST" ("XMLDATA") INDEXTYPE IS "XDB"."XMLINDEX" PARAMETERS ('paths (include (/SWs/SW/@s_ID))');
However, it is still the loop nest join when the join of two tables after the XML in the process... Is it possible to tell Oracle to a join index or some kind of faster join after the XML select part.
My real case got way as many lines to make the join of X - Y and it may be nice to have an index to quickly reach?
When do some small tests, the clause will eventually cause oracle core dump. It should not happen even it is a virtual machine with 3G of memory max and the max_memory_target = 800 M as all my data are not not even 50 M.
We are the team of analysts and Dev team suggest that is a little too much time to contact Oracle Support and I finally create 3 global temporary table with commit preserve rows and operate with performance much better.
Instend of show the value of 'i' in the view list, I need to show how many element in the table. I have to call synchronize because I call a query that counts the number of elements to 'result.rows.item (i). CaseName"this item... ?
Hello
If I understand correctly, you are looking for a SQL statement that you will get the count for the number of items in the table AB? If so, this can be useful:
http://www.w3schools.com/SQL/sql_func_count.asp
Please let me know if you have any other questions.
Maybe you are looking for
-
Is it possible to Airplay of an iPad or iPhone to an iMac?
-
Why does take so long to clear a task (with the DMA transfer) using the PXI bus?
I am migrating from a system that has the habit of using a PCI-6133 (8 channels HAVE simultaneous) and PCI-6733 (8 channels AO) to one that uses a PXI-6133 and PXI-6733 mounted in a PXI-1033 chassis. In general, things seem to be working (synchronize
-
Just bought a refurbished 8000 Elite, specifications: Intel Core 2 Quad 2.83 GHz 8 GB Ram HARD DRIVE 750 GB Optical drive DVD - RW Win 7 Professional 64 bit Restore CD, apparently no recovery on the HARD drive partition 1 year distributor warranty pa
-
dvd tray opens... car needs
When I try to open the drawer of the dvd it does not work so I'll my programs... one is called roxio and try to eject the tray... the message on the screen saying needs drive - can someone help me with this
-
How do you uninstall the narrator in Windows 7
How do you uninstall the narrator in Windows 7