ASH: IN_PARSE and IN_SQL_EXECUTION simultaneously? - oracle

Can someone please point me towards documentation on the V$ACTIVE_SESSION_HISTORY columns IN_PARSE, IN_HARD_PARSE and IN_SQL_EXECUTION?
Version is 11.2.0.3
I have a TMPDLT-Query of an aggregate-mview fast-refresh (http://www.oaktable.net/content/fast-refresh-aggregate-only-materialized-views-%E2%80%93-introduction) that stalls other queries via a library cache lock for over 20 minutes 2 out of 7 days.
My question is: This TMPDLT-Query in ASH shows as IN_PARSE, IN_HARD_PARSE and IN_SQL_EXECUTION simultaneously for 20 minutes. According to SQL_PLAN_OPERATION and SQL_PLAN_OPTIONS it is doing a TABLE ACCESS FULL on the one MLOG-table it needs and according to file# it is in the UNDO table space.
My question: Have you ever seen sessions in ASH being IN_PARSE, IN_HARD_PARSE and IN_SQL_EXECUTION simultaneously? For 20 minutes? Is this normal? What does it mean? How can it full table scan already and still be in hard parse? Is this maybe related to the result_cache hint in the query? Or an effect of dynamic sampling?
Many thanks!
Update for BobC:
Plan of the query at the time the problem happened:
OTHER_XML as not fully shown above:
<other_xml>
<info type="db_version">11.2.0.3</info>
<info type="parse_schema">
<![CDATA["SYS"]]>
</info>
<info type="plan_hash">2866394291</info>
<info type="plan_hash_2">1823969956</info>
<outline_data>
<hint>
<![CDATA[IGNORE_OPTIM_EMBEDDED_HINTS]]>
</hint>
<hint>
<![CDATA[OPTIMIZER_FEATURES_ENABLE('11.2.0.3')]]>
</hint>
<hint>
<![CDATA[DB_VERSION('11.2.0.3')]]>
</hint>
<hint>
<![CDATA[OUTLINE_LEAF(#"SEL$335DD26A")]]>
</hint>
<hint>
<![CDATA[MERGE(#"SEL$3")]]>
</hint>
<hint>
<![CDATA[OUTLINE_LEAF(#"SEL$1")]]>
</hint>
<hint>
<![CDATA[OUTLINE_LEAF(#"SEL$ABDE6DFF")]]>
</hint>
<hint>
<![CDATA[MERGE(#"SEL$6")]]>
</hint>
<hint>
<![CDATA[OUTLINE_LEAF(#"SEL$4")]]>
</hint>
<hint>
<![CDATA[OUTLINE(#"SEL$2")]]>
</hint>
<hint>
<![CDATA[OUTLINE(#"SEL$3")]]>
</hint>
<hint>
<![CDATA[OUTLINE(#"SEL$5")]]>
</hint>
<hint>
<![CDATA[OUTLINE(#"SEL$6")]]>
</hint>
<hint>
<![CDATA[NO_ACCESS(#"SEL$4" "V4"#"SEL$4")]]>
</hint>
<hint>
<![CDATA[NO_ACCESS(#"SEL$ABDE6DFF" "DLT$"#"SEL$6")]]>
</hint>
<hint>
<![CDATA[NO_ACCESS(#"SEL$1" "MAS$"#"SEL$1")]]>
</hint>
<hint>
<![CDATA[FULL(#"SEL$335DD26A" "MAS$"#"SEL$3")]]>
</hint>
</outline_data>
SQL of the query (anonymized a bit):
WITH "TMPDLT$_XXXXXXXXXXXXXXXX" AS(
SELECT /*+ RESULT_CACHE(LIFETIME=SESSION) */
"MAS$"."RID$" "RID$",
"MAS$"."PARTITION_YYYY",
"MAS$"."ZZZZZZZZZ",
"MAS$"."KEY",
"MAS$"."VALUE",
decode("MAS$"."OLD_NEW$$",'N','I','D')"DML$$",
"MAS$"."OLD_NEW$$" "OLD_NEW$$",
"MAS$"."TIME$$" "TIME$$",
"MAS$"."DMLTYPE$$" "DMLTYPE$$"
FROM
(
SELECT
"MAS$".*,
MIN("MAS$"."SEQ$$")OVER(
PARTITION BY "MAS$"."RID$"
)"MINSEQ$$",
MAX("MAS$"."SEQ$$")OVER(
PARTITION BY "MAS$"."RID$"
)"MAXSEQ$$"
FROM
(
SELECT /*+ CARDINALITY(MAS$ 0) */
chartorowid("MAS$"."M_ROW$$")rid$,
"MAS$"."PARTITION_YYYY",
"MAS$"."ZZZZZZZZZ",
"MAS$"."KEY",
"MAS$"."VALUE",
decode("MAS$".old_new$$,'N','I','D')dml$$,
"MAS$"."DMLTYPE$$" "DMLTYPE$$",
"MAS$"."SEQUENCE$$" "SEQ$$",
"MAS$"."OLD_NEW$$" "OLD_NEW$$",
"MAS$"."SNAPTIME$$" "TIME$$"
FROM
"AAAAAAAAAAAAAA"."MLOG$_XXXXXXXXXXXXXXXX" "MAS$"
WHERE
"MAS$".snaptime$$ > :b_st0
)AS OF SNAPSHOT(:b_scn)"MAS$"
)"MAS$"
WHERE
((("MAS$"."OLD_NEW$$" = 'N')
AND("MAS$"."SEQ$$" = "MAS$"."MAXSEQ$$"))
OR(("MAS$"."OLD_NEW$$" IN(
'O',
'U'
))
AND("MAS$"."SEQ$$" = "MAS$"."MINSEQ$$")))
)
SELECT
CASE
WHEN ddt_1 = 'D'
AND ddt_2 = 'I' THEN
'U'
ELSE
ddt_1
END "DML$$",
MAX(mtime)"TIME$$"
FROM
(
SELECT
MIN(dd_type)OVER(
PARTITION BY rid,old_new
)ddt_1,
MAX(dd_type)OVER(
PARTITION BY rid,old_new
)ddt_2,
mtime
FROM
(
SELECT
"DLT$"."RID$" rid,
"DLT$"."DML$$" dd_type,
"DLT$"."TIME$$" mtime,
CASE
WHEN "DLT$"."DMLTYPE$$" = 'U'
AND "DLT$"."OLD_NEW$$" = 'N' THEN
'U'
ELSE
"DLT$"."OLD_NEW$$"
END old_new
FROM
"TMPDLT$_XXXXXXXXXXXXXXXX" "DLT$"
)v3
)v4
GROUP BY
CASE
WHEN ddt_1 = 'D'
AND ddt_2 = 'I' THEN
'U'
ELSE
ddt_1
END

Related

Combining Oracle xpath expressions

I'm running xpath on Oracle 11g.
Using the following code i'm trying to select the number element under the application element with #code="A2" or "U2". If no such #code attribute exists then I just want to select the first application element, with or without a #code attribute.
The 2 expressions (2nd one commented out) in the following code work individually for the 2 halves of the problem:
1) Where #code="A2" or "U2"
2) The first application
Can anyone please tell me how to combine these 2 expressions:
WITH et
AS (SELECT XMLType (
'<patent>
<applications>
<application code="U7">
<applicationId>
<number>351149</number>
</applicationId>
</application>
<application>
<applicationId>
<number>54632</number>
</applicationId>
</application>
<application code="A2">
<applicationId>
<number>DM090884</number>
</applicationId>
</application>
</applications>
</patent>
' ) xt
from dual
)
SELECT
ext.*
FROM
et,
XMLTABLE('/patent'
passing et.xt
Columns
APPLICATION_NUMBER VARCHAR (14)
PATH 'applications/application[(#code="A2" or #code="U2" )][1]/applicationId/number'
--PATH ' applications/application[1]/applicationId/number'
) ext
I think the logic can be - return a or return b if there is no a
a | b[not(../a)]
In your case is something as:
applications/
(application[(#code="A2" or #code="U2")] |
application[not(../application[#code="A2" or #code="U2"])])
[1]/applicationId/number
I have splitted the line for readability

case statement with in case statement

I am not sure what kind of statement I need in order for what i need to do. I would like to add a case statement after the case statement i currently have. Bellow is the case statement.
CASE
WHEN LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO')
THEN 'GAS'
WHEN LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC')
THEN 'ELECTRIC'
ELSE 'MISSING'
END AS TYPE,
I would like to say if EXT_Distworktype is GC or GT and the TYPE is ELECTRIC,
GIVE ME TYPE THAT SAYS WRONG POLYGON OR IF THE EXT_DISTWORKTYPE is EC, ES, or ET and Type is GAS then give me a TYPE that say WRONG POLYGON.
here is my full query:
SELECT WO.WONUM AS "Work Order",
WO.LOCATION AS "Location",
WO.STATUS AS "Status",
WO.DESCRIPTION,
WO.ACTFINISH AS "Actual Finish",
WO.PARENT AS "Parent WO",
WO.WORKTYPE AS "Work Type",
CASE
WHEN WO.REPORTDATE IS NULL
THEN TO_DATE('29-JAN-15 00:00:00', 'DD-MON-YY HH24:MI:SS')
ELSE WO.REPORTDATE
END AS "Reported Date",
WO.SITEID AS "Site",
CASE
WHEN WO.ACTFINISH IS NULL
THEN GREATEST(TRUNC(TO_DATE(SysDate, 'DD-MON-YY HH24:MI:SS') - TO_DATE(WO.REPORTDATE, 'DD-MON-YY HH24:MI:SS'), 6), 0)
WHEN WO.STATUS NOT IN ('COMP')
THEN GREATEST(TRUNC(TO_DATE(SysDate, 'DD-MON-YY HH24:MI:SS') - TO_DATE(WO.REPORTDATE, 'DD-MON-YY HH24:MI:SS'), 6), 0)
WHEN WO.STATUS IN ('COMP')
THEN GREATEST(TRUNC(TO_DATE(WO.ACTFINISH, 'DD-MON-YY HH24:MI:SS') - TO_DATE(WO.REPORTDATE, 'DD-MON-YY HH24:MI:SS'), 6), 0)
END AS "Age",
WO.EXT_DISTWORKTYPE,
LOCOFF.EXT_OFFICE,
CASE
WHEN LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO')
THEN 'GAS'
WHEN LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC')
THEN 'ELECTRIC'
ELSE 'MISSING'
END AS TYPE,
LOCOFF.EXT_STATECODE,
WO.OWNERGROUP,
WO.EXT_JOBCODE
FROM LOCATIONS LOCOFF
RIGHT JOIN WORKORDER WO
ON WO.LOCATION = LOCOFF.LOCATION
WHERE WO.STATUS NOT IN ('CLOSE', 'WAIV', 'CAN', 'REJ', 'REVOKED')
AND LOCOFF.SITEID = 'OPS'
AND WO.EXT_JOBCODE NOT LIKE 'A%' AND WO.E`enter code here`XT_JOBCODE NOT LIKE 'B%'
AND WO.EXT_JOBCODE NOT IN ('K008','K009','I006','I007','I008');
You can nest case expressions if you only want to modify the existing column value (rather than adding a separate column with the 'wrong polygon' message):
CASE
WHEN LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO')
THEN
CASE
WHEN WO.EXT_DISTWORKTYPE IN ('EC', 'ES', 'ET')
THEN 'WRONG POLYGON'
ELSE 'GAS'
END
WHEN LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC')
THEN
CASE
WHEN WO.EXT_DISTWORKTYPE IN ('GC', 'GT')
THEN 'WRONG POLYGON'
ELSE 'ELECTRIC'
END
ELSE 'MISSING'
END AS TYPE,
Quick SQL Fiddle demos with the same made-up data; your original case and this nested case, which gives:
EXT EX TYPE
--- -- -------------
BOF GC GAS
CDA GT GAS
SPO ES WRONG POLYGON
CDC EC ELECTRIC
COC ET ELECTRIC
SPC GC WRONG POLYGON
This is a bit simpler than trying to do a second step that interprets the TYPE value, because you can't refer to a column alias in the same level of query; you would need to make your existing query into an inline view.
Will an extra case not do it?
CASE
WHEN WO.EXT_DISTWORKTYPE IN ('GC', 'GT')
AND LOCOFF.EXT_SRV_POLYGON IN ('CDC', 'COC', 'DAV', 'DPC', 'GRC', 'KEC', 'LCC', 'OTC', 'PAC', 'SAC', 'SPC') --electronic
THEN 'WRONG POLYGON'
CASE
WHEN WO.EXT_DISTWORKTYPE IN ('EC', 'ES', 'ET' )
AND LOCOFF.EXT_SRV_POLYGON IN ('BOF', 'CDA', 'COL', 'DAC', 'GOS', 'KEL', 'KLF', 'LAG', 'LEC', 'MED', 'PUM', 'RIT', 'ROS', 'SAN', 'SPO') --gas
THEN 'WRONG POLYGON'
ELSE 'MSSING'
END AS YOURANSWER,
You could do an case within a case
CASE
WHEN (TRUE) THEN
CASE WHEN (true) THEN 'a' ELSE 'b' END
ELSE
'A'
END

java.lang.IllegalArgumentException

I am executing a unit test and keep getting this error for the following code:
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing [SELECT t1 FROM Test1 t1, Test1Rel t1r, Type tp WHERE ( sysdate >= t1.sdate and sysdate <= t1.edate ) OR ( t1.sdate <= sysdate AND t1.edate is null ) AND t1r.Test1Id = t1.Test1Id AND t1.relTpId = tp.TypeId and t1.isActv = 1 AND t1r.isActv = 1 AND tp.shrtCode = :shrtCode AND t1r.custId = :custId].
[69, 163] The expression is not a valid conditional expression.
[164, 330] The query contains a malformed ending.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1512)
at com.etiya.mts.data.dao.newpac.dao.ProductOfferDAO.findTest1(ProductOfferDAO.java:821)
at com.etiya.mts.data.dao.newpac.service.ProductOfferService.findTest1(ProductOfferService.java:241)
at com.etiya.mts.rules.productoffering.Test1Controller.execute(Test1Controller.java:36)
at com.etiya.mts.ejb.BasketValidateBean.validateBasket(BasketValidateBean.java:108)
at com.etiya.mts.ejb.RuleManager.validateBasket(RuleManager.java:887)
at com.etiya.mts.ejb.BasketValidateBeanTest.validaBasketTest(BasketValidateBeanTest.java:210)
at com.etiya.mts.ejb.BasketValidateBeanTest.basketValidateBeanTest(BasketValidateBeanTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [SELECT t1 FROM Test1 t1, Test1Rel t1r, Type tp WHERE ( sysdate >= t1.sdate and sysdate <= t1.edate ) OR ( t1.sdate <= sysdate AND t1.edate is null ) AND t1r.Test1Id = t1.Test1Id AND t1.relTpId = tp.TypeId and t1.isActv = 1 AND t1r.isActv = 1 AND tp.shrtCode = :shrtCode AND t1r.custId = :custId].
[69, 163] The expression is not a valid conditional expression.
[164, 330] The query contains a malformed ending.
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:150)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:325)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:270)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:157)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:138)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:112)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:98)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:82)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1510)
... 31 more
String cqString = "SELECT t1 FROM Test1 t1, Test1Rel t1r, Type tp" +
" WHERE ( sysdate >= t1.sdate and sysdate <= t1.edate )" +
" OR ( t1.sdate <= sysdate AND t1.edate is null )" +
" AND t1r.Test1Id = t1.Test1Id" +
" AND t1.relTpId = tp.TypeId" +
" and t1.isActv = 1" +
" AND t1r.isActv = 1" +
" AND tp.shrtCode = :shrtCode" +
" AND t1r.custId = :custId";
Query cq = getEntityManager().createQuery(cqString);
cq.setParameter("shrtCode", ofrRuleTpShrtCode);
cq.setParameter("custId", custId);
If I change the code to this:
StringBuilder query = new StringBuilder();
query.append("SELECT * FROM Test1 t1,Test1_REL t1r,Type tp ");
query.append("WHERE (sysdate between t1.SDATE and t1.EDATE) OR (t1.sdate <= sysdate AND t1.edate is null) ");
query.append("AND t1r.CUST_OFR_RULE_ID=t1.CUST_OFR_RULE_ID ");
query.append("AND t1.REL_TP_ID=tp.GNL_TP_ID and t1.IS_ACTV=1 ");
query.append("AND t1r.IS_ACTV=1 AND tp.SHRT_CODE = ? AND t1r.CUST_ID = ? ");
Query cq = getEntityManager().createNativeQuery(query.toString());
cq.setParameter(1, shrtCode);
cq.setParameter(2, custId);
I don't get any exception. The strange thing is this code works in other environments, but not mine. It looks like a syntax error, but I keep getting this error in other parts of the code where createQuery is used.
I checked the maven's repository but it is up to date. Does any one have any opinion about this issue?
Edit:
I changed the eclipselink version from 2.4.0 to 2.5.0 but still getting the same error. Can it be something like a language setting of windows?
Which EclipseLink version are you using? You might be hitting this bug, it is resolved in version 2.4.3. Looks like the problem is with Hermes query parser.
I changed system language, locale and format information. It worked.

Linq to XML: Finding value of a specific element

I am new bee to "Linq" and "Linq to XML" concepts. I have the following xml tree
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>IWS</name>
<SSIDConfig>
<SSID>
<hex>496153</hex>
<name>ISL</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<MSM>
<security>
<authEncryption>
<authentication>WPA2PSK</authentication>
<encryption>AES</encryption>
<useOneX>false</useOneX>
</authEncryption>
<sharedKey>
<keyType>networkKey</keyType>
<protected>false</protected>
<keyMaterial>BFEBBEA9B0E78ECD671A8D35D96556A32E001B7524A1</keyMaterial>
</sharedKey>
</security>
</MSM>
</WLANProfile>
I was wondering how to retrieve the KeyMaterial element value using linq to xml?
I have tried to use the following code, but I get empty enumeration
var networkKey = from c in doc.Descendants("WLANProfile")
select (string)c.Element("keyMaterial").Value;
Any suggestions?
Two mistakes:
1.) keyMaterial is not a direct child of WLANProfile that's why you don't get any results (c.Elements will only look for a direct child)
2.) you need to use the specified namespace in the XML - otherwise no node will match
Both applied:
XNamespace xns = "http://www.microsoft.com/networking/WLAN/profile/v1";
var networkKey = (from c in doc.Descendants(xns + "keyMaterial")
select (string)c.Value).FirstOrDefault();
Somewhat shorter in dot notation if you know there is always going to be exactly one key:
string networkKey = xdoc.Descendants(xns + "keyMaterial").Single().Value;

SQL 'FOR XML' query

I am trying to write a SQL 'FOR XML' query that produces an XML block in a specific xml format. The query I have so far is close but I am having problems getting it produce the exact xml format that I need. I hoping someone on here can help me.
Using the following SQL, I populate the table against which, the SQL FOR XML query is run:
CREATE TABLE PerfTable
(
ID INT NOT NULL,
Name VARCHAR(500) NOT NULL,
P_Performance1 NUMERIC(10,2),
B_Performance1 NUMERIC(10,2),
P_Performance2 NUMERIC(10,2),
B_Performance2 NUMERIC(10,2),
P_Performance3 NUMERIC(10,2),
B_Performance3 NUMERIC(10,2)
);
insert PerfTable(id, Name, P_Performance1, B_Performance1, P_Performance2,
B_Performance2, P_Performance3, B_Performance3)
values (111, 'Item1', -0.111, -0.112, -0.121, -0.122, -0.131, -0.132)
insert PerfTable(id, Name, P_Performance1, B_Performance1, P_Performance2,
B_Performance2, P_Performance3, B_Performance3)
values (222, 'Item2', -0.211, -0.212, -0.221, -0.222, -0.231, -0.232)
insert PerfTable(id, Name, P_Performance1, B_Performance1, P_Performance2,
B_Performance2, P_Performance3, B_Performance3)
values (333, 'Item3', -0.311, -0.312, -0.321, -0.322, -0.331, -0.332)
SELECT TOP 9
id, Name,
period as "Period_Performance/#Period",
F_Perf as "Period_Performance/F_Perf",
B_Perf as "Period_Performance/B_Perf"
FROM
(SELECT pt.id, pt.Name,
pt.P_Performance1 ,
pt.B_Performance1,
'WTD' as Period1,
pt.P_Performance2 ,
pt.B_Performance2,
'MTD' as Period3,
pt.P_Performance3 ,
pt.B_Performance3,
'YTD' as Period2
FROM PerfTable pt) a
UNPIVOT
(F_Perf FOR F IN
(P_Performance1, P_Performance2, P_Performance3)
) AS Fund_unpvt
UNPIVOT
(B_Perf FOR B IN
(B_Performance1, B_Performance2, B_Performance3)
) AS bmk_unpvt
UNPIVOT
(period FOR periods IN
(Period1, Period2, Period3)
) AS period_unpvt
WHERE
(RIGHT(F, 1) = RIGHT(B, 1))
AND (RIGHT(F, 1) = RIGHT(periods, 1))
FOR XML PATH('Performance')
Then I run the following query:
SELECT
id, Name,
period as "Period_Performance/#Period",
F_Perf as "Period_Performance/F_Perf",
B_Perf as "Period_Performance/B_Perf"
FROM
(SELECT
pt.id,
pt.Name,
pt.P_Performance1 ,
pt.B_Performance1,
'WTD' as Period1,
pt.P_Performance2 ,
pt.B_Performance2,
'MTD' as Period3,
pt.P_Performance3 ,
pt.B_Performance3,
'YTD' as Period2
FROM PerfTable pt) a
UNPIVOT
(F_Perf FOR F IN
(P_Performance1,P_Performance2,P_Performance3)
) AS Fund_unpvt
UNPIVOT
(B_Perf FOR B IN
(B_Performance1,B_Performance2,B_Performance3)
) AS bmk_unpvt
UNPIVOT
(period FOR periods IN
(Period1,Period2, Period3)
) AS period_unpvt
WHERE
(RIGHT(F,1) = RIGHT(B,1))
AND (RIGHT(F,1) = RIGHT(periods,1))
FOR XML PATH('Performance')
This query produces the following XML (this xml may not display correctly on this webpage(?)):
<Performance>
<id>111</id>
<Name>Item1</Name>
<Period_Performance Period="WTD">
<F_Perf>-0.11</F_Perf>
<B_Perf>-0.11</B_Perf>
</Period_Performance>
</Performance>
<Performance>
<id>111</id>
<Name>Item1</Name>
<Period_Performance Period="YTD">
<F_Perf>-0.12</F_Perf>
<B_Perf>-0.12</B_Perf>
</Period_Performance>
</Performance>
<Performance>
<id>111</id>
<Name>Item1</Name>
<Period_Performance Period="MTD">
<F_Perf>-0.13</F_Perf>
<B_Perf>-0.13</B_Perf>
</Period_Performance>
</Performance>
<Performance>
<id>222</id>
<Name>Item2</Name>
<Period_Performance Period="WTD">
<F_Perf>-0.21</F_Perf>
<B_Perf>-0.21</B_Perf>
</Period_Performance>
</Performance>
<Performance>
<id>222</id>
<Name>Item2</Name>
<Period_Performance Period="YTD">
<F_Perf>-0.22</F_Perf>
<B_Perf>-0.22</B_Perf>
</Period_Performance>
</Performance>
<Performance>
<id>222</id>
<Name>Item2</Name>
<Period_Performance Period="MTD">
<F_Perf>-0.23</F_Perf>
<B_Perf>-0.23</B_Perf>
</Period_Performance>
</Performance>
<Performance>
<id>333</id>
<Name>Item3</Name>
<Period_Performance Period="WTD">
<F_Perf>-0.31</F_Perf>
<B_Perf>-0.31</B_Perf>
</Period_Performance>
</Performance>
<Performance>
<id>333</id>
<Name>Item3</Name>
<Period_Performance Period="YTD">
<F_Perf>-0.32</F_Perf>
<B_Perf>-0.32</B_Perf>
</Period_Performance>
</Performance>
<Performance>
<id>333</id>
<Name>Item3</Name>
<Period_Performance Period="MTD">
<F_Perf>-0.33</F_Perf>
<B_Perf>-0.33</B_Perf>
</Period_Performance>
</Performance>
This XML that I need to produce is below:
<Performance>
<id>1</id>
<Name>Item1</Name>
<Period_Performance Period="WTD">
<F_Perf>-0.11</F_Perf>
<B_Perf>-0.11</B_Perf>
</Period_Performance>
<Period_Performance Period="YTD">
<F_Perf>-0.12</F_Perf>
<B_Perf>-0.12</B_Perf>
</Period_Performance>
<Period_Performance Period="MTD">
<F_Perf>-0.13</F_Perf>
<B_Perf>-0.13</B_Perf>
</Period_Performance>
</Performance>
<Performance>
<id>2</id>
<Name>Item2</Name>
<Period_Performance Period="WTD">
<F_Perf>-0.21</F_Perf>
<B_Perf>-0.21</B_Perf>
</Period_Performance>
<Period_Performance Period="YTD">
<F_Perf>-0.22</F_Perf>
<B_Perf>-0.22</B_Perf>
</Period_Performance>
<Period_Performance Period="MTD">
<F_Perf>-0.23</F_Perf>
<B_Perf>-0.23</B_Perf>
</Period_Performance>
</Performance>
<Performance>
<id>3</id>
<Name>Item3</Name>
<Period_Performance Period="WTD">
<F_Perf>-0.31</F_Perf>
<B_Perf>-0.31</B_Perf>
</Period_Performance>
<Period_Performance Period="YTD">
<F_Perf>-0.32</F_Perf>
<B_Perf>-0.32</B_Perf>
</Period_Performance>
<Period_Performance Period="MTD">
<F_Perf>-0.33</F_Perf>
<B_Perf>-0.33</B_Perf>
</Period_Performance>
</Performance>
Any help to create the desired XML you can give, is greatly appreciated.
Thanks
Resolved.
Thanks Marc. I have been able to solve this issue, but what I needed was one Performance block per id, with repeating Period_Performance (WTD, MTD, YTD etc.) blocks within the Performance block. You might notice that in the 'before' xml, there is one Period_Performance block per Performance block. Thanks anyway. B

Resources