XML PIVOT QUERY QUESTIONS
I'm new to PIVOT queries. I was making reference to the discussion that follows, but ran into a problem.
When I added the XML to format data, I started to have NULL values rather than what I was looking for.
WITH (AS PIVOT_DATA)
SELECT * FROM)
SELECT THE REGION, FCST_PERIOD, PRIME_PART, FINAL_FORECAST
OF XYZ WHERE FCST_PERIOD > = last_day (trunc (sysdate)) + 1 and FCST_PERIOD < = last_day (add_months(sysdate,12)) AND PRIME_PART IN ('BLAH')
)
PIVOT XML (SUM (FINAL_FORECAST) FOR FCST_PERIOD IN (SELECT DISTINCT FCST_PERIOD OF XYZ))
)
SELECT PRIME_PART, REGION,
EXTRACTVALUE(FCST_PERIOD_XML,'/PIVOTSET/ITEM[1]/COLUMN[2]') FCST_PERIOD1,
EXTRACTVALUE(FCST_PERIOD_XML,'/PIVOTSET/ITEM[2]/COLUMN[2]') FCST_PERIOD2,
EXTRACTVALUE(FCST_PERIOD_XML,'/PIVOTSET/ITEM[3]/COLUMN[2]') FCST_PERIOD3
OF PIVOT_DATA;
RESULTS IN:
PRIME_PART | REGION | FCST_PERIOD1 | FCST_PERIOD2 | FCST_PERIOD3 |
BLA | MIDWEST | (NULL) | (NULL) | (NULL) |
BLA | WEST-NV | (NULL) | (NULL) | (NULL) |
BLA | SOUTH-EAST | (NULL) | (NULL) | (NULL) |
BLA | WEST-CA | (NULL) | (NULL) | (NULL) |
BLA | SOUTHWEST | (NULL) | (NULL) | (NULL) |
BLA | EAST | (NULL) | (NULL) | (NULL) |
The second part of my question is how would I do a group when you use a pivot query. When I finished, I've has several parts and would like to group by REGION.
Thank you!
Names of elements and attributes in XML are case-sensitive.
Try with:
EXTRACTVALUE (FCST_PERIOD_XML, ' / PivotSet/item [1] /column [2]') FCST_PERIOD1
There is also a good chance that EXTRACTVALUE is frowned upon in your version. Instead, use XMLQuery or XMLTable.
That being said, using XML PIVOT just to grind it later into a number known columns is pretty useless.
The same can be achieved much more efficiently with regular PIVOT operator (and if necessary analytical function).
Post some examples of data in the table of your database and your final result expected to get useful assistance.
Tags: Database
Similar Questions
-
I have a query in Oracle 11:
Resultselect to_char(da,'DY-DD') days from ( select rownum -1 + to_date('2012-10-15','yyyy-mm-dd') da, l from (select level l from dual connect by level <= to_date('2012-10-21','yyyy-mm-dd') - to_date('2012-10-15','yyyy-mm-dd')+1) order by 1)
What I need which is present in a row instead of a column:"DAYS" MON-15 TUE-16 WED-17 THU-18 FRI-19 SAT-20 SUN-21
This should be done with pivot requests? Any suggestion please!MON-15 TUE-16 WED-17 THU-18 FRI-19 SAT-20 SUN-21
Thanks in advance.Hello
user564819 wrote:
I need in 7 different columns.
Oracle version is 11.2.0.1.0. Could you give an example of how to do this with pivot query?Here's one way:
WITH all_days AS ( SELECT LEVEL AS n , TO_CHAR ( TO_DATE ('2012-10-15','yyyy-mm-dd') + LEVEL - 1 , 'DY-DD' ) AS dy_dd FROM dual CONNECT BY LEVEL <= TO_DATE ('2012-10-21','yyyy-mm-dd') + 1 - TO_DATE ('2012-10-15','yyyy-mm-dd') ) SELECT * FROM all_days PIVOT ( MIN (dy_dd) FOR n IN ( 1 AS col_1 , 2 AS col_2 , 3 AS col_3 , 4 AS col_4 , 5 AS col_5 , 6 AS col_6 , 7 AS col_7 , 8 AS col_8 , 9 AS col_9 , 10 AS col_10 ) ) ;
This always displays exactly 10 columns.
If the subquery produces less than 10 dates, then the last columns will be NULL.
If the subquery produces more than 10 dates, will be not shown the 11th (and later versions).There is nothing special about the number 10; You can have any number of columns you want. The number of columns and their names is fixed when you write the query, regardless of what is in the subquery. If you want a dynamic number of columns, or names derived from data, see {message identifier: = 3527823}
-
Hi all
I wrote a pivot query but it is not giving me the expected output.
with test as ( select 'outgoing' type_of_call, 'local' sub_type_call, '1sec' pulse, 0.3 call_charge from dual union all select 'outgoing' type_of_call, 'STD' sub_type_call, '2sec' pulse, 1.5 call_charge from dual union all select 'outgoing' type_of_call, 'ISD' sub_type_call, '1sec' pulse, 4 call_charge from dual union all select 'SMS' type_of_call, 'local' sub_type_call, '1' pulse, 0.5 call_charge from dual union all select 'SMS' type_of_call, 'STD' sub_type_call, '1' pulse, 1 call_charge from dual union all select 'SMS' type_of_call, 'ISD' sub_type_call, '1' pulse, 3 call_charge from dual ) select * from test pivot(count(type_of_call) for (sub_type_call) in ('local','STD','ISD') ); O/P should look like Type_of_call pulse_local charge_local Pulse_STD charge_STD pulse_ISD charge_isd outgoing 1sec 0.3 2sec 1.5 1sec 4 SMS 1 0.5 1 1 1 3
Is there something that I am missing in the query?
Thank you
SID
SQL> with test as ( select 'outgoing' type_of_call, 'local' sub_ty, '1sec' pulse, 0.3 call_charge from dual union all select 'outgoing' type_of_call, 'STD' sub_ty, '2sec' pulse, 1.5 call_charge from dual union all select 'outgoing' type_of_call, 'ISD' sub_ty, '1sec' pulse, 4 call_charge from dual union all select 'SMS' type_of_call, 'local' sub_ty, '1' pulse, 0.5 call_charge from dual union all select 'SMS' type_of_call, 'STD' sub_ty, '1' pulse, 1 call_charge from dual union all select 'SMS' type_of_call, 'ISD' sub_ty, '1' pulse, 3 call_charge from dual ) select * from test pivot(max(CALL_CHARGE) as CALL_CHARGE, max(pulse) as pulse for (sub_ty) in ('local' as local,'STD' as std,'ISD' as isd) ) order by type_of_call desc 2 3 4 5 6 7 8 9 10 11 12 13 / TYPE_OF_ LOCAL_CALL_CHARGE LOCA STD_CALL_CHARGE STD_ ISD_CALL_CHARGE ISD_ -------- ----------------- ---- --------------- ---- --------------- ---- outgoing .3 1sec 1.5 2sec 4 1sec SMS .5 1 1 1 3 1
-
need help on creating pivot query
SELECT * FROM TEST1
----------------
VALUE OF PERSON COMPUTERNAME
COMP1 ABC 3
COMP2 ABC 5
COMP1 CAD 3
COMP3 CAD 5
COMP2 TES 1
COMP1 TES 5
COMP3 ABC 2
myQuery
----------------------------
Select the link null, label, value1 COUNT (VALUE)
from 'test1 '.
CONTROL group PER PERSON
Results
---------
Link label value1
- ABC 3
- CAD 2
- TES 2
My requirement
---------------
can we have something like that out using the concept of pivot? If so can you share an example query pls.
Link label value1
-ABC ORDI1, COMP2, COMP3
-CAD COMP1, COMP2
-YOUR ORDI1, COMP3
Hello
Subhash C-Oracle wrote:
need help on creating pivot query
SELECT * FROM TEST1
----------------
VALUE OF PERSON COMPUTERNAME
COMP1 ABC 3
COMP2 ABC 5
COMP1 CAD 3
COMP3 CAD 5
COMP2 TES 1
COMP1 TES 5
COMP3 ABC 2
myQuery
----------------------------
Select the link null, label, value1 COUNT (VALUE)
from 'test1 '.
CONTROL group PER PERSON
Results
---------
Link label value1
- ABC 3
- CAD 2
- TES 2
My requirement
---------------
can we have something like that out using the concept of pivot? If so can you share an example query pls.
Link label value1
-ABC ORDI1, COMP2, COMP3
-CAD COMP1, COMP2
-YOUR ORDI1, COMP3
This sounds like a job for LISTAGG:
SELECT NULL AS link
label
LISTAGG (comp_name, ',')
THE Group (ORDER BY ComputerName) AS value1
OF test1
GROUP BY label
;
If you would care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.
Are you sure that the results you posted are what you want from data provided?
Is of the order of the elements in a significant list? In other words, when you say you want to get the results:
COMP1, COMP2
you'd be just as happy with
ORDI1, COMP2
? If the order is important, explains what this order.
-
Getting counties and divide by the sum in a single Pivot query
Hello
I have a pivot query that gives counties of all codes. I have also separated the number with the total in the same query.
Now, the output should have the counts / divided into sum multiplied by 100. Basically, the percentage.e.g col1 col2 col3 error_1 05-Jun-2012 parts error_1 05_june_2012 parts error_1 04_june_2012 consumables error_2 04_june_2012 consumables error_3 03_june_2012 parts . . .
output will be something like
Any suggestions please?error June-2012 May-2012 Feb-2012 .... headers error_1 60% 0 % 0% error_2 20% 0 0% error_3 20% 0% 0%
Thank you
Sunwith t1 as (select 'error_1' col1,trunc(sysdate) - 1 col2 from dual union all select 'error_1',trunc(sysdate) - 1 from dual union all select 'error_1',trunc(sysdate) + 30 from dual union all select 'error_2',trunc(sysdate) + 31 from dual union all select 'error_3',trunc(sysdate) - 2 from dual union all select 'error_3',trunc(sysdate) + 30 from dual union all select 'error_1',trunc(sysdate) - 3 from dual union all select 'error_2',trunc(sysdate) - 2 from dual union all select 'error_3',trunc(sysdate) + 30 from dual union all select 'error_4',trunc(sysdate) - 6 from dual ) select col1, count(case to_char(col2,'mon') when 'jul' then 1 end) one, 100 * ratio_to_report(count(case to_char(col2,'mon') when 'jul' then 1 end)) over () x, count(case to_char(col2,'mon') when 'jun' then 1 end) two, 100 * ratio_to_report(count(case to_char(col2,'mon') when 'jun' then 1 end)) over () y from t1 group by col1 order by col1
Concerning
Etbin
Edited by: Etbin on 8.6.2012 14:15
used month next to keep English and slovene "LUN" same ;) -
I have a table in the form of:
A, B AND C 1 2 3
B 4 5 6 D
A C 7 8 9
I want my XML in the form
< name FOO = 'A' >
< BAR name = "B" >
< name ZEE 'C' = >
< x >/< x > 1
< Y > 2 / < y >
< Z > 3 < /z >
< / ZEE >
< name ZEE = "D >.
< x > 4 / < x >
< Y > 5 / < y >
< Z > 6 / < z >
< / ZEE >
< / BAR >
< BAR name = "C" >
< name ZEE = 'A' >
< x > 7 / < x >
< Y > 8 / < y >
< Z > 9 / < z >
< / ZEE >
< / BAR >
< / FOO >
Is such a thing possible via SQL directly or I have to do a sort of XSL transformation? Can someone tell me something that speaks of how this is done?It should do so using bys group successive and enveloping the xml of lower level with parent elements.
with -- Get the distinct data sets by domain, datacenter, resource. res as( select akdomain, datacenter, akresource, xmlagg( xmlforest(currentload as "currentload", targetload as "targetload", capacity as "capacity") ) x from clusterload2 group by akdomain, datacenter, akresource ), -- Consolidate the resources by domain, datacenter. dc as( select akdomain, datacenter, xmlagg( xmlelement("resource",xmlattributes(akresource "name"),x) ) x from res group by akdomain, datacenter ), -- Consolidate the datacenters by domain. domain as( select akdomain, xmlagg( xmlelement("datacenter",xmlattributes(datacenter "name"),x) ) x from dc group by akdomain ) -- Wrap the xml with a domain. select xmlelement("load-object",xmlattributes(akdomain "domain"),x) x from domain /
Product:
123 124 125 223 224 225 1 1 1 -
I develop a connector and had some questions about the presentation of the petition. I tried to read the documentation and help, but saw no reference to these issues. I know that some of these things may not still have public API, but any help would be appreciated.
(1) is it possible to disable the entry blank for a ComboBox and set one of the values? I am referring to when you click on the button '+' on a query and selecting a comboBox field. I tried the 'enableEmptyEntry' attribute but could do not seem to work.
(2) is there a way to prevent several lines for the same domain? I'm trying to match a web interface that allows values separated by commas in certain areas and not others. Trying to decipher the intent of the user if they use multiple lines for the same field becomes confused.
(3) is there a way to disable 'Match All' or 'match any '?
Thank youAfter further review, the removal of the empty the box entry drop-down list on the query page is not possible.
One feature that we've added in the last release to support the case where the user needs to find records with a null or empty value for this combobox field. For example, finding bugs without test status. We have generic for all drop-down list boxes.
The 'enableEmptyEntry' attribute is intended to work with the attribute "emptyEntryValue" as a pair. The user can do something like this in def.xml:
enableEmptyEntry = 'true '.
"emptyEntryValue =""
Our framework posterin the drop-down list instead of empty or the default value . We recorded a feature request for this for a future version. Currently, writer connector is not a way to customize our application page, including the MatchAll, MatchAny option.
-
Progressive, Question on the fields of dynamic/DocID/Query Question of profiling
This may seem like a newb question but someone has a logical division of the BOM designation DocID?
In the course, we are responsible to use DocID = 1 and DocID = 2, but when/why would you use DocID = 3 or higher?
Any help would be greatly appreciated with the portion of dynamic fields/query string of the progressive profiling course.
Thank you.
The DocID is a complete example. You can actually assign the query string to be what you want. I recently use cmpID for the campaign ID, where I spent to map identity of the campaign and then sent the URL to redirect dynamically the campaign ID field. In my case, we worked with 10 different docs or goods, so we had to actually 10 unique codes. In this case we would have, for example cmpID = 483h76d; cmpID = 9j872hd, etc, etc. If you want to learn more about how you can use the query string values, see this post: how Progressive E10 profiling using the component of cloud - scroll down to step 6 and you'll see how to set up dynamic rules using query strings.
-
I have a request pivot within a region of APEX (pl sql function return sql query) report
declare v_term_pivot varchar2(500); PIVIN LONG; cursor xtoy is SELECT distinct d.JOB FROM emp d; v_query LONG; begin OPEN xtoy; LOOP FETCH xtoy into v_term_pivot; EXIT when xtoy%NOTFOUND; PIVIN:=PIVIN||''''||v_term_pivot||''' "'||substr(v_term_pivot,1,30)||'", '; END LOOP; v_query:='SELECT * FROM (SELECT x.ENAME, x.JOB, x.SAL FROM emp x) PIVOT ( SUM(x.SAL) FOR x.JOB IN ('||substr(PIVIN, 0, length(PIVIN)-1)||') ) order by 1;'; --dbms_output.put_line(v_query); RETURN v_query; end;
Get an error:
Failed to parse the SQL query:
ORA-01748: only here allowed simple column names
Can someone advice how can this be corrected?
I reworked the code to use the scott. EMP table.
I got the SQL Select function resulting and he ran in SQL Developer.
Syntax 1
You must remove aliases for tables of the PIVOT() section.
line 16 and 17 should be
SUM (SAL)
FOR USE IN (...)
Syntax 2
Line 11: you add comma and space (2 characters)
Line 17: you remove only 1 tank
Syntax 3 (problem semi)
Although the APEX works with it, I would abandon the semicolon (;) to the end.
To name the columns, you must create a function that returns the name of the report column.
Again, this must match the columns listed in the SELECT part
reminder of my warning:
Using a "SELECT *" and lack of "ORDER BY" in your cursor will cause you problems. (maybe not now, but it will.)
MK
-
Query does not work on my iPad (was: media query question)
I am a site for mobile/tablet/Office of coding and while my office and the iphone respond to the CSS, my ipad does not work. The changes that I do take the tablet in dreamweaver, but when they go live the ipad does not. Of course, my media query must be incorrect, but I can't understand the question. Any help is very appreciated.
http://gbetza.mydomain.com/WebService2/test/KathrynFee/2014/profile.html
Thank you.
Yes. It is inevitable because smart phones are getting bigger while tablets are smaller & more. He's always going to be some crossover. Think of it this way, when a Tablet is behaving as a mobile device, he takes over the mobile network. When the shelf behaves more like a desktop computer, you need the layout of office.
Similarly, a great smartphone in landscape mode is likely to resemble the layout of your tablet.
See the FluidGrid example below:
http://ALT-Web.com/FluidGrid/Fluid2.html
Office = 4-col layout
Tablet = 2-col layout
Mobile = 1-col layout
For purposes of illustration, I added a thick border of gold at the disposal of the Tablet only.
/ * Tablet Layout: 481px to 768px. Inherits the styles of: Mobile layout. */
@media only screen and (min-width: 481px) and (max-width: 768px) {}
.gridContainer {width: 92%; border: 16px solid gold}
}
Nancy O.
-
Why XML XML Simple query on Table Eval does not work?
I have a request that a simple select on a binary xml table on 11.2.0.1:
Now this xml document exists as a record in the binary xml table with evalId = "RickBlanchard9972" attributeSELECT xmlcast(xmlquery('$p/eval/@evalId' passing e.object_value AS "p" RETURNING CONTENT) AS VARCHAR2(30)) "EVAL ID" FROM eval e WHERE xmlexists('declare default element namespace "http://www.cigna.com/acme/domains/eval/2010/03"; /eval[@evalId=$eval_id]' PASSING object_value, 'RickBlanchard9972' as "eval_id");
Unfortunately, the query returns with a null value instead of; RickBlanchard9963<?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?><eval createdById="1957" acmeMemberId="37e6f05a-88dc-41e9-a8df-2a2ac6d822c9" category="external" eval_dt="2012-02-11T23:47:02.645Z" evalId="RickBlanchard9963" xmlns="http://www.cigna.com/acme/domains/eval/2010/03" xmlns:ns2="http://www.cigna.com/acme/domains/derived/fact/2010/03" xmlns:ns3="http://www.cigna.com/acme/domains/common/2010/03"> <derivedFacts> <ns2:derivedFact> <ns2:defId>888</ns2:defId> <ns2:defUrn>urn:coderunner:Medical:Definition:DerivedFact:52600:2</ns2:defUrn> <ns2:factSource>CCDR Member</ns2:factSource> <ns2:origInferred_dt>2012-02-11T23:47:02.645Z</ns2:origInferred_dt> <ns2:factValue> <ns2:type>string</ns2:type> <ns2:value>null</ns2:value> </ns2:factValue> </ns2:derivedFact> <ns2:derivedFact> <ns2:defId>777</ns2:defId> <ns2:defUrn>urn:coderunner:Medical:Definition:DerivedFact:52599:1</ns2:defUrn> <ns2:factSource>CCDR Member</ns2:factSource> <ns2:origInferred_dt>2012-02-11T23:47:02.645Z</ns2:origInferred_dt> <ns2:factValue> <ns2:type>string</ns2:type> <ns2:value>INT</ns2:value> </ns2:factValue> </ns2:derivedFact> <ns2:derivedFact> <ns2:defId>666</ns2:defId> <ns2:defUrn>urn:coderunner:Medical:Definition:DerivedFact:51400:1</ns2:defUrn> <ns2:factSource>CCDR Member</ns2:factSource> <ns2:origInferred_dt>2012-02-11T23:47:02.645Z</ns2:origInferred_dt> <ns2:factValue> <ns2:type>integer</ns2:type> <ns2:value>32</ns2:value> </ns2:factValue> </ns2:derivedFact> <ns2:derivedFact> <ns2:defId>555</ns2:defId> <ns2:defUrn>urn:coderunner:Medical:Definition:DerivedFact:52177:3</ns2:defUrn> <ns2:factSource>CCDR Member</ns2:factSource> <ns2:origInferred_dt>2012-02-11T23:47:02.645Z</ns2:origInferred_dt> <ns2:factValue> <ns2:type>string</ns2:type> <ns2:value>null</ns2:value> </ns2:factValue> </ns2:derivedFact> <ns2:derivedFact> <ns2:defId>444</ns2:defId> <ns2:defUrn>urn:coderunner:Medical:Definition:DerivedFact:52075:1</ns2:defUrn> <ns2:factSource>CCDR Member</ns2:factSource> <ns2:origInferred_dt>2012-02-11T23:47:02.645Z</ns2:origInferred_dt> <ns2:factValue> <ns2:type>string</ns2:type> <ns2:value>F</ns2:value> </ns2:factValue> </ns2:derivedFact> <ns2:derivedFact> <ns2:defId>333</ns2:defId> <ns2:defUrn>urn:coderunner:Medical:Definition:DerivedFact:51392:1</ns2:defUrn> <ns2:factSource>CCDR Member</ns2:factSource> <ns2:origInferred_dt>2012-02-11T23:47:02.645Z</ns2:origInferred_dt> <ns2:factValue> <ns2:type>boolean</ns2:type> <ns2:value>true</ns2:value> </ns2:factValue> </ns2:derivedFact> <ns2:derivedFact> <ns2:defId>222</ns2:defId> <ns2:defUrn>urn:coderunner:Medical:Definition:DerivedFact:52837:2</ns2:defUrn> <ns2:factSource>CCDR CaseManagement</ns2:factSource> <ns2:origInferred_dt>2012-02-11T23:47:02.645Z</ns2:origInferred_dt> <ns2:factValue> <ns2:type>boolean</ns2:type> <ns2:value>true</ns2:value> </ns2:factValue> </ns2:derivedFact> </derivedFacts> </eval>
Must be something obvious missing here...
Any help is appreciated.
Kind regards
Rick BlanchardThe sample document contains evalId = "RickBlanchard9963", not "RickBlanchard9972" but it's probably a typo?
The real problem is that you are missing from the default namespace declaration in XMLQuery.
-
Dynamic form: XML Data Import Question (I'm almost there..)
Niall was very useful gettig me at this point. Thank you very much!
I did all my updates and changes that have been suggested in the following form:
https://Acrobat.com/#d=t1uvlNlj4Yrh7zxtpbJiRg
My question is, when I import the XML data file, only a few of the shows of information upward?
Thought of back in all it took to get to this day, I don't know if I should not be only to count my blessings to be where I am? But, if someone (and knowing Niall is probably reading this - but I was too embarrassed to ask for more help), could help to tell me what I should do, I think it will be the last time I have to ask for help... at least on this form.
Thank you!!!Hi Rob,
In this case you don't need a data connection.
If you select a field and go to object > link tab, you will see a drop-down list to specify the data binding for this field. Currently it is set to 'None', which means that the field data will not be exported. Similarly, if you had an XML file with data, it would not be imported into the field.
Here is a screenshot of LC Designer ES2, which is slightly different for earlier versions, but the basic process is the same.
Go through each field and change 'No data binding' / 'None' to 'Use the name' / 'Normal '.
Then when you are proposing and import, data populates the fields.
Hope that helps,
Niall
-
BI Publisher data model / query question
Hello
I'm designing a report of BI Publisher where I need to show negative numbers in red. I'm perfectly fine with the help of the conditional formatting to do this (just using the default value is conditional formatting in the add-in in MS Word, that generates code like this:)
<? If: number(Cost_-_MTD) < 0? > <? attribute@InContext:color;' Red '? > <? end if? >
BUT...
I have columns returning who sometimes have a NULL value in them, and when they have the value NULL, it causes the foregoing to the pipe upwards. So I thought, 'I'll just add IfNull() in queries defined in the model data, and who will take care of the problem... "
that works - a little. Now, my problem is if I have to go back and change the query for some reason, I get an error "query cannot be parsed, please modify the query manually. I really don't want to do that, I wish that other developers do NOT have to manually change the queries.
So my question is - is it possible to add in the IfNull() directly in the query designer, so it should not be changed manually from that moment?
p.s. using IfNull OBIEE server as our data source.
Thanks in advance,
ScottI don't think there is a way to add the ifnull condition in the query designer. So, you will need to manually change the query...
In your model so you can add additional control like this:
<0?>...Thank you!
Published by: BIPuser on Sep 17, 2010 09:20
0?> -
Replace NULL values for PIVOT query
Hello
I'm working a table for sales, for certain values, when now columns, finishing with a null value. How can I handle these values 0 (zeros).
WITH (AS PIVOT_DATA)
SELECT S.ZONE_CODE, Z.ZONE_NAME, S.YEAR, S.PERIOD, S.SALES
OF STAT_TABLE_SALES ADV
AREAS OF JOIN Z ON S.COMP = Z.COMP AND S.ZONE_CODE = Z.ZONE_CODE
WHERE S.COMP = '001'
AND S.BRAND_CODE = '001'
)
SELECT *.
OF PIVOT_DATA
PIVOT)
SUM (SALES) FOR THE PERIOD (YEAR) TO ((20091),
(2009,2),
(2009,3),
(20094)
(2009,5),
(2009,6),
(2009,7),
(2009.8),
(2009,9),
(2009.10)
(2009,11),
(2009.12),
(2010,1),
(20102)
(2010,3),
(2010,4),
(2010,5),
(2010,6),
(2010.7))
)
ORDER BY DESC NULLS LAST 14;
This query returns the following:
COD_ZONA NOM_ZONA 2009_3 2009_2 2009_1
-------- ------------------------------ ---------------------- ---------------------- --------------------
01 YEDUSIJH. 1382367.75 1559943.27 1441279.64
02 C, ASKAK 711897.82 865854,05 1583232.3
ASDFG 03 130443.03 205409,84 178633.69
04 OSOIDSD 320118.32 439008,83 409251.18
05 ODFSDF 300908.21 276301,59 260188.53
06 CH 242749.65 325196,71 464938.9
SOA 07 610312.31 606312,93 754569.82
08 SAN 89426.8 81360,04 61649.27
09 YP 284487.79 328281,31 267210.85
10 TC 87043.28 158594,43 85195.8
11 BAGNN 76778.78 68180,76 118530.04
12 CRT 122023.7 143442,21 134744.85
13 ABC 209992.79 196477,03 185222.14
IDLIB 14
15 ARE 23870.41 4137,33 16660.53
* These are not all the columns and rows, but it is a piece of what it returns.
How can I replace the NULL values with 0 (zeros).Still not sure why nvl shouldn't meet your needs:
SQL> select ename, nvl (clerk_20, 0) clerk_20, nvl (sal_30, 0) sal_30 from emp pivot (sum (sal) for (job, deptno) in ( ('CLERK', 20) clerk_20, ('SALESMAN', 30) sal_30)) / ENAME CLERK_20 SAL_30 ---------- --------------- --------------- WARD 0 1250 JONES 0 0 TURNER 0 1500 ADAMS 1100 0 ALLEN 0 1600 SMITH 800 0 CLARK 0 0 KING 0 0 BLAKE 0 0 JAMES 0 0 FORD 0 0 SCOTT 0 0 MARTIN 0 1250 MILLER 0 0 14 rows selected.
-
All (running 11.2 OEL on 5),
I'm fighting figuring how to get the result I want to with the following data:
Here's what I would get. The column date 10 - Jun 10 - Jul is 'dynamic', and there will be a new column each month.CREATE TABLE REGION_LOOKUP ( REGION_ID NUMBER NOT NULL ENABLE, REGION VARCHAR2(5) NOT NULL ENABLE, PRIMARY KEY ("REGION_ID") ENABLE ) / CREATE TABLE IND_REVENUE ( ID NUMBER, IND_REV_DATE VARCHAR2(30), IND_REVENUE NUMBER, REGION_ID NUMBER, CONSTRAINT IND_REVENUE_PK PRIMARY KEY (ID) ENABLE ) / INSERT INTO REGION_LOOKUP (REGION_ID, REGION VALUES(1,'EMEA'); INSERT INTO REGION_LOOKUP (REGION_ID, REGION VALUES(2,'LAD'); INSERT INTO REGION_LOOKUP (REGION_ID, REGION VALUES(3,'APAC'); INSERT INTO REGION_LOOKUP (REGION_ID, REGION VALUES(4,'NAS'); INSERT INTO REGION_LOOKUP (REGION_ID, REGION VALUES(5,'JAPAN'); / INSERT INTO IND_REVENUE VALUES(1,'10-Jun',73.10,4); INSERT INTO IND_REVENUE VALUES(2,'10-Jul',49.30,4); INSERT INTO IND_REVENUE VALUES(3,'10-Jun',3.20,2); INSERT INTO IND_REVENUE VALUES(4,'10-Jul',0.30,2); INSERT INTO IND_REVENUE VALUES(5,'10-Jun',28.60,3); INSERT INTO IND_REVENUE VALUES(6,'10-Jul',12.40,3); INSERT INTO IND_REVENUE VALUES(7,'10-Jun',64.00,1); INSERT INTO IND_REVENUE VALUES(8,'10-Jul',19.80,1); INSERT INTO IND_REVENUE VALUES(9,'10-Jun',6.60,5); INSERT INTO IND_REVENUE VALUES(10,'10-Jul',4.70,5); /
I think something like that (I'm stuck) but of course, this does not at all and is also hardcoded regarding columns.Region 10-Jun 10-Jul Total APAC $28.6 $12.4 $41.0 EMEA $64.0 $19.8 $83.8 JAPAN $6.6 $4.7 $11.3 LAD $3.2 $0.3 $3.5 NAS $73.1 $49.3 $122.4 Total $175.5 $86.5 $262.0
All great ideas?select rn, 10-Jun, 10-Jul, (10-Jun + 10-Jul) as Total from (select RL.REGION_NAME as rn, RL.REGION as re, IR.IND_REVENUE as rev from REGION_LOOKUP RL, IND_REVENUE IR where IR.region_id = RN.region_id) pivot (SUM(rev) for rn in ('10-Jun' as 10-Jun, '10-Jul' as 10-Jul))
See you soon,.
Andy
Fixed the instructions insert, sorry about that. Copy and paste before the coffee is not good.Hi, Andy.
A Tael says:
A nice solution,Thanks, but what solution do you mean? There were at least 4 different solutions on this page.
but is there a way to avoid the queue to a file?
Do you mean the dynamic SQL solution? Yes, the coil is just a way to make the dynamic SQL statements.
In SQL * more you can write the variable part of the query to a substitution variable (see below).
In PL/SQL, you can assemble a select in VARCHAR2 variable.PROMPT ========== 2. Dynamic Pivot using Substitution Variable ========== -- ***** Preliminary Query: ***** COLUMN sql_txt_col NEW_VALUE sql_txt WITH all_jobs AS ( SELECT DISTINCT job , DENSE_RANK () OVER (ORDER BY job) AS r_num FROM scott.emp ) SELECT SYS_CONNECT_BY_PATH ( job || ''' THEN 1 END) AS ' || job || '_CNT' || CHR (10) -- Newline, for legibility only , ', COUNT (CASE WHEN job = ''' -- Delimiter, goes before each entry ) AS sql_txt_col FROM all_jobs WHERE CONNECT_BY_ISLEAF = 1 START WITH r_num = 1 CONNECT BY r_num = PRIOR r_num + 1 ; -- ***** Main Query ***** SELECT deptno &sql_txt -- Ends with newline, so occupy that line FROM scott.emp GROUP BY deptno ORDER BY deptno ; /* EXPLANATION: Using a substitution variable is very similar to using a script. The main difference is that the output of the preliminary query has to go into one row. This is done with SYS_CONNECT_BY_PATH, and getting that requires that the jobs be numbered with consecutive integers, 1, 2, 3, ... which we get from DENSE_RANK. The NEWLINE character was added just to make the output line easier to read during debugging. */
Maybe you are looking for
-
How can I add a link to a Pages document I want to export to PDF?
I want to add links to various web sites in a Pages 5.6.2 document (2573) I export and publish to PDF on my web site. How to go about creating a hyperlink in this version of Pages? the old version of Pages contained an "Inspector" who allowed me to
-
My Toshiba laptop keeps freezing
I bought a Toshiba laptop a few months ago, but it keeps freezing. I use internet for the most part not sure if it's my internet or computer. Should I just bring it back to the shop?Thank you very much.
-
I recently bought an unused, sealed box, any new version of iWork 9.0.3 for my iMac (details below). A few years ago I tried the app the iWork trial and helped the 30 trial period days without purchasing a license. I installed the new version yesterd
-
HP Pavilion 15-p077tx: error CTO
Restoration incomplete Component: Windows 8.1 tools of diagnosis Preinstall - TDCCalendar: FactoryUpdateError: brand name is not validNext step: Please check laptop model via EEPROM-------------------------------------------------------------- [ 7:07
-
How to disable the vibrations during the (de-) connection phone?
Hallo, I am very often in the distance of the phone, when SW3 and phone are connection/disconnection again and again... [It is not a problem, the BT range is given by hw:], but I want to disable notifications of vibrations on smartwatch (one and two