Pivot table wihtout Aggregate Functions ( Urgent Please. )

I am trying to select date and time. But having a hard time since I dont have any aggregate function that is needed. Just pivot the rows to columns.
IN sql 
stage
starttime
endtime
1
1/22/2011 15:31
1/22/2011 15:32
2
1/22/2011 15:33
1/22/2011 15:34
3
1/22/2011 15:35
1/22/2011 15:36
Required 
starttime_s1
endtime_s1
starttime__s2
endtime_s2
starttime_s3
endtime_s3
1/22/2011 15:31
1/22/2011 15:32
1/22/2011 15:33
1/22/2011 15:34
1/22/2011 15:35
1/22/2011 15:36

What is it that you're trying to accomplish with this?
The reason I ask is that pivoting data is a display function and therefore best left to the application or reporting tier.
If you're going to insist on "driving screws with a hammer" and really want to do this in SQL, the next question is, Is there a set number of "Stages" that will exist? Is it always going to be 3 stages and only 3 stages?
If the answer is YES, there will be a fixed number of stages, then it's a simple thing to do with either a PIVOT or CASE statements. (my personal preference is to use case statements, especially if you're pivoting 2 or more columns)
If the answer is NO, the number is stages can vary... You're left with a Dynamic PIVOT. Dynamic Pivots are a bit more complex to write but there is enough sample code out on the interwebs that you won't have a problem finding the correct syntax. 
The real problem with a Dynamic Pivot query is the fact that it's pretty much useless. You can't do much (if anything at all) with it... Due to the fact that they have variable numbers of columns, they can't be used in views or procs and I can't think of
a singe, off the shelf, reporting platform that will allow accommodate data source that has a different number of columns every time it executes... So unless you're going to be content looking at the results in SSMS or developing a custom application, I don't
see the point.
Also... Aggregates aren't a problem based on the data you're showing. You only have 1 start time and 1 end time per stage. Using either the MIN or MAX aggregate functions will work to give you the desired result.
Anyway, hope that helps,
Jason
Jason Long

Similar Messages

  • How to create a Pivot table without aggregate function

    Dear team
    I have the following table. Follow the link to download SQL Script to create table
    https://drive.google.com/file/d/0B5nQIsvTifixV2Jkb043bVd2Zlk/view?usp=sharing
    I want to create a pivot or cross tab so that it appear like this
    LocationDesc
    >=360
    >=300
    >=270
    >=240
    <240
    Port Moresby
    John Aita
    Mawe Felix
    Augustine Eri Luke
    Joseph Aura
    Robert  ROAKEINA
    Port Moresby
    Ambane Gigmai
    Paul Dem
    Henry  Wanpis
    Tiniki Bau
    William Laki NIGINTS
    Port Moresby
    June Vutnamur
    Alphonse Waieng
    Rex TOMARA
    Mika OROMOIA
    Eveni Ekeni
    Mount Hagen
    Dominic Kouroi
    Senis Ospea
    JOSEPH KLAWA
    Peter WAI
    PHILIP JAMES
    Lae 
    Henry TIAS
    Mathew Dominic
    Jobert Idigel
    Wagi Jerry
    Christopher Bianta
    etc
    etc
    etc
    etc
    etc
    etc
    Marsh Narewec

    Hi Marsh,
    To achieve your requirement, you may reference the below query.
    --pivot the table to get the expected output
    ;WITH cte AS(
    SELECT *,ROW_NUMBER() OVER(PARTITION BY LocationDesc,BandGroup ORDER BY BandGroup) AS rn FROM [TmpBandGroup]
    SELECT LocationDesc,[>=360],[>=300],[>=270],[>=240],[<240]
    FROM cte
    PIVOT
    (MAX(EmployeeName) FOR BandGroup IN([>=360],[>=300],[>=270],[>=240],[<240])) PVT
    ORDER BY LOCATIONDESC
    --the pivot result can be validated with the queries below
    ;WITH cte AS(
    SELECT *,ROW_NUMBER() OVER(PARTITION BY LocationDesc,BandGroup ORDER BY BandGroup) AS rn FROM [TmpBandGroup]
    ,cte2 AS
    SELECT RN, LocationDesc,[>=360],[>=300],[>=270],[>=240],[<240]
    FROM cte
    PIVOT
    (MAX(EmployeeName) FOR BandGroup IN([>=360],[>=300],[>=270],[>=240],[<240])) PVT
    SELECT LocationDesc, COUNT([>=360]) [>=360] ,COUNT([>=300]) [>=300],COUNT([>=270]) [>=270],COUNT([>=240]) [>=240],COUNT([<240]) [<240]
    FROM cte2
    group by LocationDesc
    ORDER BY LocationDesc
    SELECT LocationDesc,bandgroup,COUNT(EmployeeName) FROM [TmpBandGroup] group by LocationDesc ,bandgroup
    ORDER BY LocationDesc,bandgroup DESC
    If you have any feedback on our support, you can click
    here.
    Eric Zhang
    TechNet Community Support

  • Issue in pivot while using aggregate functions

    when I use this below query in oracle sql developer
    ------------->select sum(round(8.08/0.54,2)*30) from dual.
    i am getting result as 448.8.it is a correct value
    but i use this below queries in pivot as same like above query i am getting result of doubled value 914.4.
    PIVOT
    SUM(round(ROUND(sellout,2)/6,2)) AS LAST_6_MON_SELL_OUT,
    SUM(ROUND(inventory,2)) AS INVENTORY_INTINS_1,
    Sum(round(ROUND(inventory,2)/round(ROUND(sellout,2)/6,2),2)*30) As Stockperday
    FOR PRODUCT IN (56,78)
    actually i am getting value for SUM(round(ROUND(sellout,2)/6,2)) is 0.54,*SUM(ROUND(inventory,2))* is 8.08 i the above query ,but i am getting wrong value for this aggregate function Sum(round(ROUND(inventory,2)/round(ROUND(sellout,2)/6,2),2)30)* as 914.4.but actual value is 448.8
    why this problem.can anybody explain me.why this problem

    Try ur luck in 'sql plsql thread'
    PL/SQL

  • Join tables with aggregate function

    Hello
    I have 4 views that I need to perform aggregate function, count, on and then join them for query output.
    Basically each view has a column with a score and a subcontractor name. One subcontractor may have more than one score in each view.
    Here is the sql that I'm working with thus far:
    select e.sub_name, (count(e.score) - 1), (count(v.score) - 1), (count(s.score) - 1), (count(u.score) - 1)
    from speed.v_ratings_ex e, speed.v_ratings_vg v, speed.v_ratings_sat s, speed.v_ratings_uns u
    where v.sub_name = e.sub_name
    and s.sub_name = e.sub_name
    and u.sub_name = e.sub_name
    group by e.sub_name
    order by e.sub_name asc;
    This results in each column returning the same value b/c the join is performed before the aggregate function.
    Can anyone offer some help so that I may get the desired results?

    You need to use in-line views to perform the aggregates, then join the in-line views.
    Something like:
    SELECT e.sub_name, e.score - 1 escore, v.score - 1 vscore,
           s.score - 1 sscore, u.score - 1 uscore
    FROM (SELECT sub_name,count(*) score
          FROM v_ratings_ex
          GROUP BY sub_name) e,
         (SELECT sub_name,count(*) score
          FROM v_ratings_vg
          GROUP BY sub_name) v,
         (SELECT sub_name,count(*) score
          FROM v_ratings_sat
          GROUP BY sub_name) s,
         (SELECT sub_name,count(*) score
          FROM v_ratings_uns
          GROUP BY sub_name) u
    WHERE v.sub_name = e.sub_name and
          s.sub_name = e.sub_name and
          u.sub_name = e.sub_name
    ORDER BY e.sub_name asc;TTFn
    John

  • Wrong order in pivot table using Rank function

    Hi Gurus,
    I have the following query:
    SELECT Periods."Year" saw_0, Periods."Month" saw_1, SalesFacts.Dollars saw_2, RANK(SalesFacts.Dollars BY Periods."Year") saw_3
    FROM SupplierSales ORDER BY saw_0, saw_3
    In table view the order is correct.
    However in pivot table for 1998 (year) the order is correct, for 1999 not!
    Ordering : 1. Year, 2. RANK
    Page: Year, Rows : Months
    Measures : Dollars and RANK
    ( I tried to put the RANK into Rows section, the result is same )
    What is the reason of this strange behavior?
    Thanks
    Laszlo

    Try Sort Column and Row properties given in pivot Table Chart...
    Maybe your problem will resolve

  • JTable's setValueAt() function -URGENT please help!!!

    I am using a subclass of the DefaultTableModel for my JTable. The data is in the form of a Vector of vectors. I am catching user input (editing) by implementing the setValueAt() function. The table consists of 1 row, and 4 columns (thats a required limitation). When I edit(change) only one cell of the table, and dont tab out of the cell, I see that the setValueAt() function is not being called, and the model is not being updated. But when I change the cell value and tab out of the cell to the next cell, the setValueAt() function is being called.
    How can I force the setValueAt() function to be called without tabbing out of the edited cell. I need to implement this feature as a user can change a single cell and click the "save" button.
    here is the setValueAt() function implementation.. Could it be that I might have implemented this in error or need to implement some other methods? Any help/pointers will be greatly appreaciated.
    public void setValueAt(Object value, int row, int col)
    String cellValue = "";
         if (value instanceof String)
              cellValue = (String)value;
         Vector tmp = (Vector)dataVector.elementAt(row);
         tmp.setElementAt(cellValue,col);
         fireTableCellUpdated(row,col);
    Thank You in advance

    Seems to me that the problem is to do with when setValueAt() gets called.
    Sounds like you want it to be called when the Cell's editor loses focus.
    One solution would be something like the following to ensure editing stops when focus is lost.
    There may be other less complicated solutions, but this one should work !
    class  MyEditor extends AbstractCellEditor implements TableCellEditor {
        public MyEditor() {
            this.text = new JTextField();
            this.text.addFocusListener(new FocusListener() {
                public void focusGained(FocusEvent e) {
                public void focusLost(FocusEvent e) {
                    stopCellEditing();
        public Component getTableCellEditorComponent(JTable table, Object value,
                                                                               boolean isselected, int row, int col) {
            this.text.setText((String)value);
            return this.text;
        public Object getCellEditorValue() {
            return this.text.getText();
        private JTextField text;
    table.setDefaultEditor(String.class, new MyEditor());

  • Date function: urgent : please help

    This is the problem statement.:
    Problem: Given two dates (during the years 1901 and 2999, inclusive), find
    the number of days between the two dates.
    Input: Two dates consisting of month, day, year. For example, the inputs
    '6, 2, 1983' and '6, 22, 1983' represent the date range June 2, 1983 to
    June 22, 1983.
    Output: The number of days between the given dates, excluding the starting
    date and the ending date. For example, there are 19 days between June 2,
    1983 and June 22, 1983; namely, June 3, 4, ..., 21.
    Test Input (3 sets):
    Input Line #1: 6, 2, 1983, 6, 22, 1983
    Output #1: 19
    Input Line #2: 7, 4, 1984, 12, 25, 1984
    Output #2: 173
    Input Line #3: 1, 3, 1989, 3, 8, 1983
    Output #3: 1979
    ==========
    I have a GregorianCalender function that works. But I want to use a basic java function that can solve the above problem.
    regards
    ~m

    Here is the code that uses the Gregorian Funtion. Does anybody know of anything basic that replaces what gregorian does in this case.
    import java.util.*;
    import java.io.*;
    import java.math.*;
    import java.text.*;
    import java.lang.*;
    public class Days {
         static int year1,month1,day1;
         static int year2,month2,day2;
         public static void main(String[] args) throws Exception{
              parse(args);
              GregorianCalendar gc1 = new GregorianCalendar(year1, day1, month1);
              GregorianCalendar gc2 = new GregorianCalendar(year2, day2, month2);
              DateFormat df1 = DateFormat.getDateInstance();
              DateFormat df2 = DateFormat.getDateInstance();
              try {
                   Days ds = new Days();
                   ds.getDays(gc1, gc2);
                   System.out.println("Output #: " + (ds.getDays(gc1, gc2) - 1));
              } catch (java.lang.IllegalArgumentException e) {
                   System.out.println("Unable to parse");
         // validate input values,
              // if input parameters are not correct and not in a proper format
              // then throw an exception
         private static void parse(String[] args) throws Exception{
              boolean flag = true;
              if(args == null) output();
              if(args != null && args.length != 2) output();
              validate(args);
              return;
         private static void output() throws Exception{
                   throw new Exception("Input arguments are not correct. You should pass "+
                                            "two input dates with a space such as 1901,1,1 2999,1,1");
         private static void validate(String[] args) throws Exception{
              String arg1 = args[0];
              String arg2 = args[1];
              System.out.println(arg1);
              System.out.println(arg2);
              java.util.StringTokenizer tokenizer = new StringTokenizer(arg1,",");
              if(tokenizer.countTokens() < 3) output();
              String year1AsString = tokenizer.nextToken(",");
              String month1AsString = tokenizer.nextToken(",");
              String day1AsString = tokenizer.nextToken(",");
              year1 = getInt(year1AsString,4);
              month1 = getInt(month1AsString,2);
              day1 = getInt(day1AsString,2);
              java.util.StringTokenizer tokenizer2 = new StringTokenizer(arg2,",");
              if(tokenizer2.countTokens() < 3) output();
              String year2AsString = tokenizer2.nextToken(",");
              String month2AsString = tokenizer2.nextToken(",");
              String day2AsString = tokenizer2.nextToken(",");
              year2 = getInt(year2AsString,4);
              month2 = getInt(month2AsString,2);
              day2 = getInt(day2AsString,2);
         private static int getInt(String string, int maxLimit) throws Exception{
              if(string.length() > maxLimit) output();
              int temp = 0;
              try{
                   temp = Integer.parseInt(string);
              }catch(NumberFormatException e){
                   output();
              return temp;
         public int getDays(GregorianCalendar g1, GregorianCalendar g2) {
              int elapsed = 0;
              GregorianCalendar gc1, gc2;
              if (g2.after(g1)) {
                   gc2 = (GregorianCalendar) g2.clone();
                   gc1 = (GregorianCalendar) g1.clone();
              } else {
                   gc2 = (GregorianCalendar) g1.clone();
                   gc1 = (GregorianCalendar) g2.clone();
              gc1.clear(Calendar.MILLISECOND);
              gc1.clear(Calendar.SECOND);
              gc1.clear(Calendar.MINUTE);
              gc1.clear(Calendar.HOUR_OF_DAY);
              gc2.clear(Calendar.MILLISECOND);
              gc2.clear(Calendar.SECOND);
              gc2.clear(Calendar.MINUTE);
              gc2.clear(Calendar.HOUR_OF_DAY);
              while (gc1.before(gc2)) {
                   gc1.add(Calendar.DATE, 1);
                   elapsed++;
              return elapsed;
    ~m

  • How to disable Row label from the aggregation function in Pivot table

    Hello everyone,
    I have table in Power Pivot like shown below:
    Item_Name
    Category
    Vendor
    Sales_Amount
    Item 1
    Category 1
    Vendor 1
    30
    Item 2
    Category 1
    Vendor 2
    25
    Item 3
    Category 2
    Vendor 3
    50
    Item 3
    Category 2
    Vendor 3
    60
    Item 3
    Category 2
    Vendor 3
    20
    Item 2
    Category 1
    Vendor 2
    10
    Item 2
    Category 1
    Vendor 2
    30
    Item 2
    Category 1
    Vendor 2
    100
    Item 2
    Category 1
    Vendor 1
    20
    By using above table i have to create Rank(Based on Sales amount) by Category and Item name in Pivot table, i have done easily like added two dimension attribute(Category, Item_Name) into row label and Sales_Amount into aggregation tab then I calculated
    rank:=RANKX(ALL(Sales[Item_Name]),[Sum of Sales_Amount]) so finally pivot table looks like shown below:
    But end user want to see the vendor name also in the pivot table but the Rank suppose to be based on Sales amount by Category and Item name. if i added the vendor name also into the row label, rank calculated based on on Sales amount by Category, Item
    name and vendor.
    I would be really grateful if anyone advise how to fix this problem as it will be helpful my most of the reports.
    Regards,
    Robert 

    Darren Gosbell,
    Thanks for your reply.
    Item_Name
    Category
    Vendor
    Sales_Amount
    Item 1
    Category 1
    Vendor 1
    30
    Item 2
    Category 1
    Vendor 2
    25
    Item 3
    Category 2
    Vendor 3
    50
    Item 3
    Category 2
    Vendor 3
    60
    Item 3
    Category 2
    Vendor 3
    20
    Item 2
    Category 1
    Vendor 2
    10
    Item 2
    Category 1
    Vendor 2
    30
    Item 2
    Category 1
    Vendor 2
    100
    Item 2
    Category 1
    Vendor 2
    20
    Item 4
    Category 1
    Vendor 2
    3
    Item 4
    Category 1
    Vendor 2
    50
    Item 4
    Category 1
    Vendor 2
    3
    The above is my new source data.
    I used this function to calculate  Rank:=RANKX(ALL(Sales[Item_Name]),[Sum of Sales_Amount])
    and also used yours below:  
    Rank2:=RANKX(SUMMARIZE(ALL(Sales),[Item_Name],[Category]),CALCULATE([Sum of Sales_Amount],ALLEXCEPT(Sales,Sales[Item_Name],Sales[Category])))
    The Preceding screenshot is the result of our two function but i wanna pivot table like shown below:
    Could please help me to fix it out.

  • How to disable Pivot Table Prompt ability to move into sections?

    Pivot Table Prompts can be moved to sections when report is running. How can the ability to move the prompts be disabled?
    When running the analysis, hover near the beginning of the prompt to get the arrows.
    Can then drag and drop the prompt into another section of the report.
    Can also do the same and move it back up into the prompt, but users sometimes accidentally move the prompt and can't get it out of the section.
    How can the drag and drop of the pivot table prompts be disabled?
    Thanks

    Darren Gosbell,
    Thanks for your reply.
    Item_Name
    Category
    Vendor
    Sales_Amount
    Item 1
    Category 1
    Vendor 1
    30
    Item 2
    Category 1
    Vendor 2
    25
    Item 3
    Category 2
    Vendor 3
    50
    Item 3
    Category 2
    Vendor 3
    60
    Item 3
    Category 2
    Vendor 3
    20
    Item 2
    Category 1
    Vendor 2
    10
    Item 2
    Category 1
    Vendor 2
    30
    Item 2
    Category 1
    Vendor 2
    100
    Item 2
    Category 1
    Vendor 2
    20
    Item 4
    Category 1
    Vendor 2
    3
    Item 4
    Category 1
    Vendor 2
    50
    Item 4
    Category 1
    Vendor 2
    3
    The above is my new source data.
    I used this function to calculate  Rank:=RANKX(ALL(Sales[Item_Name]),[Sum of Sales_Amount])
    and also used yours below:  
    Rank2:=RANKX(SUMMARIZE(ALL(Sales),[Item_Name],[Category]),CALCULATE([Sum of Sales_Amount],ALLEXCEPT(Sales,Sales[Item_Name],Sales[Category])))
    The Preceding screenshot is the result of our two function but i wanna pivot table like shown below:
    Could please help me to fix it out.

  • How to use Aggregate Functions during Top N analysis?

    Say i want to find top 5 highest salaries and their totals and average. In that case how to use aggregate functions. Please give me an example on this.
    Regards,
    Renu
    Message was edited by:
    user642387

    Hi,
    Yes, you can do that with aggregate functions.
    First, do a sub-query to retrieve all the salaries (in descending order), then say "WHERE ROWNUM <= 5" in the main query. Use the aggregate SUM and AVG functions in the main query.
    Analytic functions are easier to use for jobs like this, once you get familiar with them. If you're not leaving the field this month, then it's probably worthwhile for you to get familiar with analytic functions.

  • I try to find such as pivot table like in excel sheet

    How to collect data like pivot table in excel sheet. Please help me

    Hi Chepot,
    Welcome to Apple Discussions and the Numbers '09 forum.
    Numbers does not support pivot tables, but there may be a way to accomplish what you want.
    Here's a link to search results for 'pivot AND table' in the Numbers '09 forum during the past year. Check these prior discussions. If they don't answer your question, post a reply here with further details on the arrangement of data you want to collect.
    Regards,
    Barry

  • Pivot table in BI Publisher: Different aggregate functions in data columns

    Hi, everyone!
    I`ve got some troubles with pivot table in my rtf-template.
    Here is my xml:
    <ROWSET>
         <ROW>
              <_BI_SUBRF_MO_._MUN_NAME_>МО Петроградский р-н</_BI_SUBRF_MO_._MUN_NAME_>
              <_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>714000003</_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>
              <_BI_MONTH_DEBET_._BI_DEBET_SUM_>0.0</_BI_MONTH_DEBET_._BI_DEBET_SUM_>
              <_BI_CALENDAR_._YEAR__>2009</_BI_CALENDAR_._YEAR__>
              <_BI_CALENDAR_._MONTH__>8</_BI_CALENDAR_._MONTH__>
         </ROW>
         <ROW>
              <_BI_SUBRF_MO_._MUN_NAME_>МО Петроградский р-н</_BI_SUBRF_MO_._MUN_NAME_>
              <_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>714000004</_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>
              <_BI_MONTH_DEBET_._BI_DEBET_SUM_>165.58</_BI_MONTH_DEBET_._BI_DEBET_SUM_>
              <_BI_CALENDAR_._YEAR__>2009</_BI_CALENDAR_._YEAR__>
              <_BI_CALENDAR_._MONTH__>7</_BI_CALENDAR_._MONTH__>
         </ROW>
         <ROW>
              <_BI_SUBRF_MO_._MUN_NAME_>МО Петроградский р-н</_BI_SUBRF_MO_._MUN_NAME_>
              <_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>714000004</_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>
              <_BI_MONTH_DEBET_._BI_DEBET_SUM_>165.58</_BI_MONTH_DEBET_._BI_DEBET_SUM_>
              <_BI_CALENDAR_._YEAR__>2009</_BI_CALENDAR_._YEAR__>
              <_BI_CALENDAR_._MONTH__>7</_BI_CALENDAR_._MONTH__>
         </ROW>
    ...... and so on..
    </ROWSET>
    In the pivot table i`d like to see one row for every BISUBRF_MO_._MUN_NAME_ using BICALENDAR_._YEAR__ and BICALENDAR_._MONTH__ as measures and sum of BIMONTH_DEBET_._BI_DEBET_SUM_ and count of unique (or at least just count of) BIPERS_ACCOUNT_CARD_._BI_PAC_NMB_ in cells.
    I create a pivot table using wizard, everything ok except one thing: as i understand, in pivot table for data in rows we can use only one kind of aggregate function e.g. only sum or only count.
    Is there any way to use different aggregate functions in data cells?
    Thank you

    As you can see, when we use crosstab tag we can specify only one aggregate function for data cells.
    Is there any way to get in XSL-FO (where we can what really happens) instead of:
    <T1>
    <xsl:value-of select="sum(current-group()/_BI_PERS_ACCOUNT_CARD_._BI_PAC_UNID_)"/>
    </T1>
    <T2>
    <xsl:value-of select="sum(current-group()/_BI_MONTH_DEBET_._BI_DEBET_SUM_)"/>
    </T2>
    this
    <T1>
    <xsl:value-of select="sum(current-group()/_BI_PERS_ACCOUNT_CARD_._BI_PAC_UNID_)"/>
    </T1>
    <T2>
    <xsl:value-of select="count(current-group()/_BI_MONTH_DEBET_._BI_DEBET_SUM_)"/>
    </T2>
    Edited by: user12115038 on 26.10.2009 8:08

  • Rounding the aggregate function in a pivot table

    How do I round the avg(GRADE) when I tried just wrapping it around the avg function I get an error message saying expect aggregate function inside pivot operation
    pivot
    ( avg(GRADE)
    for Column
    in ('1012222','2221112','333113' );
    Thanks for the help.
    Edit : Sorry wasn't very clear all thats shown is the pivot part of the statement.
    Edited by: 836321 on Feb 13, 2011 8:56 AM
    Edited by: 836321 on Feb 13, 2011 8:58 AM

    Hi, and welcome to the forum.
    It is hard to see how your piece of pseudo SQL is related to your subject title.
    There should not be any problem in wrapping ROUND around AVG:
    SQL> select deptno, round(avg(sal)) from emp
    where deptno in (10,20)
    group by deptno
        DEPTNO ROUND(AVG(SAL))
            20            2175
            10            2917
    2 rows selected.Edit: Sorry, did not recognize you query bit. You probably are asking about this:
    SELECT * FROM
       (SELECT deptno, sal FROM emp)
        PIVOT
       (ROUND(AVG(sal)) FOR deptno IN (10 AS Accounting, 30 AS Sales));
    Error at line 4
    ORA-56902: expect aggregate function inside pivot operationSuppose you have to (There might be other ways)
    SQL> SELECT Round(accounting), round(sales) FROM
       (SELECT deptno, sal FROM emp)
        PIVOT
       (AVG(sal) FOR deptno IN (10 AS Accounting, 30 AS Sales));
    ROUND(ACCOUNTING) ROUND(SALES)
                 2917         1567
    1 row selected.Regards
    Peter
    Edited by: Peter on Feb 13, 2011 9:05 AM
    Edited by: Peter Gjelstrup on Feb 13, 2011 9:08 AM

  • SSAS Cube Hierarchy Default Member, not aggregatable in pivot table multi select, Urgent

    Hi All,
    I have one problem in my project ssas cube. one of the dimension hierarchy, a value has been set up as default member in calculation tab.
    The problem is:
    When i filter (select multiple) this defaulted value and other value in excel pivot, it is always showing default member value instead of selected members aggregated value. if i remove default member it is working fine. Can you please provide work around
    for this.
    Please note that, if select multiple values within that hierarchy (this time not selected default value), aggregation is working fine.
    Thanks, Rajendra

    Hi Hirmando,
    According to your description, the default member cause incorrect data when dragging a attribute that contain a default member to the FILTERS area, right?
    I can reproduce this issue on my environment, when dropping this dimension in the filter area of my pivot table and select multiple members including the default member then only data for the default member is shown. Currently, it's hard to say the root
    reason that cause this issue. In order to narrow down this issue, please apply the latest server pack and cumulative update.
    Besides, you can submit a feedback at
    http://connect.microsoft.com/SQLServer/Feedback So that microsoft will confirm if this is a know issue.
    Regards,
    Charlie Liao
    If you have any feedback on our support, please click
    here.
    Charlie Liao
    TechNet Community Support

  • Why doesn't PIVOT clause work with COLLECT aggregate function in 11g ?

    Hello all !
    I am really puzzled as to what is considered an aggregate function in the context of the PIVOT clause in 11g.
    I have been toying with quite a few things related to collections lately and this arose as an aside :
    CREATE TABLE TEST_COLL
       NODE_ID    VARCHAR2(15 CHAR) NOT NULL,
       NODE_VALUE VARCHAR2(45 CHAR) NOT NULL,
       NODE_LEVEL NUMBER(1)         NOT NULL
    CREATE OR REPLACE TYPE TREE_NODE AS OBJECT
       NODE_KEY  VARCHAR2( 15 CHAR),
       NODE_NAME VARCHAR2(127 CHAR)
    CREATE OR REPLACE TYPE TREE_NODES AS TABLE OF TREE_NODE NOT NULL;At this stage I am sure we all agree that the query
    SELECT NODE_LEVEL,
           CAST(COLLECT(TREE_NODE(NODE_ID, NODE_VALUE)) AS TREE_NODES) AS NODES
      FROM TEST_COLL
    GROUP BY NODE_LEVEL;is perfectly valid as the COLLECT function is an aggregate function according to the [Official Documentation|http://docs.oracle.com/cd/E11882_01/server.112/e10592/functions031.htm#i1271564]
    But then, one of the following two queries should work
    SELECT CAST(REGION_NODES     AS TREE_NODES) AS REGIONS,
           CAST(DEPARTMENT_NODES AS TREE_NODES) AS DEPARTMENTS,
           CAST(AREA_NODES       AS TREE_NODES) AS AREAS,
           CAST(CENTRE_NODES     AS TREE_NODES) AS CENTRES
      FROM (SELECT NODE_LEVEL, TREE_NODE(NODE_ID, NODE_VALUE) AS NODE
              FROM TREE_COLL
    PIVOT (COLLECT(NODE) FOR NODE_LEVEL IN (1 AS REGION_NODES,
                                             2 AS DEPARTMENT_NODES,
                                             3 AS AREA_NODES,
                                             4 AS CENTRE_NODES
    or (better)
    SELECT REGION_NODES     AS REGIONS,
           DEPARTMENT_NODES AS DEPARTMENTS,
           AREA_NODES       AS AREAS,
           CENTRE_NODES     AS CENTRES
      FROM (SELECT NODE_LEVEL, TREE_NODE(NODE_ID, NODE_VALUE) AS NODE
              FROM TREE_COLL
    PIVOT (CAST(COLLECT(NODE) AS TREE_NODES) FOR NODE_LEVEL IN (1 AS REGION_NODES,
                                                                 2 AS DEPARTMENT_NODES,
                                                                 3 AS AREA_NODES,
                                                                 4 AS CENTRE_NODES
           );yet, both fail with
    ORA-56902: expect aggregate function inside pivot operationInvestigating further, I found the same behaviour when using XMLAGG as the aggregate function in the PIVOT clause.
    Is this normal ? And if it is, is there any other way to achieve the result I was anticipating ?
    My version is
    SQL> SELECT BANNER FROM V$VERSION;
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    PL/SQL Release 11.2.0.3.0 - Production
    CORE    11.2.0.3.0      Production
    TNS for 64-bit Windows: Version 11.2.0.3.0 - Production
    NLSRTL Version 11.2.0.3.0 - ProductionThanks in advance
    Best Regards
    Philip

    Most likely a bug. But you can bypass it by using any other aggregate making sure group consists of a single row and apply collect to a pivoted value. Yes, the cost is double aggregation. And also there is another cost - you would need to create MAP member function otherwise aggreations like MAX/MIN, etc. will not work:
    CREATE OR REPLACE TYPE TREE_NODE AS OBJECT
       NODE_KEY  VARCHAR2( 15 CHAR),
       NODE_NAME VARCHAR2(127 CHAR),
       map member function f return varchar2
    Type created.
    CREATE OR REPLACE TYPE BODY TREE_NODE AS
      map member function f return varchar2 is
      begin
         return NODE_NAME;
      end f;
    end;
    Type body created.
    CREATE OR REPLACE TYPE TREE_NODES AS TABLE OF TREE_NODE NOT NULL
    Type created.
    SQL> select  *
      2    from  test_coll
      3  /
    NODE_ID NODE_VALUE NODE_LEVEL
    1       A                   1
    2       B                   2
    3       C                   3
    4       D                   4
    5       E                   1
    6       F                   2
    7       G                   3
    8       H                   4
    8 rows selected.
    SQL> Now:
    SELECT CAST(COLLECT(REGION_NODES)     AS TREE_NODES) AS REGIONS,
           CAST(COLLECT(DEPARTMENT_NODES) AS TREE_NODES) AS DEPARTMENTS,
           CAST(COLLECT(AREA_NODES)       AS TREE_NODES) AS AREAS,
           CAST(COLLECT(CENTRE_NODES)     AS TREE_NODES) AS CENTRES
      FROM (
            SELECT  ROWID RID,
                    NODE_LEVEL,
                    TREE_NODE(NODE_ID, NODE_VALUE) AS NODE
              FROM  TEST_COLL
    PIVOT (MAX(NODE) FOR NODE_LEVEL IN (
                                         1 AS REGION_NODES,
                                         2 AS DEPARTMENT_NODES,
                                         3 AS AREA_NODES,
                                         4 AS CENTRE_NODES
    REGIONS(NODE_KEY, NODE_NAME)                         DEPARTMENTS(NODE_KEY, NODE_NAME)                     AREAS(NODE_KEY, NODE_NAME)                           CENTRES(NODE_KEY, NODE_NAME)
    TREE_NODES(TREE_NODE('1', 'A'), TREE_NODE('5', 'E')) TREE_NODES(TREE_NODE('6', 'F'), TREE_NODE('2', 'B')) TREE_NODES(TREE_NODE('7', 'G'), TREE_NODE('3', 'C')) TREE_NODES(TREE_NODE('8', 'H'), TREE_NODE('4', 'D'))
    SQL> SY.

Maybe you are looking for