Pivoting question

I have been reading and experimenting with pivoting and found a nice description at:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::p11_question_id:766825833740
My question regards the following code:
scott@DEV816>
scott@DEV816> select job,
2 max( decode( deptno, 10, cnt, null ) ) dept_10,
3 max( decode( deptno, 20, cnt, null ) ) dept_20,
4 max( decode( deptno, 30, cnt, null ) ) dept_30,
5 max( decode( deptno, 40, cnt, null ) ) dept_40
6 from ( select job, deptno, count(*) cnt
7 from emp
8 group by job, deptno )
9 group by job
10 /
Why is the max function needed? It seems that the "decode() AS dept_10", and the rest of the lines, only yield a single response, and so I don't see why the max is needed. Thank you.

Hi,
apex wrote:
I have been reading and experimenting with pivoting and found a nice description at:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::p11_question_id:766825833740
My question regards the following code:
scott@DEV816>
scott@DEV816> select job,
2 max( decode( deptno, 10, cnt, null ) ) dept_10,
3 max( decode( deptno, 20, cnt, null ) ) dept_20,
4 max( decode( deptno, 30, cnt, null ) ) dept_30,
5 max( decode( deptno, 40, cnt, null ) ) dept_40
6 from ( select job, deptno, count(*) cnt
7 from emp
8 group by job, deptno )
9 group by job
10 /
Why is the max function needed? It seems that the "decode() AS dept_10", and the rest of the lines, only yield a single response, and so I don't see why the max is needed. Thank you.Try removing MAX and see what happens.
Remember the ABC's of GROUP BY:
When you use a GROUP BY clause and/or an aggregate fucntion, then every item in the SELECT clause must be:
(A) an <b>A</b>ggregate function,
(B) one of the "group <b>B</b>y" expressions,
(C) a <b>C</b>onstant, or
(D) something that <b>D</b>epends entirely on the above. (For example, if you "GROUP BY TRUNC(dt)", you can "SELECT TO_CHAR (TRUNC(dt), 'Mon-DD')").
In this case, you want to say "GROUP BY job" in the main query because that will combine the rows onto one row for each job. Given that you need to "GROUP BY job", how are you going to get a value of cnt in the output? Cnt is not one of the GROUP BY expressions, it's not a constant, and it's not dependent on anything else. The only way you can get cnt into the results is to use it in an aggregate function, like MAX. Since there will only be 1 non-NULL value in each group (if that many), it doesn't matter if you use MIN or MAX to get that value. Since it's a NUMBER, you could even use MEDIAN, SUM or AVG, if you really wanted to be cute.
By the way, there's another way to get those same results without uisng a sub-query:
SELECT       job
,       SUM (CASE WHEN deptno = 10 THEN 1 END)     AS dept_10
,       SUM (CASE WHEN deptno = 20 THEN 1 END)     AS dept_20
,       SUM (CASE WHEN deptno = 30 THEN 1 END)     AS dept_30
,       SUM (CASE WHEN deptno = 40 THEN 1 END)     AS dept_40
FROM       scott.emp
GROUP BY  job
;You could use COUNT instead of SUM, above, to show 0 instead of leaving a column empty.
Using the SELECT ... PIVOT feature that was introduced in Oracle 11, you need a sub-query, but only to remove the columns that play no role in the problem:
WITH     got_relevant_columns     AS
     SELECT     job
     ,     deptno
     FROM     scott.emp
SELECT     *
FROM     got_relevant_columns
PIVOT     (     SUM (1)
     FOR     deptno IN ( 10     AS dept_10
                        , 20     AS dept_20
                        , 30     AS dept_30
                        , 40     AS dept_40
;Edited by: Frank Kulash on Sep 29, 2011 11:29 AM

Similar Messages

  • Clip Mask and MovieClip pivot questions

    Two more beginner questions:
    1) What's the easiest way to invert a clipping mask for a
    MovieClip in ActionScript? I tried using a ColorTransformMatrix to
    invert the alpha of the mask MovieClip, but it didn't work (looks
    like the clip mask simply does nothing where there are no filled
    shapes). I would like to add a graphic everywhere EXCEPT within the
    area of an animated MovieClip, and due to various requirements I
    cannot simply place it behind that MovieClip.
    2) Is there a way to set the pivot of a MovieClip in relation
    to the animated drawings/frames? It looks like the "origin" of a
    movieclip is the lower left-most corner of all the bounding boxes
    of all the frames of animation in my MovieClip. So if I set the _x
    and _y of a MovieClip in ActionScript to 150,150 it places the
    lowerleft-hand corner there instead of what I would like to be
    logical center of the MovieClip (say, center of a wheelbase of a
    car, or center of the feet of a person, or center of mass of a
    rock). I am fine with dialing in an offset, but just wondering if
    there is a cleaner way to do it (esp. if I happen to add more
    animation frames or replace drawings and the bounding box changes
    size).
    Thanks,
    Ken

    Ah, I figured everything out.
    1) Actually, the ColorTransformMatrix does work, but I didn't
    realize that alpha was 0-255, not 0-1 (you'd think after being
    bitten by MovieClip scales being 100 = 100% instead of 1 = 100%,
    I'd have guessed, but...). So a ColorTransformMatrix of
    (1 0 0 0 0)
    (0 1 0 0 0)
    (0 0 1 0 0)
    (0 0 0 -1 255)
    applied to the mask will successfully invert it. Nice.
    2) Apparently I had previously messed up grabbing all the
    images and dragging them - I had forgotten to turn onion skinning
    to "onion all" so that I could actually see everything (all my
    images are all on top of each other so it wasn't obvious I didn't
    have everything visible). I had been selecting all the frames and
    dragging the images around in relation to the registration point,
    but only the last 3 frames were actually moving, and they happened
    to be a part of the animation I didn't bother triggering while
    testing. Oops.
    Thanks much for your help.
    Ken

  • Import Manager Partitioning/Pivot Question

    My source data has two fields (Part Number, Page Number).  There are multiple rows for each part number with a single value for page number.  I need to combine the page number values for all instances of each part number and import into MDM as a comma delimited value.
    For example:
    Part Number  |  Page Number
    A1 | 1
    A1 | 2
    I want to store in destination field "Page Numbers" as "1, 2" for single record where part number is "A1".
    I do not understand how to accomplish this with Import Manager.

    Hi ,
    I tried in one more case also but there was limitaion .In that case you will end up with more no. of delimiters or nulls
    You can try with same procedure but just define a clone field for partnumber and  create pivot table with the same constraints
    just swaaping with newly introduced field.
    After this just partiton all the new fields and then combine .
    the newly partition field treat as page number and  map to destination field and also map source part number with destination field.
    This is also an option with limitation of trailng or preceeding null .I think we can ignore that also for that we have to do some setting of ignoring nulls.
    Please try this  alos but if you are able to add new field than that is the best one.
    please revert back with your results.
    Thanks,
    Sudhanshu

  • Sql problem, pivot question?

    Hi guys,
    I have an interesting problem and do not manage to find a sollution.
    I need to create a report, and we call it variance/trending report. let me explain.
    I have a table that holds all employess with their salaries, every month.
    currenly my selects gets all employess with their salaries from current and previous month. and the result looks like:
    Employee_iD| Salary| month
    1          |500   |   C
    2          |100   |   P
    1          |650   |   P
    3          |100   |   p
    3           |180   |   C
    how can I do my select so I can see the result, in different columns, in a manner in which I can compare the salaries between them (from current and previous month):
    Employe_iD| current_month| previous_month | variance
    1        |500                 | 650                 | -150
    2        |                      | 100                 |
    3        |180                 |100                   |80you can see that C, measn current month, and P measn previous month.
    I think I will have data in my select for both, previous and current. If not, I will treat it as zero.
    I gave a simple exemple . My select is a little more complex.
    any help is appreciated.
    Thanks

    May be..
    SQL> WITH tbl AS (SELECT 1 empid,500 sal,'C' mon FROM DUAL UNION ALL
      2               SELECT 2 empid,100 sal,'P' mon FROM DUAL UNION ALL
      3               SELECT 1 empid,650 sal,'P' mon FROM DUAL UNION ALL
      4               SELECT 3 empid,100 sal,'P' mon FROM DUAL UNION ALL
      5               SELECT 3 empid,180 sal,'C' mon FROM DUAL
      6               )
      7  SELECT empid,NVL(MAX(DECODE(mon,'P',sal)),0) previous_sal
      8              ,NVL(MAX(DECODE(mon,'C',sal)),0) current_sal
      9              ,NVL(MAX(DECODE(mon,'P',sal)),0)- NVL(MAX(DECODE(mon,'C',sal)),0)
    10                var
    11  FROM tbl
    12  GROUP BY empid          
    13               ;
         EMPID PREVIOUS_SAL CURRENT_SAL        VAR
             1          650         500        150
             2          100           0        100
             3          100         180        -80

  • Power pivot report freeze while scrolling

    when scrolling the power pivot reports that are long the excel window freezes, the field list turns white, there's no data loss though, I can click the close button and it will give me the option to save the data if I haven't saved anything, they I go in
    the task manager and end task any running excel processes and it would start working again, this happens on multiple computer in the orgs (with different os windows 7, 8.1 server 2012 r2).

    Excel 32-bit or 64-bit? And how large is the workbook?
    If 32-bit, it's possible that it is facing memory issues. Try 64-bit if you can and see if you have better results.
    Regards, Avi
    www.powerpivotpro.com
    Wiki:How to ask a Power Pivot Question to get a prompt, accurate and helpful response

  • Power Pivot 2013 Using SharePoint 2013 List as Source Data

    Background: On a SharePoint 2013 site, there are lists that have the exact same columns but due to their size I had to create a different list for each year.
    These are essentially my data lists or tables that end users enter data into. These lists have look up columns in them, referencing columns from look up lists or tables located on the same site.
    Goals:
    1) Create a Power Pivot Gallery on SharePoint 2013 that is able to leverage the "manage data refresh" feature using the SharePoint 2013 List data that is on the same SharePoint 2013 site.  
    2) Combine all the data lists/tables into one table once in Power Pivot 2013. The Look Up lists/tables will remain separate.
    Challenge: Being able to merge or do a union query to the "data lists" and still maintain the ability to leverage the "manage data refresh" feature. 
    This is a tactical, quick fix solution, and I
    cannot use SQL Server and am operating under the assumption that I do not have Access Services 2013 as an option.  
    Failed Attempt #1: What I already tried was linking the lists to an Access 2013 database and did a Union query from Power Pivot which merged the data from the different data tables
    as I wanted it to. Then I uploaded that Access Database to a document library on that same site, thinking that I could maintain the ability to leverage the "manage data refresh" feature but I was wrong.
    Any thoughts or suggestions? 
    Thanks in advance for any help!
    Cheers, Johnny
    Johnny

    You can connect to SharePoint Lists using Power Query (and of course Union multiple Lists and more using Pwer Query) - but Automatic refresh would not work with SharePoint. Power BI (Cloud service) allows auto refresh of Power Query, but not on SharePoint.
    Hopefully some day soon.
    Regards, Avi
    www.powerpivotpro.com
    Wiki:How to ask a Power Pivot Question to get a prompt, accurate and helpful response

  • Calculating Interest on principle amount only

    Hi all
    I have set up the interest calculation configuration but I can't get the interest calculation to only calculate interest on the principle amount and not calculate interest on interest.
    I have been able to do this in the selection parameters excluding the document type for interest - but I want to be able to specify interest calculation on the principle in the interest indicator configuration.
    Can anyone help as to whether this can be done and if so where I can specify this in the IMG.
    Thanks and regards
    Chris
    PS Points to be awarded

    +1 on Greg's note. This page has some pointers How to ask a Power Pivot Question to get
    a prompt, accurate and helpful response
    May be I am missing something, but the descriptive formula
    Payable interest = (Total interest accrued to date - Any amount already payable) x Repaid amount / Outstanding amount before repayment
    Does not seem to correlate with the formulas shown in the table.
    Repayment number      Interest accrued       Ratio of amounts     Payable interest
       1                                    r1                                  A1
                      r1 x A1
       2                                    r2                                  A2
                      r2 x (A2-r1A1)
    I expected the formula to be as below.
    Repayment number      Interest accrued       Ratio of amounts     Payable interest
       1                                    r1                                  A1
                      r1 x A1
       2                                    r2                                  A2
                      (r2 - r1A1) x A2
    Again, if you can provide a file with some sample values and expected results, that would clear things up.
    Regards, Avi
    www.powerpivotpro.com
    Wiki:How to ask a Power Pivot Question to get a prompt, accurate and helpful response

  • How to create a Power View bringing barchart and line chart together in same graph

    We are planning to create power view report that has bar and line charts on same graph. Could you please let us know whether it is feasible in Power View & how to add trend line in power view .
    Thanks,
    Sandeep.

    This is not possible with Power View. It is possible with Power BI (Online at PowerBI.com) See image below.
    Please vote or mark as answer if you found the response helpful.
    Regards, Avi
    www.powerpivotpro.com
    Wiki:How to ask a Power Pivot Question to get a prompt, accurate and helpful response

  • Code completion particular not working

    Hello,
    in my program there are serveral internal classes. Each class has its own include file. Code completion seems not working for internal classes in other include files.
    example:
    lcl_class1 in includec01   (first included)
         class-methods static1_1
         methods method1_1
    lcl_class2 in includec02   (second included)
         class-methods static1_1
         methods method1_1
    problem:
    While development in lcl_class2 there are no code completion for lcl_class1.
    No code completation for lcl_class1=>static1_1
    but if you declarate "data lo_myclass type ref to lcl_class1." code completion is working fine for public member methods and attributes in the same file.
    In Functiongroups all working fine.
    Best Regards
    Markus Bauernschmitt
    Und da hier eh die meisten deutschsprachig sind und mein Englisch grausam ist, nachfolgend die Erklärung nochmal auf deutsch.... ;-)
    In meinen Programmen findet die Codevervollständigung keine interne Klassen aus vorangegangenen include Dateien. Dadurch werden weder die Klassen selbst noch die statischen Methoden angezeigt. Gebe ich eine statische Methode ein und möchte die Hilfe anzeigen (F2) erscheint die Fehlermeldung "Codeinformationen sind nicht verfügbar".
    Dieses Problem tritt nicht auf, falls die Deklaration einer Variablen in der gleichen Include Datei erfolgt.
    Deklaration einer Variablen in der aktuellen Include Datei -> Dokumentation der öffentlichen (nicht statische) Methoden und Attribute funktioniert
    Zugriff auf öffentliche (statische oder nicht statische) Methoden und Attribute von globalen Variablen (deklariert in einer anderen Datei) funktioniert dagegen nicht.
    Es sind nur anscheinend nur Programme betroffen. Bei Funktionsgruppen (getestet am Beispiel "ldemo_cr_car_rental_screen") trat der Fehler nicht auf.

    Hi Dominik,
    thank you for your answer. The internal classes are already simple and lightweight. In my understanding of OO I keep classes private as possible and so I create this specific "one program helper classes" in the smallest reasonable scope.
    However, this is a architektur decission. But the pivotal question is: Why working this kind of code completion in function groups but not in programs.
    Some additional information.
    - Code completion working in "START-OF-SELECTION."
    - In Functiongroup includes (Testcase lcl_class1 show as public type and lcl_class2 show as local public class.
    Best regards
    Markus

  • Map visualizations in Power View

    As I am currently working on building interactive reports for my client using the Power BI Tools (PowerPivot/PowerView/PowerQuery) I have stumbled upon a visualization issue. 
    My client is very much in awe of the powerful visualizations possible in PowerMap and the interactive reporting of Powerview. The map used in PowerView however is somewhat simple and far less exciting. Is there a way to work around the situation of the standard
    PowerView map and get a better visualisation in the powerview reports? Think of integrating geocoded information such as coloured regions (which is possible in PowerMap) and not just dots/bubbles on the chart.
    Can anyone provide me with answer on this issue? Or is Microsoft working on an update to integrate a larger variety of maps into PowerView?
    Thanks.

    PowerBI (powerBI.com) has some new map visualization such as Filled Map (see image below) and more are likely coming. However, I do not think it will ever really compare with Power Map.
    Please vote or mark as answer if you found the response helpful.
    Regards, Avi
    www.powerpivotpro.com
    Wiki:How to ask a Power Pivot Question to get a prompt, accurate and helpful response

  • Power View crash after selecting one bar

    After the December update, 15.0.4675.1002, my power
    view sheets crash (they disappear) after selecting one bar (or column) in a
    graphic. 32 bits version in Spanish language.<o:p></o:p>
    Any suggestion or workaround?
    Best Regards.<o:p></o:p>

    You may not be the only one Matt Allington at Excelerator reported about this in his
    blog post (which seems to have been removed?!). You may be able to view a cached copy of his post
    here. At the bottom of the post it says:
    "Some users have reported that the O365 Update has broken previously working Power View reports. Given O365 updates are automatic, I guess this will need to be fixed with some priority."
    Regards, Avi
    www.powerpivotpro.com
    Wiki:How to ask a Power Pivot Question to get a prompt, accurate and helpful response

  • Converting Multiple Excel formulas to Single DAX formula

    Hi,
    I am trying to find a way in powerpivot of combining three 4 validation test columns into one column. The idea is that if the result in test 3 comes up with 'Exclude', I will filter it out of of a measure for a calculation I am doing. I have demonstated
    the calculations I am trying to combine in fields k-n, not sure if anyone can help arrive at this result in one single powerpivot calculation - thanks for looking at this if you can help.
    https://onedrive.live.com/redir?resid=B5D3C9D77C8E76FB!1344&authkey=!ABsaTOjjG2RAYHU&ithint=file%2cxlsx

    Grinch,
    Combining into a single column would be possible but not advised, due to the complexity of the logic you are using. I would recommend - keeping the intermediate columns as shown to get to final result. Then you can right click and choose "Hide from
    Client Tools" for all the intermediate columns so that they are not visible to the user (in the Pivot Table Field List).
    Please vote or mark as answer if you found the response helpful.
    Regards, Avi
    www.powerpivotpro.com
    Wiki:How to ask a Power Pivot Question to get a prompt, accurate and helpful response

  • Getting the first member of a cube dimension

    Hi,
    I need to get the first (and the unique) member of a cube dimension, but using CUBEMEMBER function seems be a wrong manner.
    Obviously, I don't know which is the first member.
    CUBEMEMBER("DataModel";"[ElaborationDates].[Dates]") returns ALL as also CUBEMEMBER("DataModel";"[ElaborationDates].[Dates].[All]").
    Any suggests to me, please?
    Thanks

    You can also look at
    CUBESET and CUBERANKEDMEMBER
    Regards, Avi
    www.powerpivotpro.com
    Wiki:How to ask a Power Pivot Question to get a prompt, accurate and helpful response

  • How to load from a data model located in SharePointOnline/PowerBI?

    PowerPivot has the option to load 'From Analysis Services Or PowerPivot'.
    The Table Import Wizard usually expects the full path of the workbook (including the data model) in SharePoint.
    However, 'http://myname.sharepoint.com/shared documents/myfile.xlsx' doesn't work.
    How can I access  my online data model from PowerPivot ?

    SharePoint needs to have Power Pivot Enabled. Speak to your SharePoint Admin if you have any questions about that. Once enabled, you would be able to create a Power Pivot Gallery - where you can upload your Power Pivot Models. Once uploaded the easiest way
    is to go to the Gallery and click on the "Excel" button next to the model. This would provide you a connection to the file on SharePoint. 
    See screenshot below OR watch this video for a demo: Power Pivot to SharePoint: Create thin reports connected to SharePoint Model
    Related Blog Article: Power Pivot to SSAS Tabular in less than 30 minutes (speaks about separating Model from Reports using SSAS or SharePoint)
    Regards, Avi
    www.powerpivotpro.com
    Wiki:How to ask a Power Pivot Question to get a prompt, accurate and helpful response

  • Options for BI without SharePoint Enterprise

    Hi,
    I'm trying to gather some information in the limitations on using SQL Server 2012 Enterprise BI without SharePoint Enterprise.  Specifically, BI distribution is of a concern.
    If I had the capabilities of SQL Enterprise but only had SharePoint Foundation, would I still be able to distribute BI PowerPivot reports and would users still be able to use the features of PowerPivot?  What would be the options of distribution with
    the said infrastructure?
    Also, what would I be missing out on.  I know there's a lot of information scattered on the net about this, so for brevity, I'd only ask for a summary of options and disadvantages.
    Any input would be most appreciated.
    Thanks

    ...Specifically, BI distribution is of a concern.
    If I had the capabilities of SQL Enterprise but only had SharePoint Foundation, would I still be able to distribute BI PowerPivot reports and would users still be able to use the features of PowerPivot?  What would be the options of distribution with the
    said infrastructure?
    Also, what would I be missing out on...
    SharePoint is not the only way to do enterprise level BI using Power Pivot. I wish Microsoft would bring some clarity to this. You have other options including...
    SSAS (SQL Server Analysis Services): My favorite by far. Real easy to setup (contrast with SharePoint, which is a bear). Heck even I could step through the install myself.
    Read about
    migrating from Excel Power Pivot Workbooks to SSAS
    You get some of the same benefits of a
    "Hub and Spoke" or "Core and Thin" model - Your Data Model (Power Pivot) is centralized on the central server, and you do not need to be emailing large Excel files around. You can have a host of "Thin" reports connected to
    this central model ("Spoke"s) - these can be Excel based, SSRS or another tool of your choice connected to your central cube hosted on SSAS. Thus BI distribution should be real easy. You can also setup autorefresh of your data model very easily.
    Power BI (mmm if you are not considering SharePoint, I doubt this is an option for you)
    Other options - e.g. Insight Center developed in conjunction
    with Microsoft
    What would you be missing out on
    Power View on SharePoint. If you are using Excel 2013, users would still have Power View inside of Excel - but I prefer the Power View on SharePoint
    Mmm..all the SharePoint functionality of course (file sharing, permissions, excel services...)
    Regards, Avi
    www.powerpivotpro.com
    Wiki:How to ask a Power Pivot Question to get a prompt, accurate and helpful response

Maybe you are looking for

  • Command Link : Backing bean setters/getters always invoked

    I am trying to understand how to the setters and getters of a request scope managed backing bean are invoked through the navigation flow when using a command link. It seems that no matter where the navigation rules redirects the page as a result of t

  • Creating dll in c++ and using labview to call it

    Greetings All, I have created a .dll file in visual C++ 2005 Express... I have attached both the c file and header file I used to create this dll in the .txt files cfile and header file. I have also included the .dll file itself so that you can use t

  • To read data in a file and to post data in a graph

    hello, I have a textfile and I want to read the data and to post them in a graph. In my textfile, I have 3 column and each column represents a curve. Thank you.

  • Video playback from PB17 to Sony TV firewire port ?

    I'm wondering if it's possible to get video playback from my PowerBook 17 to a Sony HDTV firewire port ? I can't see in prefs where you can do this. Is there some software that enables this? I tried to select firewire playback from Final Cut Pro even

  • What is the reason for this error?

    java.lang.RuntimeException: Didn't find TagInfo for tag:strutsContent Errors found in C:\bea8.1\user_projects\appReview\appReviewProject\framework\skeletons\default\desktop.jsp: Error at line 1 column 1: Description: Package com.bea.portlet contains