Select with transposition and join
Hi Guys!
I'm using Oracle 9i and I'm facing the following problem:
My table looks like this:
Stripe
Unit
Value
1
1
a1
1
2
a2
1
3
a3
2
1
b1
2
2
b2
2
3
b3
2
4
b4
4
1
c1
4
2
c2
4
3
c3
4
4
c4
4
5
c5
My result should look like this:
Unit
Stripe1 Value
Stripe2 Value
Stripe3 Value
Stripe4 Value
1
a1
b1
c1
2
a2
b2
c2
3
a3
b3
c3
4
b4
c4
5
c5
I tried it with one select for each stripe and full joins, but in this example I would only see the first 3 units....
Any ideas?
Another way, but only if your on database version 11gR1 and onwards, is to use the PIVOT operator:
SQL> -- generating sample date:
SQL> with t as (
2 select 1 stripe, 1 unit, 'a1' value from dual union
3 select 1, 2, 'a2' from dual union
4 select 1, 3, 'a3' from dual union
5 select 2, 1, 'b1' from dual union
6 select 2, 2, 'b2' from dual union
7 select 2, 3, 'b3' from dual union
8 select 2, 4, 'b4' from dual union
9 select 4, 1, 'c1' from dual union
10 select 4, 2, 'c2' from dual union
11 select 4, 3, 'c3' from dual union
12 select 4, 4, 'c4' from dual union
13 select 4, 5, 'c5' from dual
14 )
15 --
16 -- actual query:
17 --
18 select *
19 from ( select unit
20 , stripe
21 , value
22 from t
23 )
24 pivot (max(value) for (stripe) in ( 1 as stripe1value
25 , 2 as stripe2value
26 , 3 as stripe3value
27 , 4 as stripe4value
28 )
29 )
30 order by unit;
UNIT ST ST ST ST
1 a1 b1 c1
2 a2 b2 c2
3 a3 b3 c3
4 b4 c4
5 c5
5 rows selected.
ORACLE-BASE - PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1
Similar Messages
-
Hi
I want to create a app redirecting to my mebsite.This app permit to select a "area" selected on a UICollectionViewController, and an "event" selected with a UITableViewController.
I need to integer both informations on the FinalViewController (called in my app AnnoncesViewController) =>
The fisrst array is selected on CollectionViewController:@"http://carnet-du-jour.com/index.php/component/ohanah/?option=com_ohanah&view=e vents&Itemid=0&ohanah_venue_id=21"
The second array is selected onUITableViewController:@"&ohanah_category_id=1&textToSearch="
The final array must be:http://carnet-du-jour.com/index.php/component/ohanah/?option=com_ohanah&view=eve nts&Itemid=0&ohanah_venue_id=21&ohanah_category_id=1&textToSearch=
How can I integer these 2 NSArray to obtain this result to link to a UIWebView as FinalViewController?
Thank you for your help!!
My App is on Drobox:https://www.dropbox.com/s/43l1p2hj5bqamvn/Carnet%20du%20Jour.zip
Gracefully
RémySpehlerHi
I want to create a app redirecting to my mebsite.This app permit to select a "area" selected on a UICollectionViewController, and an "event" selected with a UITableViewController.
I need to integer both informations on the FinalViewController (called in my app AnnoncesViewController) =>
The fisrst array is selected on CollectionViewController:@"http://carnet-du-jour.com/index.php/component/ohanah/?option=com_ohanah&view=e vents&Itemid=0&ohanah_venue_id=21"
The second array is selected onUITableViewController:@"&ohanah_category_id=1&textToSearch="
The final array must be:http://carnet-du-jour.com/index.php/component/ohanah/?option=com_ohanah&view=eve nts&Itemid=0&ohanah_venue_id=21&ohanah_category_id=1&textToSearch=
How can I integer these 2 NSArray to obtain this result to link to a UIWebView as FinalViewController?
Thank you for your help!!
My App is on Drobox:https://www.dropbox.com/s/43l1p2hj5bqamvn/Carnet%20du%20Jour.zip
Gracefully
RémySpehler -
SELECT, hierarchical queries and JOIN
Hi everyone,
I have a small SELECT statement but I can't find an easy solution.
Look at this situation:
drop table departments;
CREATE TABLE departments
dpt_id NUMBER(10) UNIQUE,
dpt_name VARCHAR2(100),
dpt_parent_id NUMBER(10)
TRUNCATE table departments;
INSERT INTO departments VALUES(1, 'Company', null);
INSERT INTO departments VALUES(2, 'HR', 1);
INSERT INTO departments VALUES(3, 'SALES', 1);
INSERT INTO departments VALUES(4, 'IT', 1);
INSERT INTO departments VALUES(222, 'Helpdesk', 4);
INSERT INTO departments VALUES(223, 'French Speaking', 222);
INSERT INTO departments VALUES(224, 'Another level', 223);
INSERT INTO departments VALUES(5, 'LEGAL', 1);
INSERT INTO departments VALUES(66, 'Recruitment', 2);
INSERT INTO departments VALUES(33, 'Logistics', 2);
INSERT INTO departments VALUES(39, 'Fleet management', 33);
INSERT INTO departments VALUES(31, 'Local Sales', 3);
INSERT INTO departments VALUES(60, 'European Sales', 3);
INSERT INTO departments VALUES(61, 'Germany', 60);
INSERT INTO departments VALUES(62, 'France', 60);
INSERT INTO departments VALUES(620, 'Paris', 62);
INSERT INTO departments VALUES(621, 'Marseilles', 62);
INSERT INTO departments VALUES(38, 'American Sales', 3);
INSERT INTO departments VALUES(34, 'Asian Sales', 3);
CREATE table persons
person_id NUMBER(10) UNIQUE,
person_name VARCHAR2(100),
person_dpt_id NUMBER(10)
truncate table persons;
INSERT INTO persons VALUES(1, 'Jim', 2);
INSERT INTO persons VALUES(2, 'Jack', 621);
INSERT INTO persons VALUES(3, 'John', 620);
INSERT INTO persons VALUES(4, 'John', 224);
INSERT INTO persons VALUES(5, 'Fred', 61);It's a simple hierachy like the one we can find in HR schema. The link between an department and its parent is with parent id. THe following statement build the whole tree:
SELECT dpt_id, level, LPAD(' ', LEVEL-1)|| dpt_name
FROM departments
START WITH dpt_parent_id IS NULL
CONNECT BY dpt_parent_id = PRIOR dpt_id;As you can see in the script above, I have a few people assigned to these departments. It's also a classic situtation...
I would like to have something like this:
WITH temp AS
SELECT dpt_id, dpt_name, SYS_CONNECT_BY_PATH(dpt_name, '#') as full_path
FROM departments
START WITH dpt_parent_id IS NULL
CONNECT BY dpt_parent_id = PRIOR dpt_id
SELECT p.person_name, d.dpt_name, --d.full_path,
regexp_substr(d.full_path, '[^#]+', 1, 2, 'i') as t1,
regexp_substr(d.full_path, '[^#]+', 1, 3, 'i') as t2,
regexp_substr(d.full_path, '[^#]+', 1, 4, 'i') as t3,
regexp_substr(d.full_path, '[^#]+', 1, 5, 'i') as t4
FROM persons p
JOIN temp d ON d.dpt_id = p.person_dpt_id;This is the exact output I want, but I wonder... Is it possible to do it without the factored sub-query? It's nice and works fine but I had to precompute the whole path to split it again. I mean, this should be possible in one step. Any suggestion?
I'm using Oracle 10g
Thanks,Hi,
user13117585 wrote:
... But sometimes, I just find the statements difficult for what they do. For example, my previous one. I have a person, and I want to see his department and the path in the tree.Actually, you want more than that; you want to parse the path, and display each #-delimited part in a separate column. If you didn't want that, then you could do away with the 4 REGEXP_SUBSTR calls, like this:
WITH temp AS
SELECT dpt_id, dpt_name
, SUBSTR ( REPLACE ( SYS_CONNECT_BY_PATH ( RPAD (dpt_name, 15) -- Using 15 just for demo
, 16
) as full_path
FROM departments
START WITH dpt_parent_id IS NULL
CONNECT BY dpt_parent_id = PRIOR dpt_id
SELECT p.person_name, d.dpt_name, d.full_path
FROM persons p
JOIN temp d ON d.dpt_id = p.person_dpt_id;Output:
PERSON_N DPT_NAME FULL_PATH
Jim HR HR
Fred Germany SALES European Sales Germany
John Paris SALES European Sales France Paris
Jack Marseilles SALES European Sales France Marseilles
John Another level IT Helpdesk French SpeakingAnother levelAs you can see, full_path is one giant column, but it's formatted to look like 4 separate columns, forresponding to your original t1, t2, t3 and t4. I limited the output to 15 characters, just for debugging and posting purposes. You can use any number of characters you like.
It's too complex for this simple thing.It would be nice if there was something simpler that did exactly what you wanted, but I'm not sure it's reasonable to expect it in every case. I asked a lot of questions in my first message, but I'm not sure you've tried to answer any of them, so I'm not sure why you're unhappy with the query you posted. I can think of lots of ways to change the query, but I have no way of telling if you would like them any better than what you already have.
And hopefully, I know where to start in the hierarchy and I know where to stop. If I had to show all the levels and have one column by level dynamically, I'd be stuck. Sorry, I don't understand this part.
Are you saying that it seems inefficient to generate the entire tree, when perhaps few of the nodes will have have matches in the persons table? If so, you can invert the whole query. Instead of doing the CONNECT BY first and then joining, do the join first and then the CONNECT BY. Instead of doing a top-down CONNECT BY, where you start with the parentless nodes (whether or not you'll ultimately need them) and then find their descendants, do a bottom-up CONNECT BY, where you start with the nodes you know you'll need, and then find their ancestors.
I just find it difficult for such a simple need. Again, there are lots of things that could be done. If you won't say what you want, that makes it hard for me to tell you how to get it. All that I've picked up for sure is that you don't like doing a sub-query. That's unfortunate, because sub-queries are so basic. They have very important been since Oracle 8.1, and they don't seem to be going away. Quite the opposite, in fact. You need sub-queries for all kinds of things, not just CONNECT BY. To give just a couple of examples, they're the only thing that make analytic functions really useful, and they simplfy chasm traps (basically, multiple 1-to-many relationships on the same table) considerably. I'm sorry if you don't lke sub-queries, but I don't see how you can work in this field and not use them.
Edited by: Frank Kulash on Nov 15, 2011 3:18 PM
Revised query -
UPDATE Statement with subquerry and join
I have 2 tables (say, employee and dept). employee.empno and dept.empno.
These tables are not joined.
I want to update the first name (fname) in employee WHERE employee.empno = dept.empno
My query is :
UPDATE emp SET fname='jack' WHERE exists(select * from emp,dept where emp.empno = dept.empno)
But this query updates all the records.... even where there is no corresponding record in dept
For example, emp has empno values of 10,20,30,40 and dept has empno values of 10 and 20 only,
yet all records in emp are updated.
Can someone help me ???
nullThat is because an UPDATE statemente without a WHERE clause will update all of the rows.
There are three parts to an UPDATE
UPDATE tablename
SET columnlist
WHERE
The UPDATE clause specifies the table to update.
The SET clause specifies the columns to update and the values to use to update them.
The WHERE clause specified which records in the table to update.
No WHERE clause means update all rows.
The correlated subquery in the SET clause has nothing to do with the WHERE clause that determines which rows get updated. You have to be very careful to also include a WHERE clause (that often duplicates the WHERE clause in the subquery) or you will set columns to NULL that don't get values returned from the subquery.
Alias the emp table being updated and do not include the emp table in the subquery.
Also I suggest you use 'X' (or any constant that is not actually in the table) in the EXISTS test.
UPDATE emp e SET fname='jack' WHERE exists(select 'x' from dept where e.empno = dept.empno)
WHERE exists (select 'x' from dept where e.empno = dept.empno -
Where clause with XMLExists and join on another table
Hi,
We have table like:
drop table xml_tbl;
create table xml_tbl (
xml_msg_id integer,
xml_msg_text xmltype
insert into xml_tbl values
(1, '<main><id>1</id></main>') ;
insert into xml_tbl values --(xml_msg_id,xml_msg_text)
(1, '<main><id>2</id></main>') ;
Another table like:
create Table Table1
( id1 int);
Insert into Table1 values(2);
Insert into Table1 values(3);
We need to have a view on top of the table xml_tbl where /main/id should have only those values which are in id1 column of table Table1.
Something like
CREATE OR REPLACE VIEW V_xml_tbl
xml_msg_text
AS
SELECT T.xml_msg_text
FROM xml_tbl T
WHERE XMLEXISTS (
'declare namespace Namesp1 ="Abc:Set";
let $Results as xs:boolean := fn:exists($p/main/id in (Select id1 from Table1)) --Now here I know I can't do Select id1 from
Table1*
return if ($Results ) then true() else ()'
PASSING T.xml_msg_text AS "p");
Actually in the real scenario Table1 will have many IDs and xml_tbl has many XML files..
So I am stuck on how to do it. Please help.
Thanks..
Edited by: user8941550 on Nov 20, 2012 7:19 PMOne of these two :
SQL> select t.xml_msg_text
2 from xml_tbl t
3 where exists (
4 select null
5 from table1 t1
6 where t1.id1 = xmlcast(
7 xmlquery('/main/id' passing t.xml_msg_text returning content)
8 as integer
9 )
10 );
XML_MSG_TEXT
<main>
<id>2</id>
</main>
SQL> select t.xml_msg_text
2 from xml_tbl t
3 , xmltable('/main' passing t.xml_msg_text
4 columns id integer path 'id'
5 ) x
6 where exists (
7 select null
8 from table1 t1
9 where t1.id1 = x.id
10 );
XML_MSG_TEXT
<main>
<id>2</id>
</main>
And a third one, using XMLExists :
SQL> select t.xml_msg_text
2 from xml_tbl t
3 where xmlexists (
4 'fn:collection("oradb:/DEV/TABLE1")/ROW[ID1=$d/main/id]'
5 passing t.xml_msg_text as "d"
6 );
XML_MSG_TEXT
<main>
<id>2</id>
</main>
Execution Plan
Plan hash value: 3633580934
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 2 | 116 | 8 (0)| 00:00:01 |
|* 1 | FILTER | | | | | |
| 2 | TABLE ACCESS FULL | XML_TBL | 2 | 116 | 3 (0)| 00:00:01 |
| 3 | NESTED LOOPS | | 1 | 5 | 5 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| TABLE1 | 2 | 6 | 3 (0)| 00:00:01 |
|* 5 | XPATH EVALUATION | | | | | |
Predicate Information (identified by operation id):
1 - filter( EXISTS (SELECT 0 FROM "DEV"."TABLE1"
"SYS_ORAVW_2",XPATHTABLE('/main/id' PASSING :B1 COLUMNS "C_00$" XMLTYPE
PATH '.', "C_01$" XQEXVAL CHAR PATH '.') "P" WHERE
TO_BINARY_DOUBLE("ID1")=TO_BINARY_DOUBLE("P"."C_01$")))
5 - filter(TO_BINARY_DOUBLE("ID1")=TO_BINARY_DOUBLE("P"."C_01$"))The plan is similar to that of the second query above (XMLTable/EXISTS).
Still using XMLExists, a plan similar to the first query (EXISTS/XMLCast/XMLQuery) can be achieved by casting id to an integer datatype :
SQL> select t.xml_msg_text
2 from xml_tbl t
3 where xmlexists (
4 'fn:collection("oradb:/DEV/TABLE1")/ROW[ID1=xs:int($d/main/id)]'
5 passing t.xml_msg_text as "d"
6 );
XML_MSG_TEXT
<main>
<id>2</id>
</main>
Execution Plan
Plan hash value: 1149640166
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 61 | 7 (15)| 00:00:01 |
|* 1 | HASH JOIN SEMI | | 1 | 61 | 7 (15)| 00:00:01 |
| 2 | TABLE ACCESS FULL| XML_TBL | 2 | 116 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| TABLE1 | 2 | 6 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - access("ID1"=SYS_XQ_ATOMCNVCHK(TO_NUMBER(SYS_XQ_UPKXML2SQL(SYS_XQ
EXVAL(SYS_XQEXTRACT(SYS_MAKEXML(0,"T"."SYS_NC00003$"),'/main/id'),1,50,3
3792,8192),50,1,0)),2,37))
Note
- Unoptimized XML construct detected (enable XMLOptimizationCheck for more information)Check each one on your real scenario to see which show best performance.
(I would tend to say the ones involving streaming evaluation)
Edited by: odie_63 on 5 nov. 2012 12:24
Edited by: odie_63 on 5 nov. 2012 12:38 -
Error in UPDATE statement with SET and JOIN
Hi
UPDATE lms_assessment_student QS JOIN lms_assessment_student_ans QA ON QS.pk_Assessment_Stud_Id = QA.fk_Assessment_Stud_Id SET QA.Mark = 1, QA.Comment_Field = 1 WHERE QS.pk_Assessment_Stud_Id = 1 AND QA.Question_Id = 1;
The above statement when executing is showing ORA-00971: missing SET keyword. so i changed it to
UPDATE lms_assessment_student QS SET QA.Mark = 1, QA.Comment_Field = 1 WHERE QS.pk_Assessment_Stud_Id = 1 AND QA.Question_Id = 1 JOIN lms_assessment_student_ans QA ON QS.pk_Assessment_Stud_Id = QA.fk_Assessment_Stud_Id ;
and it showing ORA-00933: SQL command not properly ended.So can anyone help me in solving this problem
Thanking you in advance
DinnyHi ,
So many errors
YOUR QUERY :
UPDATE lms_assessment_student QS SET QA.Mark = 1, QA.Comment_Field = 1 WHERE QS.pk_Assessment_Stud_Id = 1 AND QA.Question_Id = 1 JOIN lms_assessment_student_ans QA ON QS.pk_Assessment_Stud_Id = QA.fk_Assessment_Stud_Id ;
and it showing ORA-00933: SQL command not properly ended.So can anyone help me in solving this problem
first thing u want to update qa and u write update QS ??
Second y do u want to join??? just put the condition
SOLUTION
UPDATE lms_assessment_student_ans
SET lms_assessment_student_ans.Mark = 1,
lms_assessment_student_ans.Comment_Field = 1
WHERE lms_assessment_student.pk_Assessment_Stud_Id = 1
AND lms_assessment_student.pk_Assessment_Stud_Id = QA.fk_Assessment_Stud_Id
Hope it works for u..
Bhavesh -
Requiring selection with cfform and cfselect
I would like to have CF enforce the required="yes" attrribute
of the cfselect tag as the documentation says it should. I've seen
this discussed before here, but no resolution. The documentation of
the required parameter says:
============ quoted text ============================
* Yes: a list element must be selected when the form is
submitted.
Note: This attribute has no effect if you omit the size
attribute or set it to 1, because the browser always submits the
displayed item. You can work around this issue: format forms by
having an initial option tag with value=" " (note the space
character between the quotation marks).
============ end quoted text =========================
Yet it does not work. I can use additional javascripts,
validate on server side, etc., but I would like it to work as the
documentation says. Or to know why the documentation continues to
say it will work, if it won't.
Thanks!
Code below:Try putting in the closing tag
<option value=" "> </option>
Ken -
EJB3 QL - Selecting with wildcards and parameters
Hi,
Can you please help me create a select query which takes parameters and uses wildcards.
QL:
(List<SubjectEntity>) entityManager
.createQuery("{color:#000080}*SELECT aE FROM SubjectEntity aE WHERE aE.name like :word*{color} ")
.setParameter("word", word)
.getResultList();
I pass Corporate* to my word parameter and my TABLE has a row that contains Corporate Finance as a name and I want it matched by the parameter.
Below is an equivalent statement using SQL*
DB:
SELECT * FROM SUBJECT WHERE NAME LIKE(%?%)
Thanks.One quick fix is in the DAO method where you invoke the query, append a '%' to the end of the word before passing it to the query.
.setParameter("word", word + '%') -
How to create VO with multiple dynamic where clauses on select with UNION?
I am trying to implement the View Object for the UNION query that looks like this:
select a,b,c...
from t1,t2,...
where dynamic_where_clause1
union all
select a,b,c,...
from t11,t12, ...
where dynamic_where_clause2
There are up to 60 input parameters that are used to generate dynamic where clauses. They are actually created by calling PL SQL function.
So far, I was not able to assign both where clauses to the view object. Is there a workable solution for this problem, besides resorting to programmatic View Object?
I understand that recommended way with UNIONs is to wrap both queries into a parent select:
select * from (
select a,b,c...
from t1,t2,...
where ... -- table relationship joints
union all
select a,b,c,...
from t11,t12, ...
where ... -- table relationship joints
) QRSLT
where dynamic_where_clause
Unfortunately this approach doesn't work here, since individual selects are producing unmanageable amount of data and resulting query takes forever to complete.I afraid I would not have any real benefits from using VO if I replace the entire query with every request. Actually, the performance may suffer.
I solved the problem by creating a POJO Data Control and invoking the custom select query from java. Not sure if it is the best approach to the problem, but implementation time is limited and it works.
Actually, this is not the first time I see the need to implement VO with complicated SQL like select with unions and dynamic pieces. It would be nice to find a solution and not resort to workarounds.
Edited by: viksicom on Aug 2, 2012 8:48 AM -
Problems with videos and music requires restore
I have a 30GB Video and in general everything works fine. However, I have had to restore the device 4x since Christmas due to the following;
When I select a video, the display shows the play icon and battery icon then goes blank and returns to the list of videos and/or when selecting a song, it displays the song I selected (with artwork) and then scrolls through the list of songs in that play list and does nothing.
I can resolve the issue by restoring and syncing, but the problem eventually returns.
5th Generation 30GB Video Windows XP USB connectionIn case anyone else has this issue, I found the solution. The problem was caused by my earbuds. I bought a new pair and that fixed the problem.
-
How can I edit imported audio track? After selecting and deleting region, and joining remaining parts, it keeps playing the track with the deleted region!
What am I doing wrong?After selecting and deleting region, and joining remaining parts, it keeps playing the track with the deleted region!
What am I doing wrong?
How exactly are you doing this?
When I cut out a part of an audio region, then drag the remaining parts together, so they are touching, the track will skip the cut out section when playing the file. Also, if I select the remaining parts of the region and use the command "Edit > join regions". This will create a new audio file with the cut out part deleted.
You should see this prompt, when you are joining the regions: -
Problem with XMLTABLE and LEFT OUTER JOIN
Hi all.
I have one problem with XMLTABLE and LEFT OUTER JOIN, in 11g it returns correct result but in 10g it doesn't, it is trated as INNER JOIN.
SELECT * FROM v$version;
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
"CORE 11.2.0.1.0 Production"
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
--test for 11g
CREATE TABLE XML_TEST(
ID NUMBER(2,0),
XML XMLTYPE
INSERT INTO XML_TEST
VALUES
1,
XMLTYPE
<msg>
<data>
<fields>
<id>g1</id>
<dat>data1</dat>
</fields>
</data>
</msg>
INSERT INTO XML_TEST
VALUES
2,
XMLTYPE
<msg>
<data>
<fields>
<id>g2</id>
<dat>data2</dat>
</fields>
</data>
</msg>
INSERT INTO XML_TEST
VALUES
3,
XMLTYPE
<msg>
<data>
<fields>
<id>g3</id>
<dat>data3</dat>
</fields>
<fields>
<id>g4</id>
<dat>data4</dat>
</fields>
<fields>
<dat>data5</dat>
</fields>
</data>
</msg>
SELECT
t.id,
x.dat,
y.seqno,
y.id_real
FROM
xml_test t,
XMLTABLE
'/msg/data/fields'
passing t.xml
columns
dat VARCHAR2(10) path 'dat',
id XMLTYPE path 'id'
)x LEFT OUTER JOIN
XMLTABLE
'id'
passing x.id
columns
seqno FOR ORDINALITY,
id_real VARCHAR2(30) PATH '.'
)y ON 1=1
ID DAT SEQNO ID_REAL
1 data1 1 g1
2 data2 1 g2
3 data3 1 g3
3 data4 1 g4
3 data5 Here's everything fine, now the problem:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
PL/SQL Release 10.2.0.1.0 - Production
"CORE 10.2.0.1.0 Production"
TNS for HPUX: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
--exactly the same environment as 11g (tables and rows)
SELECT
t.id,
x.dat,
y.seqno,
y.id_real
FROM
xml_test t,
XMLTABLE
'/msg/data/fields'
passing t.xml
columns
dat VARCHAR2(10) path 'dat',
id XMLTYPE path 'id'
)x LEFT OUTER JOIN
XMLTABLE
'id'
passing x.id
columns
seqno FOR ORDINALITY,
id_real VARCHAR2(30) PATH '.'
)y ON 1=1
ID DAT SEQNO ID_REAL
1 data1 1 g1
2 data2 1 g2
3 data3 1 g3
3 data4 1 g4As you can see in 10g I don't have the last row, it seems that Oracle 10g doesn't recognize the LEFT OUTER JOIN.
Is this a bug?, Metalink says that sometimes we can have an ORA-0600 but in this case there is no error returned, just incorrect results.
Please help.
Regards.Hi A_Non.
Thanks a lot, I tried with this:
SELECT
t.id,
x.dat,
y.seqno,
y.id_real
FROM
xml_test t,
XMLTABLE
'/msg/data/fields'
passing t.xml
columns
dat VARCHAR2(10) path 'dat',
id XMLTYPE path 'id'
)x,
XMLTABLE
'id'
passing x.id
columns
seqno FOR ORDINALITY,
id_real VARCHAR2(30) PATH '.'
)(+) y ;And is giving me the complete output.
Thanks again.
Regards. -
Select Distinct and join in ODI
Hi,
I have following task to perform: I am loading metadata into Planning dimension from Oracle database. I have two tables
1. "Sales"
Columns: Name, Number, Value
Sample Data:
Product 1, 10, 200
Product 2, 30, 100,
Product 1, 15, 500
2. P&R
Columns:
Name, Alias
Product 1, SampleSoda1
Product 2, SampleSoda2,
Resource 1, CanForSoda,
Resource 2, CO2
What I need to do is: I have to select name and alias from second table of all products that were sold.
So I need to select distinct Name from Table 1, naxt join it with Table 2 (so I have Name and Alias) and load it to planning.
I am a little confused how to do it.
Any help would be great!
Best regards,
GregHi Greg,
What you can do is either :
- Create a yellow interface with your table Sales as source. Map the name column directly in the target. In the flow tab, click on your target and select "Distinct rows".
- Create a second interface, with your first interface as source. Select the "Use Temporary Interface as Derived Table (Sub-Select)" checkbox.
- Add your second datastore and join it. Or you can use a lookup table.
OR
- Create an interface with Sales and P&R as source (or set P&R as a lookup table).
- Go on the flow tab and select "Distinct rows".
If you've a lot a data in the first table, I would go for the first solution.
Hope it helps.
Regards,
JeromeFr
Edited by: JeromeFr on Feb 14, 2013 9:52 AM
To be more clear in the first step of solution 1 -
Select and join one month record
I receive a request from my customer . He want generate a total sales record for a month and those data stored in daily sales table and customer table.
tblcustomer-20140101
tblcustomer-20140102
tblcustomer-20140103
tblcustomer-20141231
tblsales-20140101
tblsales-20140102
tblsales-20140103
tblsales-20141231
is there anyway when user select month name from application(ex: April), it will choose all april record and join it together?Hi kjleong,
Generally to say, it is not a good practice to store the same entity data individually for each day, there are many shortages, such as the problem you posted in this thread. You can put all of them into one table and tag them with a DATE column. Anyway
in this case, to achieve your requirement, you may refer the below stored procedure(SP).
CREATE PROC ProcGetSales
@Year VARCHAR(20),
@Month VARCHAR(20)
AS
DECLARE @Date DATE;
DECLARE @Days INT;
DECLARE @SqlStr NVARCHAR(MAX);
SET @Date = @Year+'-'+@Month+'-01';
SET @Days = DATEDIFF(DAY,@Date,DATEADD(MM,1,@Date)); --get the days of the specific month
create a temp table
CREATE TABLE #Temp
col1 int --you should put the columns after join in your real envrioment here
table join statement, put the join result into a temp table
I dont know the columns so I use * here and the join condition is just based on assumption
you should modify the statement basing on your real environment
SET @SqlStr= 'INSERT INTO #Temp SELECT * FROM [tblcustomer-WhichDay] tc JOIN [tblsales-WhichDay] ts ON tc.customerID = ts.customerID';
DECLARE @Counter INT = 1;
WHILE @Counter <= @Days --loop the tables named after the specific month
BEGIN
DECLARE @Sql NVARCHAR(MAX) = REPLACE(@SqlStr,'WhichDay',CONVERT(VARCHAR(8),CAST(@Year+'-'+@Month+'-'+LTRIM(STR(@Counter)) AS DATE),112));
EXEC sp_executesql @Sql;
SET @Counter = @Counter+1;
END
SELECT * FROM #TEMP
The SP is not ready to use, you have to make some modification basing on your real environment before it works.
If you have any question, feel free to let me know.
Best regards,
Eric Zhang
If you have any feedback on our support, please click
here. -
Join two Connect By Prior Start With trees and return only common records?
Oracle 10g Release 2 (10.2)
I have two tables that have tree structured data. The results, when running the queries individually are correct, however I need to join tree one to tree two in order to obtain only the common records between them.
-- Tree one
SELECT ip_entity_name, entity_code, hier_level, entity_parent
FROM ip_hierarchy
WHERE hier_level >= 3
CONNECT BY PRIOR entity_code = entity_parent
START WITH entity_code = 'MEWWD';
-- Tree two
SELECT ip_entity_name, entity_code, hier_level, entity_parent
FROM ipt_hierarchy
WHERE hier_level >= 3
CONNECT BY PRIOR entity_code = entity_parent
START WITH entity_code = 'IPNAM';
As I understand, joins may not work with CONNECT BY/START WITH queries?
Is a WITH clause an option?
If at all possible, I don't want to put one select in a View database object and join against the other query.
Thanks.Hi JTP51,
You can use WITH clause or sub-query by using in-line view, without creating any view object in database.
for example
SELECT A.IP_ENTITY_NAME, A.ENTITY_CODE, ....
FROM (SELECT IP_ENTITY_NAME, ENTITY_CODE, HIER_LEVEL, ENTITY_PARENT
FROM IP_HIERARCHY
WHERE HIER_LEVEL >= 3
CONNECT BY PRIOR ENTITY_CODE = ENTITY_PARENT
START WITH ENTITY_CODE = 'MEWWD') A,
(SELECT IP_ENTITY_NAME, ENTITY_CODE, HIER_LEVEL, ENTITY_PARENT
FROM IPT_HIERARCHY
WHERE HIER_LEVEL >= 3
CONNECT BY PRIOR ENTITY_CODE = ENTITY_PARENT
START WITH ENTITY_CODE = 'IPNAM') B
WHERE A. ENTITY_CODE = B. ENTITY_CODE
AND ....Best regards,
Zhxiang
Edited by: zhxiangxie on Feb 2, 2010 5:35 PM
Maybe you are looking for
-
What do I do to open a document after I convert a PDF to Word
How do I open a document after I convert a PDF to a word document?
-
IPhoto 08- Can't open??
Recently l installed Leopard and then iLife 08. All seemed to be working OK but then today as I was attempting to do software updates I had a complete "power shutdown" which I have had before and usually on restart all is OK. Not this time. Can't ope
-
Hi Problem with two different users from same country first user is getting vendor name and address in japan language when he post vendor invoice either through FB60 or MIRO but when second user trying post vendor invoice using the above txn codes he
-
Hi, i have a E51 that i just want to use without email. can i set it up in such a way that when i press the mail button i access my text messages as opposed to any emails? i don't want email access on this phone. thanks for your help
-
Error installing Premiere Elements 10
The install always dies at 51% and gives me the following error. Any ideas? Exit Code: 7 -------------------------------------- Summary -------------------------------------- - 0 fatal error(s), 8 error(s), 7 warning(s) WARNING: DW031: Payload:{3F02