Self Join Issue
Hi all,
SQL> select * from TEST6152;
A B DD
2 USD 12-DEC-07
30 USD 12-DEC-07
30 USD 12-NOV-07
15 USD 22-NOV-07
65 USD 13-SEP-07
I require this output from the quiery
CUR DT SUM(T.A)
USD 30-SEP-07 65
USD 30-NOV-07 110
USD 31-DEC-07 142
which is basically the cumulative sum
Now this is what I tried
SQL> ed
Wrote file afiedt.buf
1 select t1.curr, t1.dt, sum(t.a) from
2 test6152 t,
3 (
4 select B curr, last_day(trunc(DD)) dt from TEST6152 group by B, last_day(trunc(dd))
5 ) t1
6 where
7 t.b = t1.curr
8 and t.dd < t1.dt
9 group by
10 t1.curr, t1.dt
11* order by 1, 2
SQL> /
CUR DT SUM(T.A)
USD 30-SEP-07 65
USD 30-NOV-07 110
USD 31-DEC-07 142
.Now my question is : Is there a better way to do this in SQL coz when I translate the same logic to a bigger table (rows > 3 million) , it gives me a performance issue.
Thanks in advance
Hi,
you can use analytic functions:
select
B as CUR,
C as DT,
sum (A) over (partition by B order by C) as COM_SUM
from test 6152
Similar Messages
-
Performance Tuning - Self Join Issue
Hi,
The following query takes long time to execute. Is there any better way to
re-writing the query to reduce the time it takes to execute.
INSERT INTO TT_TEMP_MAINGUI_SP_PERCENT_MOV
(prev_prc_dt,asset_id,pricing_pt_id,price_dt)
SELECT max(tpm2.prc_dt),
tpm2.asset_id ,
tpm2.pricing_pt_id ,
tpm1.prc_dt
FROM t_prc_master tpm1,
t_prc_master tpm2
WHERE tpm1.prc_dt = '19-Dec-07'
AND tpm1.asset_id = tpm2.asset_id
AND tpm1.pricing_pt_id = tpm2.pricing_pt_id
AND tpm2.prc_dt < tpm1.prc_dt
AND tpm2.accept_flg = 'Y'
AND tpm1.accept_flg = 'Y'
AND EXISTS (SELECT 1 FROM t_temp_prcmov
WHERE pca_flg = 'P'
AND tpm1.pricing_pt_id = prc_pt_cntry_atyp)
GROUP BY tpm2.asset_id, tpm2.pricing_pt_id,tpm1.prc_dt;
select count(*) from t_prc_master
where prc_dt = '19-Dec-07'
COUNT(*)
784161
-- Here is the TKPROF Output
INSERT INTO TT_TEMP_MAINGUI_SP_PERCENT_MOV
(prev_prc_dt,asset_id,pricing_pt_id,price_dt)
SELECT max(tpm2.prc_dt),
tpm2.asset_id ,
tpm2.pricing_pt_id ,
tpm1.prc_dt
FROM t_prc_master tpm1,
t_prc_master tpm2
WHERE tpm1.prc_dt = '19-Dec-07'
AND tpm1.asset_id = tpm2.asset_id
AND tpm1.pricing_pt_id = tpm2.pricing_pt_id
AND tpm2.prc_dt < tpm1.prc_dt
AND tpm2.accept_flg = 'Y'
AND tpm1.accept_flg = 'Y'
AND EXISTS (SELECT 1 FROM t_temp_prcmov
WHERE pca_flg = 'P'
AND tpm1.pricing_pt_id = prc_pt_cntry_atyp)
GROUP BY tpm2.asset_id, tpm2.pricing_pt_id,tpm1.prc_dt
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 226.01 317.50 1980173 4915655 805927 780544
Fetch 0 0.00 0.00 0 0 0 0
total 2 226.01 317.51 1980173 4915655 805927 780544
Misses in library cache during parse: 1
Optimizer goal: CHOOSE
Parsing user id: 98 (PRSDBO)
Rows Row Source Operation
780544 SORT GROUP BY (cr=4915236 r=1980165 w=0 time=312751120 us)
40416453 NESTED LOOPS (cr=4915236 r=1980165 w=0 time=245408132 us)
783459 NESTED LOOPS (cr=956325 r=92781 w=0 time=17974163 us)
55 TABLE ACCESS FULL T_TEMP_PRCMOV (cr=3 r=0 w=0 time=406 us)
783459 TABLE ACCESS BY INDEX ROWID T_PRC_MASTER (cr=956322 r=92781 w=0 time=17782856 us)
784161 INDEX RANGE SCAN PRC_DT_ASSET_ID (cr=412062 r=69776 w=0 time=14136725 us)(object id 450059)
40416453 INDEX RANGE SCAN ASSET_DT_ACCEPT_FLG (cr=3958911 r=1887384 w=0 time=217215303 us)(object id 450055)
Rows Execution Plan
0 INSERT STATEMENT GOAL: CHOOSE
780544 SORT (GROUP BY)
40416453 NESTED LOOPS
783459 NESTED LOOPS
55 TABLE ACCESS GOAL: ANALYZED (FULL) OF 'T_TEMP_PRCMOV'
783459 TABLE ACCESS GOAL: ANALYZED (BY INDEX ROWID) OF
'T_PRC_MASTER'
784161 INDEX GOAL: ANALYZED (RANGE SCAN) OF 'PRC_DT_ASSET_ID'
(NON-UNIQUE)
40416453 INDEX GOAL: ANALYZED (RANGE SCAN) OF 'ASSET_DT_ACCEPT_FLG'
(UNIQUE)
Could somebody help me in resolving the issue? It would be appreciated...Well, it's a bit of a mess to read. Please use the pre or code tags enclosed in [] next time to preserve the formatting of the code.
First thing that looks 'bad' to me is
WHERE tpm1.prc_dt = '19-Dec-07'which should be (i assume you want 2007 and not 1907)
WHERE tpm1.prc_dt = TO_DATE('19-Dec-2007', 'DD-MON-YYYY');The next thing i'm very confused with is...why are you self joining the table? You should be able to just do this.....logically, it should produce the same results, though it's obviously not tested :D)
SELECT
max(tpm2.prc_dt),
tpm2.asset_id ,
tpm2.pricing_pt_id ,
TO_DATE('19-Dec-2007', 'DD-MON-YYYY') AS prc_dt
FROM t_prc_master tpm2
WHERE tpm2.prc_dt < TO_DATE('19-Dec-2007', 'DD-MON-YYYY')
AND tpm2.accept_flg = 'Y'
AND EXISTS (
SELECT
NULL
FROM t_prc_master tpm1
WHERE tpm1.prc_dt = TO_DATE('19-Dec-2007', 'DD-MON-YYYY')
AND tpm1.asset_id = tpm2.asset_id
AND tpm1.pricing_pt_id = tpm2.pricing_pt_id
AND tpm1.accept_flg = 'Y'
AND tpm1.pricing_pt_id IN (SELECT tmov.prc_pt_cntry_atyp FROM t_temp_prcmov tmov WHERE tmov.pca_flg = 'P')
GROUP BY tpm2.asset_id, tpm2.pricing_pt_id, TO_DATE('19-Dec-2007', 'DD-MON-YYYY');Message was edited by:
Tubby -
How to write self join in sql?
Hi,
I have table named "table_upload", column "record_type" value "01,03,04....." and start_date,end_date and so on
And i will have value for start_date,end_date only for record_type=01,rest of type these two columns will be null.
now i need to write query with self join, to include above concept. can any one please help me .
and my query look like follows,I just want to re write
Please provide DDL+DML + expected output!
and short explanation what is it that we all ask you for:
DDL = Data Definition Language. In our case that is, CREATE TABLE statements for your tables and other definitions that are needed to understand your tables structure and there for let us to test and reproduce the problem in our server. Without DDL no one
can execute any query.
How to get DDL: Right click on the table in Object Explorer and select script table as CREATE. Post these create table scripts here.
DML = data manipulation language is a family of queries used for manipulating the data it self like: inserting, deleting and updating data. In our case we need some sample data in order to check the query and get result, so we need some indert query for
sample data.
If you post a "create table query" for the tables and "insert <table> query" with some sample, then we could help you without Assuming/Guessing. There is a reason that DDL is generally asked for and expected when discussing query problems - it helps
to identify issues, clarify terminology and prevent incorrect assumptions. Sample data also provides a common point of reference for the discussion. A script that can be used to illustrate or reproduce the issue you have, will encourage others to help.
[Personal Site] [Blog] [Facebook] -
Self Joining and Inline Query. A tricky report.
I am stuck with a very tricky situation.Please help.This is PROD issue.
I have written a SQL code which has 1 inline queries,and displays the right results
with the right report output
Dont get confused.Just go thru this.
select pie.id_inst_code,
ISNULL(PN.Active, 0) 'Active',
from position_master_input_event pie,
(select insx.id_inst_xref_type,insx.id_inst_xref,count(*) 'PrimaryListing'
from instrument ins, instrument_xref insx
where ins.id_inst = insx.id_inst
and insx.flg_active = 'Y'
and ins.flg_active = 'Y'
group by insx.id_inst_xref_type,insx.id_inst_xref
)PN
where id_entity = 'AGL'
and pie.id_inst_code *= PN.id_inst_xref
and pie.id_src_inst_code_type*= PN.id_inst_xref_type
group by pie.id_inst_code,PN.Active,
Table :Instrument_xref
id_inst id_inst_xref_type id_inst_xref flg_active
0372285 SE B0DV8Y9 Y
0372285 IS GB00B03MLX29 Y
Table :Instrument
id_inst id_inst_xref_type id_inst_xref flg_active flg_primary_listing
0372285 SE B0DV8Y9 Y N
OUTPUT:
id_inst_xref Active
B0DV8Y9 1
PERFECT.Works fine
2) Now comes the tricky part.:
0372285 also has GB00B03MLX29 which has flg_active to Y and which also maps to 0372285.
Am I right?
New reportOutput
id_inst_xref Active PRIMARY ISIN
B0DV8Y9 1 1
So,now I want a SELF JOIN this way built into the code:
(hardcoded values work)
(i)
select a.id_inst_xref
from instrument_xref a,
instrument_xref b
where b.id_inst_xref ='B0DV8Y9'
and b.id_inst = a.id_inst
and b.id_inst_xref_type in ('SE','IS')
and a.id_inst_xref = 'GB00B03MLX29'
(ii)
select count(*) 'PrimaryISIN'
from instrument ins,
instrument_xref insx
where ins.id_inst = insx.id_inst
and insx.flg_active = 'Y'
and ins.flg_primary_listing = 'Y'
and ins.flg_active = 'Y'
And now LINKING ALL :
select pie.id_inst_code,
ISNULL(PN.Active, 0) 'Active',
from position_master_input_event pie,
(select insx.id_inst_xref_type,insx.id_inst_xref,count(*) 'PrimaryListing'
from instrument ins, instrument_xref insx
where ins.id_inst = insx.id_inst
and insx.flg_active = 'Y'
and ins.flg_active = 'Y'
group by insx.id_inst_xref_type,insx.id_inst_xref
)PN,
(select count(*) 'PrimaryISIN'
from instrument ins,
instrument_xref insx
where ins.id_inst = insx.id_inst
and insx.flg_active = 'Y'
and ins.flg_primary_listing = 'Y'
and ins.flg_active = 'Y'
and insx.id_inst_xref = (
select DISTINCT a.id_inst_xref
from instrument_xref a,
instrument_xref b
where b.id_inst_xref = 'B0DV8Y9'
and b.id_inst = a.id_inst
and b.id_inst_xref_type in ('SE','IS')
and a.id_inst_xref = 'GB00B03MLX29'
where id_entity = 'AGL'
and pie.id_inst_code *= PN.id_inst_xref
and pie.id_src_inst_code_type*= PN.id_inst_xref_type
group by pie.id_inst_code,PN.Active,
THE Self join works fine as long as it is hardcoded.
But assume there can br multiple such situations as the above,and I dont want to
hardcode the values,how can I build the NEW REPORT by SELF JOINING.
Please can someome help.This is a tricky one.
Is there a better way to thisIsn't this the same question as:
Passing values dynamically Froman 'INLINE Query' to a 'SUB QUERY'
and
Another query regarding Inline Query and Self Join and pass Column Values -
SQL> desc messages;
Name Null? Type
MESSAGEID NOT NULL NUMBER
TITLE NOT NULL VARCHAR2(50)
AUTHOR VARCHAR2(20)
BODY NOT NULL VARCHAR2(4000)
BOARD NUMBER
THREAD NOT NULL NUMBER
DATE_CREATED NOT NULL DATE
This is the table i need to do a self join on, i know the two queries individually but i need to have them in one join,,,
select b.title,b.boardid,m.messageid,m.title,m.author,m.date_created,m.body
from messages m, boards b where b.boardid=m.board and m.thread=0 and b.boardid='198'
and m.messageid='241';
Thread title Author Starting message Last post
Austrailia noel Austrailia 04/01/2005 21:22:35
-----selects replies to the above message-----------
select author,date_created,body
from messages
where board=198 and thread=241;
AUTHOR DATE_CREATED BODY
noel 05-JAN-05 Oz is played on clay
noel 05-JAN-05 Oz played on grass
noel 05-JAN-05 Oz played on grass
This is the output that i want but at the moment thread title is repeated for all four rows returned.I want the output to look like a reply to a message like you see in some message boards on the web,,, you have the starting message and underneath you see the replies
Thread title Author Starting message Last post
Austrailia noel Austrailia 04/01/2005
noel Oz is played on clay 05-JAN-05
noel Oz played on grass 05-JAN-05
noel Oz played on grass 05-JAN-05Please please please do not post duplicate threads. It makes it hard for people to follow what's going on. And it's annoying to boot. I think this is the third thread you have started on this topic.
Please remember we are volunteers here, not Oracle employees. We do the best we can to answer queries in a timely fashion but if you want snappier service then I'm afraid you'll need to pay for a support contract.
You posted a rejoinder to Barbara's answer to your other thread URGENT: combining two sql statements. That is sufficient to keep your issue in the public arena.
Thank you for your co-operation.
Regards, APC
Issued in memory of the Rogue Moderators -
We have 3 WLC's(5500) in our network and about 150 AP's. Only 4 AP's register to 1 controller, over 70 to 2nd and about 50 to 3rd. On checking & comparing few of the AP's this is what i concluded.
1. 4 AP's that registered to the first WLC did not have that AP in the primary, secondary or tertiary list. If it was there then it was either secondary or tertiary or the device name entered is not resolvable by DNS but the device name is correct. Management IP was not configured on any of the 4 AP's for any of the WLC's
2. AP's registered to second and third WLC's have similar config. First WLC as Primary, Second as secondary and third Tertiary with correct DNS name in the field but wrong device name. Also all have Management IP's entered as well.
CAPWAP Join Taken Time for 4 AP's varies from 6to10 mins while for other AP its few seconds. DNS for cisco-capwap-controller points to WLC with4 AP's. I donot see any use of option in DNS for WAP's.
How can i make AP's join this WLC.
Should I get the DNS and device name discrepancy corrected?
What is the selection process for AP's to choose WLC, as I see AP's not joining WLC in there building but joining a WLC in other adjacent building? Is there a way for me to influence this decision?What is the selection process for AP's to choose WLC, as I see AP's not joining WLC in there building but joining a WLC in other adjacent building? Is there a way for me to influence this decision?
Best way to do this is configure AP High Availability of APs with primary,secondary,tertiary WLC name & IP (both fields required). This is taking precedence over any other methods.
http://mrncciew.com/2013/04/07/ap-failover/
If you have AP join issue, try to configure DHCP option 43 & see if that helps
http://www.cisco.com/c/en/us/support/docs/wireless-mobility/wireless-lan-wlan/97066-dhcp-option-43-00.html
If this is one off case, you can try static or broadcast forwarding as a interim solution
http://mrncciew.com/2013/03/17/ap-registration/
http://mrncciew.com/2013/05/04/wlc-discovery-via-broadcast/
HTH
Rasika
*** Pls rate all useful responses *** -
Self join with fact table in Obie 10G
I am a newbie to obiee.I have a development requirement as follows-
I need to find supervisors designation with the existing star RPD design. explanation is below
DIM_Designation(Desig_Wid)
|(Row_wid)
|
DIM_EMPLOYEE--------WORKER_FACT------------DIM_Supervisor
(Row_Wid)-----------------(Employee_Wid)
(Supervisor_Wid)------------(Row_Wid)
3 dimension is joined to fact to get employee, his supervisor and designation of employee. now i want to get the supervisor's designation? how is it possible? already employee and supervisor dimension is same W_employee_d table joined with fact as alias DIM_EMPLOYEE and DIM_SUPERVISOR. how to do self join with fact to get supervisor's designation. i do not have any supervisor_desig_wid in fact table. any help is deeply appreciated.Yes,Duplicate the fact table create a primary key on the newly fact table alias dimension table.So you can ur data modelling as usual.
-
How to achieve parent-child relationship using self join?
my table structure is as follows
parent child name
-1 1 A1
1 2 A2
1 3 A3
how to achieve the hierarchy model using self join. this can be easily achieved using "connect by prior". but how to achieve the same using self join?Hi,
Yes, that's definitely possible. If you only need to display two levels from the hierarchy, a self-join is a good option. Make it an outer join if you need to show everyone on one level, regardless of whether they have a match on the other level or not; for example, if you want the output:
child_name child_id parent_name parent_id
A1 1
A2 2 A1 1
A3 3 A1 1It's good that you posted some sample data. Now post the results you want from that data, and your query (what you think is the best attempt you've made so far). If you haven't tried anything so far, then look at some other simple self-join to get ideas. -
Problem writing a XSLT to convert XML in desired format from a table having self join
Hello,
I have to write a style sheet to convert XML generated from XSQL into a different format. The query that I have is as follows.
select LEVEL depth,
'H' || hierarchy_id id,
name,
nvl2(parent_id, 'H' || parent_id, 0) parent_id,
CURSOR(select LEVEL depth,
'H' || hierarchy_id hid,
name hname,
nvl2(parent_id, 'H' || parent_id, 0) hparent_id,
decode(system_id, NULL, '0', 'S' || system_id) formatted_system_id,
system_id
from hierarchy
where parent_id = h.hierarchy_id
and system_id is not null
) as systems
from hierarchy h
where system_id is null
start with parent_id is null
connect by prior hierarchy_id = parent_id
The hierarchy table has a self join to itself. The selfjoin is on the hierarchyid and the parentid fields which is evident from the query.
Here the hierarchy table contains the parent system and also the child systems underneath. The problem is that the no. of levels that it can go deep is not fixed. The output of this in sqlplus is as follows.
Depth Hierarchyid, name parentid
1 h1 xxx <null>
2 h2 bbb h1
3 h3 ccc h2
<Cursor for systems>
hid hname hparentid formatted_system_id systemid
h4 ccc h2 s1 1
h5 ccc h2 s2 2
<Back to original data>
Depth Hierarchyid, name parentid
2 h6 ddd h1
2 h7 eee h1
The desired output required from the stylesheet is as follows
<h id=h1 name=xxx>
<h id=h2 name=bbb parentid=h1>
<h id=h3 name=ccc parentid=h2>
<h id=h4 name=fff parentid=h3 systemid=s1>
<h id=h5 name=ggg parentid=h3 systemid=s2>
</h>
</h>
<h id=h6 name=ddd parentid=h1/>
<h id=h7 name=eee parentid=h1/>
</h>
Could some one guide me as to how to get this. I did write a stylesheet which gives me the following output.
<h id=h1 name=xxx>
<h id=h2 name=bbb parentid=h1/>
<h id=h3 name=ccc parentid=h2/>
<h id=h4 name=fff parentid=h3 systemid=s1>
<h id=h5 name=ggg parentid=h3 systemid=s2>
</h>
<h id=h6 name=ddd parentid=h1/>
<h id=h7 name=eee parentid=h1/>
</h>
As you can see I am missing the closing of the tag on the 7th line in the desired format. I have written the following stylesheet.
<!-- Hierarchy.xsl: Transform ROWSET/ROW format to the required Hierarchy format. -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Template for matching the rowset..... -->
<xsl:template match="HIERARCHY">
<HIERARCHY><xsl:apply-templates/></HIERARCHY>
</xsl:template>
<!-- Template for matching the row .... -->
<xsl:template match="HELEMENT">
<xsl:choose>
<xsl:when test="PARENT_ID=0">
<helement id="{ID}" name="{NAME}" parentid="{PARENT_ID}"/>
</xsl:when>
<xsl:when test="PARENT_ID!='0'">
<helement2 id="{ID}" name="{NAME}" parentid="{PARENT_ID}">
<xsl:for-each select="SYSTEMS/SYSTEMS_ROW">
<helement3 id="{HID}" name="{HNAME}" parentid="{HPARENT_ID}" systemid="{FORMATTED_SYSTEM_ID}">
</helement3>
</xsl:for-each>
</helement2>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Is there any way of achieving this. Any help would be appreciated. I am using XSQL to generate first the basic XML output and then applying stylesheet to achieve the desired output.
Sincerely,
DHello,
I have to write a style sheet to convert XML generated from XSQL into a different format. The query that I have is as follows.
select LEVEL depth,
'H' || hierarchy_id id,
name,
nvl2(parent_id, 'H' || parent_id, 0) parent_id,
CURSOR(select LEVEL depth,
'H' || hierarchy_id hid,
name hname,
nvl2(parent_id, 'H' || parent_id, 0) hparent_id,
decode(system_id, NULL, '0', 'S' || system_id) formatted_system_id,
system_id
from hierarchy
where parent_id = h.hierarchy_id
and system_id is not null
) as systems
from hierarchy h
where system_id is null
start with parent_id is null
connect by prior hierarchy_id = parent_id
The hierarchy table has a self join to itself. The selfjoin is on the hierarchyid and the parentid fields which is evident from the query.
Here the hierarchy table contains the parent system and also the child systems underneath. The problem is that the no. of levels that it can go deep is not fixed. The output of this in sqlplus is as follows.
Depth Hierarchyid, name parentid
1 h1 xxx <null>
2 h2 bbb h1
3 h3 ccc h2
<Cursor for systems>
hid hname hparentid formatted_system_id systemid
h4 ccc h2 s1 1
h5 ccc h2 s2 2
<Back to original data>
Depth Hierarchyid, name parentid
2 h6 ddd h1
2 h7 eee h1
The desired output required from the stylesheet is as follows
<h id=h1 name=xxx>
<h id=h2 name=bbb parentid=h1>
<h id=h3 name=ccc parentid=h2>
<h id=h4 name=fff parentid=h3 systemid=s1>
<h id=h5 name=ggg parentid=h3 systemid=s2>
</h>
</h>
<h id=h6 name=ddd parentid=h1/>
<h id=h7 name=eee parentid=h1/>
</h>
Could some one guide me as to how to get this. I did write a stylesheet which gives me the following output.
<h id=h1 name=xxx>
<h id=h2 name=bbb parentid=h1/>
<h id=h3 name=ccc parentid=h2/>
<h id=h4 name=fff parentid=h3 systemid=s1>
<h id=h5 name=ggg parentid=h3 systemid=s2>
</h>
<h id=h6 name=ddd parentid=h1/>
<h id=h7 name=eee parentid=h1/>
</h>
As you can see I am missing the closing of the tag on the 7th line in the desired format. I have written the following stylesheet.
<!-- Hierarchy.xsl: Transform ROWSET/ROW format to the required Hierarchy format. -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Template for matching the rowset..... -->
<xsl:template match="HIERARCHY">
<HIERARCHY><xsl:apply-templates/></HIERARCHY>
</xsl:template>
<!-- Template for matching the row .... -->
<xsl:template match="HELEMENT">
<xsl:choose>
<xsl:when test="PARENT_ID=0">
<helement id="{ID}" name="{NAME}" parentid="{PARENT_ID}"/>
</xsl:when>
<xsl:when test="PARENT_ID!='0'">
<helement2 id="{ID}" name="{NAME}" parentid="{PARENT_ID}">
<xsl:for-each select="SYSTEMS/SYSTEMS_ROW">
<helement3 id="{HID}" name="{HNAME}" parentid="{HPARENT_ID}" systemid="{FORMATTED_SYSTEM_ID}">
</helement3>
</xsl:for-each>
</helement2>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Is there any way of achieving this. Any help would be appreciated. I am using XSQL to generate first the basic XML output and then applying stylesheet to achieve the desired output.
Sincerely,
D -
Oracle query with out using self join
hi friends,
i have one table for exeample PERSTATUS
pk/fK STUDENT NUMBER SUBJECT MARKS STATUS
1 ACCOUNTS 15 RED
1 MATHS 35 YELLOW
1 SCINECE 45 GREEN
2 ACCOUNTS 55 BROWN
2 MATHS 35 YELLOW
2 SCINECE 45 GREEN
3 ACCOUNTS 15 RED
3 MATHS 35 YELLOW
3 SCINECE 45 GREEN
i want students how status is both red and yellow so i am using self join
i want students status is both red and yellow so i am using self join
SELECT PS.STUDENTNUMBER,PS.STATUS,PS.STATUS1 FROM PERSTATUS PS ,PERSTATUS PS1
WHERE PS.STUDENTNUMBER-PS1.STUDENTNUMER
PS.STATUS='RED' AND PS1.STAUTS='YELLOW'
i want students status is both RD and YELLOW AND GREEN so i am using self join( two self joinS}
SELECT PS.STUDENTNUMBER,PS.STATUS,PS.STATUS,PS2.STATUS FROM PERSTATUS PS ,PERSTATUS PS1,PERSTATUS PS2
WHERE PS.STUDENTNUMBER-PS1.STUDENTNUMER AND PS.STUDENTNUMBER-PS2.STUDENTNUMBER
PS.STATUS='RED' AND PS1.STAUTS='YELLOW' AND PS2.STAUTUS='GREEN'
if i require MORE STATUS then more self joins required, is there any alternative to achive this
and if results comes in multiple rows are accepted (since with the above query result will come in single row)
i tried to use group by (studentnumber,status) with status='red' and status='yellow'
but it is not possible could you povidet he solutionHi,
Whenever you have a problem, please post CREATE TABLE and INSERT statements for your sample data, and the exact results you want from that data. Explain how you get those results from that data.
See the forum FAQ {message:id=9360002}
Here's an example of how to post the sample data:
CREATE TABLE perstatus
( studentnumber NUMBER
, subject VARCHAR2 (10)
, marks NUMBER
, status VARCHAR2 (10)
INSERT INTO perstatus (studentnumber, subject, marks, status)
VALUES (1, 'ACCOUNTS', 15, 'RED');
INSERT INTO perstatus (studentnumber, subject , marks, status)
VALUES (1, 'MATHS', 35, 'YELLOW');
INSERT INTO perstatus (studentnumber, subject, marks, status)
VALUES (1, 'SCINECE', 45, 'GREEN');
INSERT INTO perstatus (studentnumber, subject, marks, status)
VALUES (2, 'ACCOUNTS', 55, 'BROWN');
INSERT INTO perstatus (studentnumber, subject , marks, status)
VALUES (2, 'MATHS', 35, 'YELLOW');
INSERT INTO perstatus (studentnumber, subject, marks, status)
VALUES (2, 'SCINECE', 45, 'GREEN');
INSERT INTO perstatus (studentnumber, subject, marks, status)
VALUES (3, 'ACCOUNTS', 15, 'RED');
INSERT INTO perstatus (studentnumber, subject , marks, status)
VALUES (3, 'MATHS', 35, 'YELLOW');
INSERT INTO perstatus (studentnumber, subject, marks, status)
VALUES (3, 'SCINECE', 45, 'GREEN');You were on the right track, thinking about GROUP BY. You're interested in something about the whole group of rows that has the same studentnumber. Looking at any individual row won't tell you if that row is part of the group you're interested in or not.
If you want to see information about the group as a whole, you can do the whole job with GROUP BY. In this case, studnetnumber is the only thing that an entire group has in common. If you wanted to see the studentnumbers that had both RED and YELLOW, that is:
STUDENTNUMBER
1
3here's one way you could do it:
SELECT studentnumber
FROM perstatus
WHERE status IN ('RED', 'YELLOW')
GROUP BY studentnumber
HAVING COUNT (DISTINCT status) = 2 -- That is, both RED and YELLOW
ORDER BY studentnumber
;But say you wanted to see details about individuals in the group; for example, say we want to see all the columns for students that have all 3 of RED, YELLOW and GREEN, like this:
STUDENTNUMBER SUBJECT MARKS STATUS
1 SCINECE 45 GREEN
1 ACCOUNTS 15 RED
1 MATHS 35 YELLOW
3 SCINECE 45 GREEN
3 ACCOUNTS 15 RED
3 MATHS 35 YELLOWWe used the aggregate COUNT function earlier, but aggregate functions require collapsing the results down to one row per group.
However, most of the aggregate functions, like COUNT, have analytic counterparts, that can give the same results without collapsing the result set. Here's one way to get the results above, using the analytic COUNT function:
WITH got_cnt AS
SELECT studentnumber, subject, marks, status
, COUNT ( DISTINCT CASE
WHEN status IN ('RED', 'YELLOW', 'GREEN')
THEN status
END
) OVER (PARTITION BY studentnumber) AS cnt
FROM perstatus
SELECT studentnumber, subject, marks, status
FROM got_cnt
WHERE cnt = 3
ORDER BY studentnumber
, status
; -
Oracle doc inconsistent on materialize view with union all and self joins
First of all, I can't seem to create a materialized view containing self-joins AND union all. Is it possible?
I checked Oracle 9i (my version: PL/SQL Release 9.2.0.4.0 - Production) documentation and I get different answers (or so it seems to me).
First I saw this: "The COMPATIBILITY parameter must be set to 9.0 if the materialized aggregate view has inline views, outer joins, self joins or grouping sets and FAST REFRESH is specified during creation..."
Did you see the part about 'self joins' in there? I did and I was pumped because that seems to say that you CAN have 'self joins' (and my compatibility is 9.2...)
BUT
In the very same document I also found "Oracle does not allow self-joins in materialized join views." (rage)
You can see the document I am speaking of here: http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96520/mv.htm#574889
Whenever I try to create the mview I get the following error. (
In any caseORA-01446 cannot select ROWID from view with DISTINCT, GROUP BY, etc.First of all, I can't seem to create a materialized view containing self-joins AND union all. Is it possible?
I checked Oracle 9i (my version: PL/SQL Release 9.2.0.4.0 - Production) documentation and I get different answers (or so it seems to me).
First I saw this: "The COMPATIBILITY parameter must be set to 9.0 if the materialized aggregate view has inline views, outer joins, self joins or grouping sets and FAST REFRESH is specified during creation..."
Did you see the part about 'self joins' in there? I did and I was pumped because that seems to say that you CAN have 'self joins' (and my compatibility is 9.2...)
BUT
In the very same document I also found "Oracle does not allow self-joins in materialized join views." (rage)
You can see the document I am speaking of here: http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96520/mv.htm#574889
Whenever I try to create the mview I get the following error. (
In any caseORA-01446 cannot select ROWID from view with DISTINCT, GROUP BY, etc. -
How to resolve Self Joins in OBIEE
Hi All
Could any one help how to resolve the Self Join relations in OBIEE (ex. "SCOTT.EMP" table has "EMPNO" column which is referenced by "MGR" column in the same table.
Request:
Need to drill down from the Top manager to his employees and so on
Another example can be applied for reporting the Organisation ChartThis is a great article that shows ho to do the Alias:
Adding an Alias table within the BI physical layer using Oracle BI Administration Tool
http://www.mandsconsulting.com/adding-an-alias-table-within-oracle-bi-administration-tool
Thanks
Subra -
Need help with self join query
Hello,
I have table A with the following data
oid parent_oid
10 4
4 2
2 2
12 6
6 6
parent_oid is the parent of oid. I'd like a query that shows the final parent of the oid. The result should show the following
oid final parent
10 2
4 2
2 2
12 6
6 6
I'm using Oracle 10g. I'm familiar with self joins, but that alone will not do the job. Thanks!Hi,
arizona9952 wrote:
... I'm familiar with self joins, but that alone will not do the job.You're absolutely right!
A 2-way self join would work for rows have no parent, or rows that are directly connected to their final ancestor (such as oid=4), but not for anything farther away.
A 3-way self-join would work for one more level away from the final row, but no more. That would be enough for the small set of sample data that you posted, but it would not work if you added a new row with parent_id=10.
An N-way self-join would work for up to N+1 levels, but no more.
You need something that can go any number of levels, such as CONNECT BY:
SELECT CONNECT_BY_ROOT oid AS oid
, parent_oid AS final_parent
FROM a
WHERE CONNECT_BY_ISLEAF = 1
CONNECT BY oid = PRIOR parent_oid
AND oid != parent_oid
;Edited by: Frank Kulash on Feb 22, 2010 7:09 PM
Upon sober reflection, I think that a Top-Down query, like the one below, would be more efficient than a Bottom-Up query, like the one above:
SELECT oid
, CONNECT_BY_ROOT parent_oid AS final_parent
FROM a
START WITH parent_oid = oid
CONNECT BY parent_oid = PRIOR oid
AND oid != PRIOR oid
; -
Student learning, self join
Our teacher isnt very clear on self joins, and searching brings up too complicated of info for someone like myself just learning, so i was hoping you guys could help out. Table below:
SQL> select * from grade;
STD_CODE GR_LNAME GR_FNAME GR_T1 GR_T2 GR_HW GR_PR
S1 Jordan Michael 95 80 98 90
S2 Barkley Charles 60 100 100 60
S3 Johnson Magic 88 98 96 98
S4 Williams Serena 92 92 92 92
S5 Duncan Tim 94 90 94 96
S6 Graff Steffi 85 84 83 72
S7 Navratilova Martina 91 88 94 95
Need to solve this question:
Create a view that will display names of students that have identical average grade (average of gr_t1, t2, hw, and pr)
I learned that we needed to create a self join to sniff out the averages that were the same.
I am pretty clueless on the logic of self join, but I made an attempt:
CREATE VIEW SAME_GRADES2 AS SELECT S.STD_FNAME, S.STD_LNAME, (GR_T1+GR_T2+GR_HW+GR_PR)/4 AVG_GR
FROM GRADE S, GRADE G
WHERE S.AVG_GR=G.AVG_GR;
i get an error on this: ORA-00904: "G"."AVG_GR": invalid identifier
No idea. Any help for a newbie?sorry that last one was a syntax error... here is the result (obviously not what i need)
GR_FNAME GR_LNAME AVG_GR
Michael________Jordan____________90.75
Charles________Barkley____________80
Magic________Johnson____________95
Martina________Navratilova_________92
Serena________Williams____________92
Tim________Duncan______________93.5
Steffi________Graff _______________81
Martina________Navratilova _________92
Serena________Williams____________92
9 rows selected.
code:
CREATE VIEW SAME_GRADES2 AS SELECT S.GR_FNAME, S.GR_LNAME, (S.GR_T1+S.GR_T2+S.GR_HW+S.GR_PR)/4 AVG_GR
FROM GRADE S, GRADE G GROUP BY S.GR_FNAME
WHERE (S.GR_T1+S.GR_T2+S.GR_HW+S.GR_PR)/4 = (G.GR_T1+G.GR_T2+G.GR_HW+G.GR_PR)/4;
Edited by: user10188205 on Oct 22, 2008 4:42 PM -
How to re-write this self join update using a CTE
I would like to improve my performance on this update statement and would like to try re-writing using a CTE:
UPDATE "usr_sessions" "a" SET "is_ended_at_trustable" = 't'
WHERE (
EXISTS (
SELECT 1
FROM "usr_sessions" "b"
WHERE "a"."ended_at" = "b"."started_at"
AND "a"."usr_space_id" = "b"."usr_space_id"
AND "a"."account_id" = "b"."account_id"
) ) AND "a"."is_ended_at_trustable" IS NULL
Any help is greatly appreciated! Open to other suggestions as well if there is a better way!If I understood your description correctly, here's a way to accomplish the same thing, while dodging the need for the self join. The update itself won't be any faster, but the overall query leading to the update will likely be faster sans self-join.
(If my interpretation wasn't exactly what you meant, tweak the "partition by" clause).
MERGE is generally considered better then UPDATE, but your particular update isn't at risk for the shortcomings of update (still, Merge is newer, cooler, and more trustworthy).
Setup_Example_Data:
Declare @Usr_Sessions table (account_id int, usr_space_id int, is_ended_at_Trustable Char(1), started_at varchar(99), ended_at varchar(99))
Insert @Usr_Sessions
Select 1, 10, 't', 'A1', 'A1'
UNION ALL Select 2, 20, 'f', 'B1', 'B2'
UNION ALL Select 3, 30, NULL, 'C1', 'C1'
UNION ALL Select 4, 40, NULL, 'D1', 'D2'
UNION ALL Select 5, 50, NULL, 'E1', 'E2'
UNION ALL Select 5, 51, NULL, 'E3', 'E3'
UNION ALL Select 6, 61, NULL, 'F1', 'F2'
UNION ALL Select 6, 62, 't', 'F3', 'F3'
UNION ALL Select 6, 62, 'f', 'F4', 'F4'
Select 'Before', * from @Usr_Sessions
OP_Query:
BEGIN TRAN
UPDATE A SET is_ended_at_trustable = 't' from @usr_Sessions A-- Select * from @Usr_Sessions "a" --
WHERE (
EXISTS (
SELECT 1
FROM @usr_sessions "b"
WHERE "a"."ended_at" = "b"."started_at"
AND "a"."usr_space_id" = "b"."usr_space_id"
AND "a"."account_id" = "b"."account_id"
) ) AND "a"."is_ended_at_trustable" IS NULL
Select 'After 1', * from @Usr_Sessions
ROLLBACK TRAN /* Just to reset test data to original form, so second query below runs against original data */
Dodge_Self_Join:
With X as
Select *
, count(case when started_at = ended_at and is_ended_at_trustable is null then 'x' else null end)
over(partition by account_id, usr_space_id) as Updatable
From @Usr_Sessions
Update X
set is_ended_at_Trustable = 'T'
where Updatable > 0 -- EDIT -- fixed error, previously said "updatable = 1"
Select 'After 2', * from @Usr_Sessions
Maybe you are looking for
-
How can I display a value in a form field
I have tried using the set_value_as_string and set_value functions functions in query buttons PL/SQL event handler .But then the value is not getting displayed in the field. Could I get any help on this ........
-
I just spent over $4,000.00 for a brand new Mac Pro, 8-Core, Two 2.4GHz Quad-Core Intel Xeon "Westmere" processors. I'm still at the point where I may not be blaming this new computer 100%, but when you upgrade to this extent you expect everything to
-
when I click on Adobe After Effects 7.0, it asks if I want to run the software, and I click on "RUN," then a window opens and immediately closes and another one comes up, "Adobe AD 7.0 Installer Information" w/ exclamation mark in yellow triangle say
-
Hello there are different opinions on how to get the swap space on Solaris. some say: swap -s and the space= used + available others say swap -l (donno how they get the swap size) other say 'top' command others say using format command (in print sub-
-
Linksys BEFW11S4 v2 with a NETGEAR RP614 v3
I have a Linksys BEFW11S4 v2 with a NETGEAR RP614 v3. My setup is my Cable modem, into my NETGEAR RP614 v3, and everything works perfectly fine. I got a Linksys BEFW11S4 v2 and updated its firmware from the linksys server. Now, I'm hooking the Linksy