Pivot table query - PLEASE HELP

I have one requirement whichis similar like this.
I have following tables.
POSITIONS (one account holds multiple securities)
account_id     security_id     total
1          101          300
1          102          500
2          102          300
2          103          600
3          104          400
4          104          800
SECURITIES
security_id     security_name     country          region
101          ABC          USA          NA
102          EFG          USA          NA
103          PQR          GBR          EUR
104          XYZ          CANADA          NA
I will get all the account numbers from front end application, for which I have to display output like this. For example I am getting accounts, 1,2,3 and 4 and have to display like columns at the end. I have to groupy by region and country.
REGION     COUNTRY          SECURITY_ID     SECURITY_NAME          1     2     3     4
NA     USA          101          ABC               300
               102          EFG               500     300
                         COUNTRY TOTAL          800     300
     CANADA          104          XYZ                         400     800
                         REGION TOTAL          800     300     400     800
EUR     GBR          103          PQR                    600
                         COUNTRY TOTAL               600          
                         REGION TOTAL          800     900     400     800
I am working on Oracle 9i Release 2 and front end is .net. Can anyone please help me out with a solution using a query or a stored proc. For Stored procedure, I should send refcurser as OUT parameter or any possible solution.
Thanks in advance.

You could do the following:
In SQL
You can use the decode function and SUM over it.
Get the totals by region and country
select region,country, s.sec_id, sec_name,
sum(decode(acct_id,1,total,0)) a1,
sum(decode(acct_id,2,total,0)) a2,
sum(decode(acct_id,3,total,0)) a3,
sum(decode(acct_id,4,total,0)) a4
from positions p, securities s
where p.SEC_ID = s.SEC_ID
group by region,country, s.sec_id, sec_name
You can run similar sql to get totals by country or region separately ... just remove from select and group by clause the column name that you do not want to be displayed.
The limitation here is that you would need to write the decode statement for every account. Some programming may be needed here.
In PLSQL
create or replace procedure acct_sum (cr OUT sys_refcursor)
is
cursor c1 is
select distinct acct_id from positions order by 1 asc;
var1 varchar2(1000) := ' ';
var2 varchar2(1000);
begin
for i in c1 loop
var1 := var1 || ' sum(decode(acct_id, ' || i.acct_id || ', total,0) ),';
end loop;
var1 := substr(var1,1,length(var1)-1);
open cr for
'select region,country, s.sec_id, sec_name, ' || var1 ||
'from positions p, securities s
where p.SEC_ID = s.SEC_ID
group by region,country, s.sec_id, sec_name';
end acct_sum;
You may want to modify the code so pass in an array of accounts and loop through the content to get all the accounts.
Hope this helps
Shakti
http://www.impact-sol.com
Developers of Guggi Oracle - Tool for DBAs and Developers

Similar Messages

  • Internal Table query Please help!!!!!

    Q. What is the differance between:
    TABLES:  KNA1.
    PARAMETERS:  STATE LIKE KNA1-REGIO DEFAULT ‘MA’.
    TYPES:       BEGIN OF OUTREC,
                 KUNNR LIKE KNA1-KUNNR,
                 REGIO  LIKE KNA1-REGIO,
                 TELF1  LIKE KNA1-TELF1,
              END OF OUTREC.
    DATA:     OUT_ITAB <b>TYPE</b> STANDARD TABLE OF OUTREC
                 INITIAL SIZE 10 WITH HEADER LINE.
    and
    TABLES:  KNA1.
    PARAMETERS:  STATE LIKE KNA1-REGIO DEFAULT ‘MA’.
    TYPES:       BEGIN OF OUTREC,
                 KUNNR LIKE KNA1-KUNNR,
                 REGIO  LIKE KNA1-REGIO,
                 TELF1  LIKE KNA1-TELF1,
              END OF OUTREC.
    DATA:     OUT_ITAB <b>LIKE</b> STANDARD TABLE OF OUTREC
                 INITIAL SIZE 10 WITH HEADER LINE.
    also cant we write it as:
    DATA:     OUT_ITAB LIKE KNA1OCCURS 10 WITH HEADER LINE.
    Thanks,
    Dhiraj

    hi Dhiraj,
    Refer to this related thread
    Re: Occurs 0 Versus Type standard table of
    Difference between Declaring variables

  • How to improve the performance of the attached query, Please help

    Hi,
    How to improve performance of the below query, Please help. also attached explain plan -
    SELECT Camp.Id,
    rCam.AccountKey,
    Camp.Id,
    CamBilling.Cpm,
    CamBilling.Cpc,
    CamBilling.FlatRate,
    Camp.CampaignKey,
    Camp.AccountKey,
    CamBilling.billoncontractedamount,
    (SUM(rCam.Impressions) * 0.001 + SUM(rCam.Clickthrus)) AS GR,
    rCam.AccountKey as AccountKey
    FROM Campaign Camp, rCamSit rCam, CamBilling, Site xSite
    WHERE Camp.AccountKey = rCam.AccountKey
    AND Camp.AvCampaignKey = rCam.AvCampaignKey
    AND Camp.AccountKey = CamBilling.AccountKey
    AND Camp.CampaignKey = CamBilling.CampaignKey
    AND rCam.AccountKey = xSite.AccountKey
    AND rCam.AvSiteKey = xSite.AvSiteKey
    AND rCam.RmWhen BETWEEN to_date('01-01-2009', 'DD-MM-YYYY') and
    to_date('01-01-2011', 'DD-MM-YYYY')
    GROUP By rCam.AccountKey,
    Camp.Id,
    CamBilling.Cpm,
    CamBilling.Cpc,
    CamBilling.FlatRate,
    Camp.CampaignKey,
    Camp.AccountKey,
    CamBilling.billoncontractedamount
    Explain Plan :-
    Description                    Object_owner          Object_name     Cost     Cardinality     Bytes     
    SELECT STATEMENT, GOAL = ALL_ROWS                              14     1     13
    SORT AGGREGATE                                                  1     13
    VIEW                         GEMINI_REPORTING               14     1     13
    HASH GROUP BY                                        14     1     103
    NESTED LOOPS                                        13     1     103
    HASH JOIN                                             12     1     85
    TABLE ACCESS BY INDEX ROWID     GEMINI_REPORTING     RCAMSIT          2     4     100
    NESTED LOOPS                                        9     5     325
    HASH JOIN                                        7     1     40
    SORT UNIQUE                                        2     1     18
    TABLE ACCESS BY INDEX ROWID     GEMINI_PRIMARY          SITE          2     1     18
    INDEX RANGE SCAN          GEMINI_PRIMARY          SITE_I0          1     1     
    TABLE ACCESS FULL          GEMINI_PRIMARY          SITE          3     27     594
    INDEX RANGE SCAN          GEMINI_REPORTING     RCAMSIT_I     1     1     5     
    TABLE ACCESS FULL     GEMINI_PRIMARY     CAMPAIGN                    3     127     2540
    TABLE ACCESS BY INDEX ROWID     GEMINI_PRIMARY          CAMBILLING     1     1     18
    INDEX UNIQUE SCAN     GEMINI_PRIMARY     CAMBILLING_U1                    0     1

    Hello,
    This has really nothing to do with the Oracle Forms product.
    Please, send the SQL or/and PL/SQL questions in the corresponding forums.
    Francois

  • Export specific records from all table? Please help me with my query

    hi
    there are around 200 tables, I want to take a export of all tables of xyz schema.
    My requirement is just a few records of primary key and their relevant records from other tables to be exported by using exp or expdp utility.

    Hi Toni...
    Thanks for your reply.
    Could you please help me understand how to user this expdp query...
    and what exactly should i mention in the parfile. I want all relevant records from all tables of primary key. please help me to write a query accordingly.

  • PROBLEM WITH HIERARCHICAL QUERY - PLEASE HELP

    I have got three tables :
    CREATE TABLE FIRM
    (FID INTEGER NOT NULL PRIMARY KEY,
    FNAME VARCHAR(40),
    FTYPE VARCHAR(3),
    MASTERID INTEGER );
    CREATE TABLE FACULTY
    (FAID INTEGER NOT NULL PRIMARY KEY,
    FANAME VARCHAR(40),
    FATYPE VARCHAR(3),
    MASTERID INTEGER );
    CREATE TABLE EMPLOYEE
         (EID INTEGER NOT NULL PRIMARY KEY,
    ENAME VARCHAR(20),
    ESURNAME VARCHAR(20),
         EJOB VARCHAR(3),
         MASTERID INTEGER );
    This is a hierarchical tree(or is ment to be, I,m complete rookie ) . Firm can be the root or can be slave to another firm. Faculty can be slave to firm, or to another faculty. Employee can be slave to faculty or to another employee(e.g. boss). This connections are specified by MASTERIDs.
    I need to write a procedure, which parameter would be node ID. It is meant to create a VIEW from this node as if it was a root (view of a subtree).
    I tried CONNECT BY clause but it works only on one table at a time and I have here three tables.
    I completely don,t know how to write it. Please help.

    create view hierarchy as
    select id, master_id, name from table1
    union all
    select id, master_id, name from table2
    union all
    select id, master_id, name from table3
    Then do your connect by query against hierarchy.
    It will not work in 8i (connect by on views not allowed), so you will need to materialize the view.
    Kirill

  • How to make recursive query.Please help

    Dear Experts:
    I want to retrieve all employees located in a department in addition to all other employees located in the child's nodes of this department too.
    Problem Details:
    I have "Employees" table and "Departments" Table
    The structure of Dept Table is:
    ID   primary key
    parent_dept   foreign key(id)
    deptname  varchar2
    deptType varchar2
    The Employee table structure is
    ID primary key
    dept_id foreign key(Department.id)
    empName varchar2Sample data for departments
    ID : 1
    parent_dept : null
    deptname: General Manager office
    deptType : 'GM'
    ID :=2
    parent_dept : 1
    deptname: Information tech.
    deptType : 'DPT'
    ID :=3
    parent_dept : 2
    deptname: Software Development
    deptType : 'SECTION'Sample Data for employees
    ID : 101
    dept_id  :1
    empName  King
    ID : 102
    dept_id  :2
    empName  ALAN
    ID : 103
    dept_id  :2
    empName  SAM
    ID : 104
    dept_id  :3
    empName  JANEI want to create a query that accepts a parameter "p_department_id" and returns All employees on the following conditions
    1- In case the parameter value is null , then retrieve All Employees "king - alan- sam-jane"
    2- In Case the parameter value is 1 , then retrieve all the employees under department id =1 in addition to all the employees located under the children departments.
    in this case it will be "king - alan- sam-jane"
    3- In case parameter value is 2 , then return all the employees under department id =2 in addition to all the employees located under the children departments.
    In this case it will be " alan- sam-jane"
    4- In case parameter value is 3 , then return all the employees under department id =3 in addition to all the employees located under the children departments.
    in this case it will be only "JANE"
    In brief , If I pass any value to the parameter :p_department_id , I want to retrieve all employees located in this department in addition to other employees located in the child's nodes of this department id
    I use oracle database 11g release 2
    Please help me
    Thanks
    Edited by: ta**** on Apr 3, 2013 5:56 PM
    Edited by: ta**** on Apr 3, 2013 5:58 PM

    SQL> variable p_department_id number
    SQL> exec :p_department_id := null
    PL/SQL procedure successfully completed.
    SQL> with employees as (
      2                     select 101 id,1 dept_id,'King' empName from dual union all
      3                     select 102,2,'ALAN' from dual union all
      4                     select 103,2,'SAM' from dual union all
      5                     select 104,3,'JANE' from dual
      6                    ),
      7     departments as (
      8                     select 1 id,null parent_dept,'General Manager office' deptname,'GM' deptType from dual union all
      9                     select 2,1,'Information tech.','DPT' from dual union all
    10                     select 3,2,'Software Development','SECTION' from dual
    11                    )
    12  select  *
    13    from  employees
    14    where dept_id in (
    15                      select  id
    16                        from  departments
    17                        start with (
    18                                       (
    19                                            :p_department_id is null
    20                                        and
    21                                            parent_dept is null
    22                                       )
    23                                    or
    24                                       id = :p_department_id
    25                                   )
    26                        connect by parent_dept = prior id
    27                     )
    28  /
            ID    DEPT_ID EMPN
           101          1 King
           102          2 ALAN
           103          2 SAM
           104          3 JANE
    SQL> exec :p_department_id := 1
    PL/SQL procedure successfully completed.
    SQL> /
            ID    DEPT_ID EMPN
           101          1 King
           102          2 ALAN
           103          2 SAM
           104          3 JANE
    SQL> exec :p_department_id := 2
    PL/SQL procedure successfully completed.
    SQL> /
            ID    DEPT_ID EMPN
           102          2 ALAN
           103          2 SAM
           104          3 JANE
    SQL> exec :p_department_id := 3
    PL/SQL procedure successfully completed.
    SQL> /
            ID    DEPT_ID EMPN
           104          3 JANE
    SQL> SY.

  • SQL Query -- Please Help

    Table1
    QTE_ID     SEQ_NO
    1435177     2
    1435177     5
    1435177     7
    1435177     8
    1435177     12
    1435177     14
    Table2
    QTE_ID     SEQ_NO     CMMT_CURR_AMT
    1435177     1     98500
    1435177     2     98500
    1435177     3     0
    1435177     4     98500
    1435177     5     98500
    1435177     7     98500
    1435177     8     98500
    1435177     11     59300
    1435177     12     59300
    1435177     14     59300
    The result should be
    QTE_ID     SEQ_NO     CMMT_CURR_AMT
    1435177     2     0 (where 0 = cmmt_curr_amt of seq 2 - cmmt_curr_amt of seq 1 from table 2)
    1435177     5     0 (where 0 = cmmt_curr_amt of seq 5 - cmmt_curr_amt of seq 4 from table 2)
    1435177     7     0 (where 0 = cmmt_curr_amt of seq 7 - cmmt_curr_amt of seq 5 from table 2)
    1435177     8     0 (where 0 = cmmt_curr_amt of seq 8 - cmmt_curr_amt of seq 7 from table 2)
    1435177     12     0 (where 0 = cmmt_curr_amt of seq 12 - cmmt_curr_amt of seq 11 from table 2)
    1435177     14     0 (where 0 = cmmt_curr_amt of seq 14 - cmmt_curr_amt of seq 12 from table 2)
    I have to get the difference of cmmt_curr_amt from the table2 from seq 14 to seq 12 for seq14 in table 1.
    Please help me in writing the query.
    Thanks in advance.
    Srinivas

    WITH table1 AS
    (SELECT 1435177 qte_id, 2 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 5 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 7 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 8 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 12 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 14 seq_no
    FROM dual
    table2 AS
    SELECT 1435177 qte_id, 1 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 2 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 3 seq_no, 0 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 4 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 5 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 7 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 8 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 11 seq_no, 59300 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 12 seq_no, 59300 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 14 seq_no, 59300 cmmt_curr_amt
    FROM dual
    SELECT qte_id, t2_seq_no, cmmt_curr_amt - cmmt_curr_lag diff
    FROM
    (SELECT t2.qte_id, t2.seq_no t2_seq_no, t1.seq_no t1_seq_no, cmmt_curr_amt, LAG(cmmt_curr_amt,1,0) OVER (PARTITION BY t2.qte_id ORDER BY t2.seq_no) cmmt_curr_lag
    FROM table1 t1, table2 t2
    WHERE t2.qte_id = t1.qte_id (+)
    AND t2.seq_no = t1.seq_no(+)
    ORDER BY 1,2
    WHERE t1_seq_no IS NOT NULL
    ORDER BY 1,2
    QTE_ID T2_SEQ_NO DIFF
    1435177 2 0
    1435177 5 0
    1435177 7 0
    1435177 8 0
    1435177 12 0
    1435177 14 0

  • Passing parameter to a SQL query - Please help

    Hi All,
    I am new to JDBC. I have been trying to pass an external variable to an SQL Query.
    The query is
    String username1="le";
    PreparedStatement pstmt = null;
    pstmt = c.prepareStatement("select * from users where USER_NAME like '%?%'");
         pstmt.setString(1, username1);
         pstmt.executeQuery();
         ResultSet rs = pstmt.getResultSet();
    I am trying to retrieve values from the users table where the USER_NAME column value that is a String contains the supplied value username1.
    I am using the question mark (?) character to pass the value from the variable username1. I am also using the '%' substitution character which matches for any number of characters. So, the above query should retrieve rows where the USER_NAME is something like "charles","leander","Elena" etc.( that contains "le")
    I am getting the error:
    SQLException: java.sql.SQLException: ORA-01006: bind variable does not exist
    I changed the query to
    PreparedStatement pstmt = null;
    pstmt = c.prepareStatement("select * from users where USER_NAME like '% " + username1 + "%'");
         //pstmt.setString(1, username1);
         pstmt.executeQuery();
    This time , it is not giving the error and retrieving properly.
    But I want to use the original query and use the "pstmt.setString(1, username1); " . Is there any way of achieving this?
    Please help.
    Cheers,
    charles_am

    hi,
    try this...
    String username1="%le%";
    pstmt = c.prepareStatement("select * from users where USER_NAME like ?")
    pstmt.setString(1,username1);
    cheers,
    rpk

  • Single sql query-please help

    Hi experts,
    what i want to do is write a single query which will show whether a employee
    exits in the company or not.I have two tables emp and dept.There are as follows.
    SQL> select * from emp;
    NAME DEPTNO EMPNO
    xxx 10 33036
    YYY 12 2345
    ZZZ 13 678
    KKK 14 5678
    RRR 15 7865
    SQL> select * from dept;
    DEPTNO LOCATION
    10 AAA
    11 BBB
    12 CCC
    13 DDD
    what i want is it will select records from the emp table and find whether corrosponding
    deptno really exists in the dept table.If the value is found in deptno column the dept table then it will set the value
    Y other wise it will be N and all i have to do with the help of a single query.
    expected result
    name empno exists
    xxx 33036 Y
    YYY 2345 Y
    ZZZ 678 Y
    KKK 5678 N
    RRR 7865 N
    Please help.
    Regards
    Rajat

    SELECT EMPNO, NAME , EMPNO , NVL( ( SELECT 'Y' FROM    DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO),'N') EXIST
    FROM EMP
    ORDER BY 1
    Demo
    SQL> WITH EMP AS(
      2  SELECT 'XXX' NAME , 10 DEPTNO ,33036  EMPNO FROM DUAL UNION
      3  SELECT 'YYY', 12, 2345 FROM DUAL UNION
      4  SELECT 'ZZZ', 13 ,678 FROM DUAL UNION
      5  SELECT 'KKK', 14 ,5678 FROM DUAL UNION
      6  SELECT 'RRR', 15 ,7865 FROM DUAL  ),
      7  DEPT AS(
      8  SELECT 10  DEPTNO,'AAA' DNAME FROM DUAL UNION
      9  SELECT 11 ,'BBB' FROM DUAL UNION
    10  SELECT 12 ,'CCC' FROM DUAL UNION
    11  SELECT 13 ,'DDD'FROM DUAL )
    12  SELECT EMPNO, NAME , EMPNO , NVL( ( SELECT 'Y' FROM    DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO),'N')
    EXIST
    13  FROM EMP
    14  ORDER BY 1
    15  /
         EMPNO NAM      EMPNO E
           678 ZZZ        678 Y
          2345 YYY       2345 Y
          5678 KKK       5678 N
          7865 RRR       7865 N
         33036 XXX      33036 Y
    SQL> Edited by: Salim Chelabi on Dec 7, 2008 4:15 AM

  • Query--please help

    Hello Forum Members,
    Can you please help me out:
    SQL> select * from quarter_test4;
    QUARTER CONFIG REP_DATE
    Q1-2007 10 12-JAN-07
    Q2-2007 10 21-APR-07
    Q3-2007 870 14-AUG-07
    Q4-2007 50 15-NOV-07
    Q1-2008 60 02-JAN-09
    Q4-2006 160 02-DEC-06
    I want the following out put:
    Please note that the future quarters should have current quarter sum(config).Please note that The current table
    does not hold future quarters.I have to genarate query on which a report is based.
    My Query:
    SELECT quarter,
    CASE
    WHEN qtr > TRUNC(SYSDATE, 'q')
    THEN LAST_VALUE(sum_config IGNORE NULLS) OVER(ORDER BY qtr)
    ELSE sum_config
    END sum_config
    FROM (SELECT qtr, q.qtrstr quarter, SUM(qt.config) sum_config
    FROM (SELECT 'Q' || TO_CHAR(qtr, 'q-yyyy') qtrstr, qtr
    FROM (SELECT ADD_MONTHS(ADD_MONTHS(TRUNC(SYSDATE, 'q'), -13), 3 *(LEVEL - 1)) qtr
    FROM DUAL
    CONNECT BY LEVEL <= 9)
    ORDER BY qtr) q,
    quarter_test4 qt
    WHERE qt.quarter(+) = q.qtrstr
    GROUP BY qtr, q.qtrstr)
    ORDER BY qtr;
    Correct Output Generated:
    QUARTER SUM_CONFIG
    Q4-2006 160
    Q1-2007 10
    Q2-2007 10
    Q3-2007 870
    Q4-2007 50
    Q1-2008 60
    Q2-2008 60
    Q3-2008 60
    Q4-2008 60
    ====================================================================================================================================
    New Requirement:Product column has been added.
    SQL> select * from quarter_test3;
    QUARTER CONFIG REP_DATE PRODUCT
    Q1-2007 10 12-JAN-07 P1
    Q2-2007 10 21-APR-07 P1
    Q3-2007 870 14-AUG-07 P1
    Q4-2007 50 15-NOV-07 P1
    Q1-2008 60 02-JAN-09 P1
    Q4-2006 160 02-DEC-06 P1
    Q4-2006 997 02-DEC-06 P2
    Q4-2007 60 14-NOV-07 P2
    Q3-2007 970 14-NOV-07 P2
    Q2-2007 20 21-APR-07 P2
    Q1-2007 20 12-JAN-07 P2
    QUARTER CONFIG REP_DATE PRODUCT
    Q1-2008 70 12-JAN-08 P2
    Expected Output:
    Q4-2006 160 P1
    Q1-2007 10 P1
    Q2-2007 10 P1
    Q3-2007 870 P1
    Q4-2007 50 P1
    Q1-2008 60 P1
    Q2-2008 60 P1
    Q3-2008 60 P1
    Q4-2008 60 P1
    Q4-2006 260 P2
    Q1-2007 20 P2
    Q2-2007 20 P2
    Q3-2007 970 P2
    Q4-2007 60 P2
    Q1-2008 70 P2
    Q2-2008 70 P2
    Q3-2008 70 P2
    Q4-2008 70 P2
    My Query:
    SELECT quarter,product,
    CASE
    WHEN qtr > TRUNC(SYSDATE, 'q')
    THEN LAST_VALUE(sum_config IGNORE NULLS) OVER(ORDER BY qtr)
    ELSE sum_config
    END sum_config
    FROM (SELECT qtr, q.qtrstr quarter,product, SUM(qt.config) sum_config
    FROM (SELECT 'Q' || TO_CHAR(qtr, 'q-yyyy') qtrstr, qtr
    FROM (SELECT ADD_MONTHS(ADD_MONTHS(TRUNC(SYSDATE, 'q'), -13), 3 *(LEVEL - 1)) qtr
    FROM DUAL
    CONNECT BY LEVEL <= 9)
    ORDER BY qtr) q,
    quarter_test3 qt
    WHERE qt.quarter(+) = q.qtrstr
    GROUP BY qtr,product, q.qtrstr)
    ORDER BY substr(qtr,-4),substr(QTR,2,1)
    Output:
    QUARTER PRODUCT SUM_CONFIG
    Q4-2006 P1 160
    Q4-2006 P2 997
    Q4-2007 P1 50
    Q4-2007 P2 60
    Q4-2008 70
    Q2-2007 P1 10
    Q2-2007 P2 20
    Q2-2008 70
    Q3-2007 P1 870
    Q3-2007 P2 970
    Q3-2008 70
    QUARTER PRODUCT SUM_CONFIG
    Q1-2007 P1 10
    Q1-2007 P2 20
    Q1-2008 P1 70
    Q1-2008 P2 70
    The query had not generated q2-2008,q3-2008,q4-2008 values [60]for P1...it had only generated q2-2008,q3-2008,q4-2008 values[70] for product P2.
    Can you please advise me.

    You can make up the data using the model clause:
    SQL> create table quarter_test3 (quarter, config, product)
      2  as
      3  select 'Q1-2007', 10, 'P1' from dual union all
      4  select 'Q2-2007', 10, 'P1' from dual union all
      5  select 'Q3-2007', 870, 'P1' from dual union all
      6  select 'Q4-2007', 50, 'P1' from dual union all
      7  select 'Q1-2008', 60, 'P1' from dual union all
      8  select 'Q4-2006', 160, 'P1' from dual union all
      9  select 'Q4-2006', 997, 'P2' from dual union all
    10  select 'Q1-2007', 60, 'P2' from dual union all
    11  select 'Q2-2007', 970, 'P2' from dual union all
    12  select 'Q3-2007', 20, 'P2' from dual union all
    13  select 'Q4-2007', 20, 'P2' from dual union all
    14  select 'Q1-2008', 70, 'P2' from dual
    15  /
    Tabel is aangemaakt.
    SQL> select 'Q' || to_char(mod(q,4) + 1) || '-' || to_char(trunc(q/4)) quarter
      2       , config
      3       , product
      4    from quarter_test3
      5   model
      6         partition by (product)
      7         dimension by (to_number(substr(quarter,4))*4 + to_number(substr(quarter,2,1)) - 1 q)
      8         measures (config)
      9         rules
    10         ( config[for q from 2006*4 + 3 to 2008*4 + 3 increment 1]
    11           = nvl(config[cv()],config[cv()-1])
    12         )
    13   order by product
    14       , q
    15  /
    QUARTER                                                                                CONFIG PR
    Q4-2006                                                                                   160 P1
    Q1-2007                                                                                    10 P1
    Q2-2007                                                                                    10 P1
    Q3-2007                                                                                   870 P1
    Q4-2007                                                                                    50 P1
    Q1-2008                                                                                    60 P1
    Q2-2008                                                                                    60 P1
    Q3-2008                                                                                    60 P1
    Q4-2008                                                                                    60 P1
    Q4-2006                                                                                   997 P2
    Q1-2007                                                                                    60 P2
    Q2-2007                                                                                   970 P2
    Q3-2007                                                                                    20 P2
    Q4-2007                                                                                    20 P2
    Q1-2008                                                                                    70 P2
    Q2-2008                                                                                    70 P2
    Q3-2008                                                                                    70 P2
    Q4-2008                                                                                    70 P2
    18 rijen zijn geselecteerd.Regards,
    Rob.

  • Help to correct to codes to move data from tables! please help!

    HI,
    There is a internal table t and t1. I used loop to move the data which zuonr has no value from t to t1.
    BSID TABLE:
    KUNNR                 ZUONR
    0000052984         no value                            &#12288;&#12288;&#12288;&#12288;&#12288;&#12288;&#12288;&#12288;&#12288;  0000052984          no value                                            00052984              2007/06&#27770;&#31639;
    00052984               no value
    000052984          &#12288;2007/07&#25391;&#24403;      
    0000052984            MSFT047/07
    0000052984           MSFT061/07
    *what I need to do is that to move all the data belonge to the same kunnr whenever zuonr = space happend. *.
    The problem is that the data which zuonr = space is moved to t1 successful, but the other data which belong to the same kunnr has zuonr valune still remain.
    -Question_ : How to move all the data belong to the same kunnr whenever zuonr = space happend.
    DATA: BEGIN OF t OCCURS 0,
            bukrs   LIKE knb1-bukrs,
            zuonr   Like bsid-zuonr,                        "sort key  "CR01
            belnr   LIKE bsid-belnr,
            kunnr   LIKE kna1-kunnr,
            bldat   LIKE bsid-bldat,
            budat   LIKE bsid-budat,
            netdt   LIKE bsega-netdt,
            waers   LIKE bsid-waers,
            wrbtr   LIKE bsid-wrbtr,
            shkzg   LIKE bsid-shkzg,
            xblnr   LIKE bsid-xblnr,                            "WD041005a
            sgtxt   LIKE bsid-sgtxt,
            dmbtr   LIKE bsid-dmbtr,
          END OF t.
    DATA : t1 LIKE STANDARD TABLE OF t WITH HEADER LINE.
    *LOOP AT t WHERE zuonr EQ space.
      MOVE-CORRESPONDING t TO t1.
      APPEND t1.
    ENDLOOP.*
    How can I write the loop statement to mention  the KUNNR
    Please help, thank you!!

    Hi,
    Try this.
    data : wa_t like line of table t.
    field-symbols : <f_t> like line of table t.
    sort t by kunnr.
    Loop at t where zuonr eq space.
    read table t1 with key kunnr = t-kunnr.
    if sy-subrc = 0.
    continue.
    endif.
    read table t into wa_t with key kunnr = t-kunnr.
    if sy-subrc = 0.
    loop at t assigning <f_t> from sy-tabix..
    if <f_t>-kunnr = wa_t-kunnr.
    move-corresponding <f_t> to t1.
    append t1.
    else.
    exit.
    endif.
    endloop.
    endif.
    endloop.
    Regards,
    Mohaiyuddin

  • How to rewrite this query without sub query please help me

    Hello All Good Evening,
    Could you please help me with this query, how can i write this query without sub query, or how can write this query another ways
    please help me
    select planno, status1, count(*) Counts from
    select a.ValetNO PlanNo  ,
    case 
         when JoinCode in ('00', '01', '02') then 'Actcess'
         when JoinCode in ('20', '21', '22', '23','38', '39') then
         'Secured' else 'Other' end Status1 ---, COUNT (*)
       from  dbo.ppt a(NOLOCK)  left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO  = b.ValetNO
    --group by a.ValetNO
      a group by planno, status1
    order by 2
    Thank you in Advance
    Milan

    Whats your objective here? Sorry, am not able to understand the reason for this change. 
    Try the below:(Not tested)
    ;With cte
    As
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 ---, COUNT (*)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    select planno, status1, count(*) Counts from cte
    a group by planno, status1
    order by 2
    Even below:
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 , COUNT (1)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    Group by a.ValetNO ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end

  • Problem while designing query please help

    Hi all,
    I have to query to find No of open cases, No of closed cases,
    Average of open cases, Average age of closed cases,
    Maximum of closed cases for a particulat Current assigne(char).
    my desired output is as follows.
    CurAssigne   Noofopencases   Noofclosed cases  Avgage(opencases) Max(open)
    CurAssigne1       10                      20                     8                              9
    CurAssigne1       18                      22                     9                              10
    My cube is consists of.
    4 Dimensions & 4 key figures
    4Dimensions are listed below.
    Business Partner{currentassigne, previousassigne,acmanager}
    createdon{createdonmonth,createdonday,createdtime}
    closedon{closedmonth,closedday,closedtime}
    Status{Latest case status-(2 status are available for each case - open,close)}
    above the objects between {} are chars.
    4 keys are listed below.
    no of cases.
    age
    first response time
    research time.
    Please help me to design this query friends..
    It's very urgent friends please help me.

    Hi,
    Please share your system configuration on which you are trying to install Ps CS6.
    Regards,
    Ashutosh
    Ps Installer QE

  • Cluster bar chart- sql query please help-

    Hi,
    I am trying to create cluster bar chart and am stumped with this sql query.Any help is appreciated.
    Here is my table
    city     region      issue     value
    c1     north     i1     y
    c1     north     i2     y
    c2     north     i1     n
    c2     north     i2     y
    c3     south     i1     y
    c3     south     i2     n
    c4     east     i1     n
    c4     east     i2     n
    The bar chart will have 3 series, north south and east.
    And labels will be i1 and i2. value will be number of times this issue was encountered(y) in this region.
    How can I get something like this from the above table-
    region     issue     count(yes)
    north     i1     1
    north     i2     2
    south     i1     1
    south     i2     0
    east     i1     0
    east     i2     0
    thanks

    WITH table1 AS
    (SELECT 1435177 qte_id, 2 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 5 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 7 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 8 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 12 seq_no
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 14 seq_no
    FROM dual
    table2 AS
    SELECT 1435177 qte_id, 1 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 2 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 3 seq_no, 0 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 4 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 5 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 7 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 8 seq_no, 98500 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 11 seq_no, 59300 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 12 seq_no, 59300 cmmt_curr_amt
    FROM dual
    UNION ALL
    SELECT 1435177 qte_id, 14 seq_no, 59300 cmmt_curr_amt
    FROM dual
    SELECT qte_id, t2_seq_no, cmmt_curr_amt - cmmt_curr_lag diff
    FROM
    (SELECT t2.qte_id, t2.seq_no t2_seq_no, t1.seq_no t1_seq_no, cmmt_curr_amt, LAG(cmmt_curr_amt,1,0) OVER (PARTITION BY t2.qte_id ORDER BY t2.seq_no) cmmt_curr_lag
    FROM table1 t1, table2 t2
    WHERE t2.qte_id = t1.qte_id (+)
    AND t2.seq_no = t1.seq_no(+)
    ORDER BY 1,2
    WHERE t1_seq_no IS NOT NULL
    ORDER BY 1,2
    QTE_ID T2_SEQ_NO DIFF
    1435177 2 0
    1435177 5 0
    1435177 7 0
    1435177 8 0
    1435177 12 0
    1435177 14 0

  • Query  -  please help - Urgent

    Hi,
    Currently we have a query (Created on Multicube - one is sales plan cube and another is Sales actuals).
    When we run the query on a particular day (example: 7/10/2006), the report looks like as below:
    Cal.Year/Month       Sales plan     Sales Actuals
    Jan'06               310             305
    Feb'06               280             277
    Mar'06               310             309
    Apr'06               300             300
    MAy'06               310             305
    June'06              300             305
    July'06              310             90
    The current report shows the whole month's Sales Plan (July'06 - 310) with Actual Sales for 9 days(for July'06 - 90). Whereas, We want to compare the sales Plan (first 9 days of the month) against the sales actuals for 9 days.
    We do not have 0calday in both the cubes. Please help.
    Thanks,

    David,
    As rightly pointed out , you cannot get the plan value for 9 days if you do not have 0calday. However some things you can do...
    1. Have a virtual KF which gets the number of days from the current calendar day ( system date ) to the first date of the month - this can be written with a simple exit since the first date is fixed.
    2. calculate the plan value from that - pro rated plan value = plan value for month / number of days in month * number of days elapsed. If need be have the virtual KF return the ratio of number of days elapsed/number of days in the month
    3. Now you can do the comparison assuming that the value of actuals will be till the current date and not beyond.
    This is assuming that you can write a Virtual KF and the number of records is less enough to avoid any performance issues
    Hope it helps..
    Arun
    Assign points if helpful
    Message was edited by: Arun Varadarajan

Maybe you are looking for

  • How do I get my Mac air to recognise my HP c4580 printer?

    I am trying to print an article (stored with Pages) using an HP photosmart printer C4580 but it says it is searching for the printer (which is connected by USB to the Mac Air) but cannot find it. Also I can't seem to find Bonjour (which I have always

  • HP D5640 printer prints green instead of black, but only when printing onto printable discs

    Hi there,  im wondering/hoping that someone can help me solve this,  basically my printer works fine when im printing on paper, but when i put a disc in and try to print anything with black in, the black comes out as a bottle green colour, this has h

  • OMW supports sybase ASE 15 to Oracle 10g migration?

    Hi, I want to migrate data from Sybase ASE 15 to Oracle 10g. Can i use OMW for this purpose if not then what is the other way of doing it? Kind regards Ankit

  • Nokia prime place in the service can not change th...

    nokia prime place in the service can not change the company's address. How can this be possible???? I received a reply from Nokia that I need to first go to here.com, look for my company and my company COMPLETELY removed. Then, to create a company AG

  • Firmware for n70 up to date but it's not

    Hi can somebody help me. am with 3 network, ive been to nika repair center's for new frimware update for my phone N70 but no luck as 3network dont provide that up date only update they is which is v2.0539.1.2. ive download the nokia updater but it sa