SQL Query : sum only numbers in row - need help

Pls see data below. I would like to display only 8 rows and BRH CCN rows should be merged together and displayed as - BRH 98 2 instead of 2 rows. The rest of the rows should remain as is. Please help.
CCN     SERVICES     MANUFACTURING
ASL     138     NA
BRH     98     NA
BRH     NA     2
C10000     NA     0
C40000     NA     0
DAO     NA     10
E10000     NA     0
I10000     NA     0
M10000     NA     0
Edited by: auxilia on Nov 7, 2009 6:25 AM

with t as (
           select 'ASL' ccn,'138' services,'NA' manufacturing from dual union all
           select 'BRH','98','NA' from dual union all
           select 'BRH','NA','2' from dual union all
           select 'C10000','NA','0' from dual union all
           select 'C40000','NA','0' from dual union all
           select 'DAO','NA','10' from dual union all
           select 'E10000','NA','0' from dual union all
           select 'I10000','NA','0' from dual union all
           select 'M10000','NA','0' from dual
select  ccn,
        nvl(max(case services when 'NA' then null else services end),'NA') services,
        nvl(max(case manufacturing when 'NA' then null else manufacturing end),'NA') manufacturing
  from  t
  group by ccn
CCN    SERVICES             MANUFACTURING
E10000 NA                   0
C10000 NA                   0
BRH    98                   2
M10000 NA                   0
C40000 NA                   0
ASL    138                  NA
I10000 NA                   0
DAO    NA                   10
8 rows selected.
SQL>   SY.
Edited by: Solomon Yakobson on Nov 7, 2009 6:46 AM

Similar Messages

  • Exists (SQL query returns at least one row) condition with MAX on 10.2.0.4

    I just wanted to note this on the forum..
    I'm using Apex 3.0.1.00.08
    My DEV environment has just been upgraded from 10g version 10.2.0.2.0 to 10.2.0.4.0.
    I created the following process to select some values into some items on my Form page.
    select  MAX(STAT_YEAR+1)
          , pcd.pct_id
          , pcd.stat_type_id
          , pcd.stat_period_type_id
          , pcd.measurement_id
    into    :P11_STAT_YEAR
          , :P11_PCT_ID
          , :P11_STAT_TYPE_ID
          , :P11_STAT_PERIOD_TYPE_ID
          , :P11_MEASUREMENT_ID
    from  plant_commodity_data pcd
        , stat_type stt
    where pcd.STAT_TYPE_ID = stt.STAT_TYPE_ID
      and pcd.pct_id = :P0_PCT_ID
      and stt.stat_type = 'PRD'
    group by pcd.pct_id
          , pcd.stat_type_id
          , pcd.stat_period_type_id
          , pcd.measurement_id;The process should run conditionally if there was at least one row to select...
    So I copied the SQL into the Process condition, removed the "into" section and set the condition type to be "Exists (SQL query retruns at least one row) ...
    select  MAX(STAT_YEAR+1)
          , pcd.pct_id
          , pcd.stat_type_id
          , pcd.stat_period_type_id
          , pcd.measurement_id
    from  plant_commodity_data pcd
        , stat_type stt
    where pcd.STAT_TYPE_ID = stt.STAT_TYPE_ID
      and pcd.pct_id = :P0_PCT_ID
      and stt.stat_type = 'PRD'
    group by pcd.pct_id
          , pcd.stat_type_id
          , pcd.stat_period_type_id
          , pcd.measurement_id;This worked perfectly until the DEV environment was upgraded from 10g version 10.2.0.2.0 to 10.2.0.4.0.
    The condition would fire even if there were no rows returning.
    I pasted the condition code into SQL Developer connected to the DEV (10.2.0.4.0) environment and it returned no rows.
    To solve the problem, I removed the MAX.
    You can test this using this code if you have access to the two versions... select  MAX(1)
          , sysdate
          , 'Gus'
    from  dual
    where 1 = 2
    group by sysdate, 2Now I know the MAX isn't required in the condition as I'm just trying to find out if any rows exist, but it was there as I copied the code in... I was just wondering why this happened between 10.2.0.2.0 and 10.2.0.4.0?
    Gus..

    Hi Gus,
    try to execute
    select count(*) from dual where exists (select  MAX(1)
          , sysdate
          , 'Gus'
    from  dual
    where 1 = 2
    group by sysdate, 2)in SQL Developer. The above statement is generated by APEX for an "Exists (SQL query retruns at least one row)". Can't test it, because I don't have a 10.2.0.4.0 at hand.
    Does SQL Developer now show the same behavior?
    Patrick
    My APEX Blog: http://www.inside-oracle-apex.com
    The APEX Builder Plugin: http://builderplugin.oracleapex.info/
    The ApexLib Framework: http://apexlib.sourceforge.net/

  • Need Sql Query that Suppress the repeating rows.

    Hi Sir,
    I have below data set.
    DeptID  Sum(SAL) Avg(Sal)
      10      1300      1300
      10      2450      1300
      10      5000      1300
      20      800        800
      20      1100      800
      20      2975      800
      20      3000      800
      30      800        800
      30      1100      800
      30      2975      800
      40      3000      800
      40      4000      900
    I Need  SQL query that can remove the duplicate or suppress.Just like below data set.
    Output:
    DeptID  Sum(SAL) Avg(Sal)
      10      1300      1300
                2450      1300
                5000      1300
      20       800        800
                1100      800
                2975      800
                3000      800
                800        800
      30      1100      800
                2975      800
      40      3000      800
               4000      900
    Could you please help me on this..

    Your requiremnt is more of a reporting request which should not be handled in SQL. For example in SQL Plus you can use the BREAK command
    SQL> break on deptno
    SQL> select deptno, ename from emp order by deptno;
        DEPTNO ENAME
            10 KING
               CLARK
            20 JONES
               SCOTT
               SMITH
               ADAMS
            30 TURNER
               MARTIN
               WARD
               ALLEN
               BLAKE
    11 rows selected.
    SQL>
    But if you still wish to do it in SQL you can use ROW_NUMBER analytic function and achieve it like this
    select decode(rno, 1, deptno) deptno
         , ename
      from (
              select row_number() over(partition by deptno order by ename) rno
                   , deptno
                   , ename
                from emp

  • SQL Query (PL/SQL Function Body returning SQL query) doesn't return any row

    I have a region with the following type:
    SQL Query (PL/SQL Function Body returning SQL query).
    In a search screen the users can enter different numbers, separated by an ENTER.
    I want to check these numbers by replacing the ENTER, which is CHR(13) || CHR(10) I believe, with commas. And then I can use it like this: POD IN (<<text>>).
    It's something like this:
    If (:P30_POD Is Not Null) Then
    v_where := v_where || v_condition || 'POD IN (''''''''||REPLACE(''' || :P30_POD || ''', CHR(13) || CHR(10), '','')||'''''''''')';
    v_condition := ' AND ';
    End If;
    But the query doesn't return any rows.
    I tried to reproduce it in Toad:
    select * from asx_worklistitem
    where
    POD IN (''''||REPLACE('541449200000171813'||CHR(13) || CHR(10)||'541449206006341366', CHR(13) || CHR(10), ''',''')||'''')
    ==> This is the query that does't return any rows
    select (''''||REPLACE('541449200000171813'||CHR(13) || CHR(10)||'541449206006341366', CHR(13) || CHR(10), ''',''')||'''')
    from dual;
    ==> This returns '541449200000171813','541449206006341366'
    select * from asx_worklistitem
    where pod in ('541449200000171813','541449206006341366');
    ==> and when I copy/paste this in the above query, it does return my rows.
    So why does my first query doesn't work?
    Doe anyone have any idea?
    Kind regards,
    Geert
    Message was edited by:
    Zorry

    Thanks for the help.
    I made it work, but via the following code:
    If (:P30_POD Is Not Null) Then
    v_pods := REPLACE(:P30_POD, CHR(13) || CHR(10));
    v_where := v_where || v_condition || 'POD IN (';
    v_counter := 1;
    WHILE (v_counter < LENGTH(v_pods)) LOOP
    v_pod := SUBSTR(v_pods, v_counter, 18);
    IF (v_counter <> 1) THEN
    v_where := v_where || ',';
    END IF;
    v_where := v_where || '''' || v_pod || '''';
    v_counter := v_counter + 18;
    END LOOP;
    v_where := v_where || ')';
    v_condition := ' AND ';
    End If;But now I want to make an update of all the records that correspond to this search criteria. I can give in a status via a dropdownlist and that I want to update all the records that correspond to one of these POD's with that status.
    For a region you can build an SQL query via PL/SQL, but for a process you only have a PL/SQL block. Is the only way to update all these records by making a loop and make an update for every POD that is specified.
    Because I think this will have a lot of overhead.
    I would like to make something like a multi row update in an updateable report, but I want to specify the status from somewhere else. Is this possible?

  • Using Parameters in SQL-Query not only in where clauses

    Hi,
    I try to use Publisher parameters in the SQL Query from a Data Set.
    All of them have default values.
    So far, this is no problem, unless I try to use such a parameter value as an ordinary attribute value:
    >
    select
         case when (:pv_Group = 'no') then "DM15D_BETRIEBSTEIL"."BETR_TEIL"
                                            else :pv_some_Text end                                                                  as Betr_Teil,
         case when (:pv_Group = 'no') then "DM15D_BETRIEBSTEIL"."SUVA_NR_FORM"
                                            else :pv_some_Text end                                                        as Suva_Nr,
         case when (:pv_Group = 'no') then "DM15K_RIS_FAKTEN_PRO_BTT_JHR"."RIS_VOLLBESCH"
                                            else sum("DM15K_RIS_FAKTEN_PRO_BTT_JHR"."RIS_VOLLBESCH") end      as Vollbesch
    from "GDWH05"
    where
         "DM15D_BETRIEBSTEIL"."SUVA_NR_FORM" in (:pv_nim100)
         fetch first 65001 rows ONLY
    >
    The parameters 'pv_Group' and 'pv_nim100' are working fine. (when or where clauses)
    The parameter 'pv_some_Text' unfortunately not. (simple literals)
    When I try to validate the above SQL, I get the following Error:
    <font color="red">
    java.io.IOException: prepare query failed[nQSError: 43113] Message returned from OBIS. [nQSError: 46033] Datatype: 25 is not supported.
    </font>
    After use a cast function:
    >
    else CAST(:pv_some_Text AS CHARACTER)
    >
    I get this ERROR:
    <font color="red">
    java.io.IOException: prepare query failed[nQSError: 43113] Message returned from OBIS. [nQSError: 19002] Incorrect use of parameters. The parameters used in CAST cannot be resolved without ambiguity.
    </font>
    We use OBIEE 11.1.1.6.4 on a Win64-System.
    Thank's for any help.

    Hi Alex,
    let's leave away any unnecessary details.
    This is the SQL, inserted in the window 'Edit Data Set' of BIP Data Model:
    >
    select
         '--1'     as Betr_Teil,
         '--2'      as Suva_Nr,
         sum("GDWH05"."DM15K_RIS_FAKTEN_PRO_BTT_JHR"."RIS_VOLLBESCH") as Vollbesch
    from "GDWH05"
    where
         "DM15D_BETRIEBSTEIL"."SUVA_NR_FORM" in ('122-4.4')
         fetch first 65001 rows ONLY
    >
    Everything is fine when I click OK, the script goes back to the metadata.
    Let's try this script with bind values in ORACLE SQL Developer.
    This SQL is the physical part, found in the OBIEE-Log (Log level 5), except the bind values. Therefore we find, in the where clause, the join. In the logical sql, we don't have to join, because it's handled in the Common Enterprise Information Model (CEIM)
    >
    with
    sawith0 as
    select
    sum(t39617.ris_vollbesch) as c1
    from
    dm15d_betriebsteil t39455,
    dm15k_ris_fakten_pro_btt_jhr t39617
    where
    t39455.id_betriebsteil = t39617.id_betriebsteil
    and t39455.suva_nr_form = '122-4.4'
    select
    d1.c1 as c1,
    d1.c2 as c2,
    d1.c3 as c3
    from
    select
    :pv_some_text as c1,
    :pv_some_text as c2,
    sum(d1.c1) as c3
    from
    sawith0 d1
    d1
    where
    rownum <= 65001
    >
    This SQL works fine, even with bind values for 'pv_some_text'.
    But, when using the following SQL in the BIP Data Model:
    >
    select
         :pv_Text as Betr_Teil,
         :pv_Text as Suva_Nr,
         sum("GDWH05"."DM15K_RIS_FAKTEN_PRO_BTT_JHR"."RIS_VOLLBESCH") as Vollbesch
    from "GDWH05"
    where
         "DM15D_BETRIEBSTEIL"."SUVA_NR_FORM" in ('122-4.4')
         fetch first 65001 rows ONLY
    >
    The following ERROR occurs:
    <font color="red">java.io.IOException: prepare query failed[nQSError: 43113] Message returned from OBIS. [nQSError: 46008] Internal error: File server\Query\Optimizer\ServiceInterfaceMgr\Utility\Src\SQOIUTypeVisitor.cpp, line 643.</font>
    In my opinion, either I use a wrong syntax, or BIP has a problem with parsing the script.
    Thank you for your most welcome help.

  • SQL query SUM and AVG

    Hi all,
    I think I'm being really stupid but I'm having SQL query issues.
    I have a table with lots of lines of transactions which all refer to different stores, for example:
    Store..............._Sales_............._Month_
    Reading............200k..............April
    Leeds...............50k................April
    Manchester........70k................May
    Reading............100k..............May
    I need to come up with the average Sales for the combined months as a total. I.e. the average Store revenue for a given period is 200k+ 50k + 70k +100k / *3* (As there are only 3 unique stores) - hopefully this makes sense!
    So essentially I am trying to do both a SUM query (and grouping by store) and then outputting the average of all stores together. Is this possible?
    Thank you,

    Hello,
    This query returns 140 what seems to be the correct value:
    with data as
    ( select 'Reading' Store
      , 200 sales
      from dual
      union
      select 'Leeds'
      , 50
      from dual
      union
      select 'Manchester'
      , 70
      from dual
      union
      select 'Reading'
      , 100
      from dual
    select sum(sales)
    , count( distinct store )
    , sum(sales)/count(distinct store)
    from dataThere are multiple options:
    1. You can get two IR's on one page (by using IFRAMEs - search for that word on the Forum)....
    2. You create another region with the query above and position that just below the report
    3. In the Region Footer call a PL/SQL process (using AJAX) that calculates the value using the query and prints it there (using htp.p)
    Greetings,
    Roel
    http://roelhartman.blogspot.com/
    http://www.bloggingaboutoracle.org/
    http://www.logica.com/
    You can award this reply to your question by marking it as either Helpful or Correct ;-)

  • SQL query to fetch approximately 9000 rows

    Hi,
    I am using the following SQL query in a java class. Using JDBC to connect to an Oracle database using JRUN app server connection pooling.
    SELECT PT.ACCOUNT_NUMBER,PAYMENT_REF_ID, TO_CHAR (PT.DATE_CREATED, 'MM/DD/YYYY'),
    PT.AMOUNT_PAID, DECODE(STATUS_VALUE,'Cancelled','Canceled',STATUS_VALUE)
    FROM EPAY_PAYMENT_TRANSACTIONS PT, EPAY_STATUS_LOOKUP SL
    WHERE
    PT.CLIENT_NAME = 'someclientname'
    AND TO_CHAR(DATE_PAYMENT_SCHEDULED,'MM/DD/YYYY') = '08/28/2006'
    AND PT.PAYMENT_STATUS = SL.STATUS_CODE
    ORDER BY PT.DATE_CREATED DESC
    EPAY_PAYMENT_TRANSACTIONS is a huge table consisting of thousands of rows. There are 4 indexes defined on this table but not on the columns used in the join condition of the query.
    EPAY_STATUS_LOOKUP is a much smaller table mainly used for lookup consisting of some 100 rows.
    The above query fetches approx 9000 records and takes a very long time to execute. Is there any way it can be optimized or we can change some attributes on the tables involved for speeding up the query?
    Thanks in advance,
    Nisha.

    So the task is to avoid FTS on EPAY_PAYMENT_TRANSACTIONSWell, maybe...
    But I believe that we have yet to hear more details about data distribution, execution plans and so on.
    Because it all depends...
    Consider simple testcase:
    SQL> select * from v$version where rownum = 1;
    BANNER
    Oracle9i Enterprise Edition Release 9.2.0.6.0 - Production
    SQL> -- Create "small" table
    SQL>
    SQL>  create table build_t as
      2   select rownum x,
      3          rpad('*', 200, '*') padding
      4     from dual
      5  connect by level <= 100;
    Table created.
    SQL> -- Create "large" table - initially, add 9000 rows which satisfy join criteria
    SQL>
    SQL>  create table probe_t as
      2   select mod(rownum - 1, 100) + 1 x,
      3          dbms_random.value y,
      4          rpad('*', 200, '*') padding
      5     from dual
      6  connect by level <= 9000;
    Table created.
    SQL> -- Now, add some extra rows to large table - "thousands of rows"
    SQL>
    SQL>  insert into probe_t
      2   select rownum + 100,
      3          0,
      4          rpad('*', 200, '*') padding
      5     from dual
      6  connect by level <= 100000;
    100000 rows created.
    SQL> exec dbms_stats.gather_table_stats(user, 'BUILD_T')
    PL/SQL procedure successfully completed.
    SQL> exec dbms_stats.gather_table_stats(user, 'PROBE_T')
    PL/SQL procedure successfully completed.
    SQL> -- Ok, let's measure ...
    SQL>
    SQL> set autot traceonly
    SQL>
    SQL> select a.*, b.*
      2    from build_t a,
      3         probe_t b
      4   where a.x = b.x
      5   order by b.y;
    9000 rows selected.
    Execution Plan
       0      SELECT STATEMENT Optimizer=CHOOSE (Cost=1387 Card=109 Bytes=45017)
       1    0   SORT (ORDER BY) (Cost=1387 Card=109 Bytes=45017)
       2    1     HASH JOIN (Cost=1386 Card=109 Bytes=45017)
       3    2       TABLE ACCESS (FULL) OF 'BUILD_T' (Cost=5 Card=100 Bytes=20400)
       4    2       TABLE ACCESS (FULL) OF 'PROBE_T' (Cost=1355 Card=109000 Bytes=22781000)
    Statistics
              0  recursive calls
              0  db block gets
           3280  consistent gets
              0  physical reads
              0  redo size
         360756  bytes sent via SQL*Net to client
           7096  bytes received via SQL*Net from client
            601  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
           9000  rows processedOn my server, this query finished in less that second and took 3280 LIO to complete.
    Now, which index did you mean to build? I guess it's and index on join column, isn't it?
    If so - let's build it and measure again:
    SQL> create index idx_probe_t on probe_t(x);
    Index created.
    SQL> select /*+ ORDERED USE_NL(a b) */
      2         a.*, b.*
      3    from build_t a,
      4         probe_t b
      5   where a.x = b.x
      6   order by b.y;
    9000 rows selected.
    Execution Plan
       0      SELECT STATEMENT Optimizer=CHOOSE (Cost=211 Card=109 Bytes=45017)
       1    0   SORT (ORDER BY) (Cost=211 Card=109 Bytes=45017)
       2    1     TABLE ACCESS (BY INDEX ROWID) OF 'PROBE_T' (Cost=3 Card=1 Bytes=209)
       3    2       NESTED LOOPS (Cost=210 Card=109 Bytes=45017)
       4    3         TABLE ACCESS (FULL) OF 'BUILD_T' (Cost=5 Card=100 Bytes=20400)
       5    3         INDEX (RANGE SCAN) OF 'IDX_PROBE_T' (NON-UNIQUE) (Cost=2 Card=1)
    Statistics
              0  recursive calls
              0  db block gets
           9124  consistent gets
             21  physical reads
              0  redo size
         360756  bytes sent via SQL*Net to client
           7096  bytes received via SQL*Net from client
            601  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
           9000  rows processedLook at this - 9124 consistent gets, almost three times larger than in case of hash join.
    Full table scan is not something to be avoided at all cost.
    Indexing - sometimes it is good,
    sometimes it's just useless,
    sometimes it only makes things worse...
    It all depends on underlying data distribution. That's why I asked user527580 to tell us more.
    Kind regards.

  • SQL Query for members of dynamic group - Need to include Name, Path and Type

    Hello,
    I built a custom dynamic group that has all my SQL databases in it using SCOM 2012 SP1.  The group works fine as I can see the Name(ie, Database name), Health State, Path (ie, hostname/instance) and Types (ie; SQL 2005).  Now I'm trying to
    build a custom report based off this same information using a SQL query.   I'm no DBA and could use some help.  So far this is what i have
    use
    select
    SourceObjectDisplayName as
    'Group Name',
    TargetObjectDisplayName,TargetObjectPath
    from RelationshipGenericView
    where isDeleted=0
    AND SourceObjectDisplayName
    like
    'SQL_Databases_All'
    ORDERBY TargetObjectDisplayName
    This gets me the Group Name (which i really don't care about), database name, and hostname/instance. What I am missing is the Health State and most importantly the Type (ie, SQL Server 2005 DB, SQL Server 2008DB).
    If someone could assist me here I would appreciate it. I believe I need to do some type of INNER JOIN but have no idea where the SQL type info lives or the proper structure to use. Thanks
    OperationsManager

    Here's the updated Query for OpsMan 2012 R2:
    To find all members of a given group (change the group name below):
    select SourceObjectDisplayName as 'Group Name', TargetObjectDisplayName as 'Group Members' 
    from RelationshipGenericView 
    where isDeleted=0 
    AND SourceObjectDisplayName = 'Agent Managed Computer
    Group' 
    ORDER BY TargetObjectDisplayName

  • SQL Query of an OWB map (row based)

    If I trace a session, execute an OWB map (row based), will the trace file contain the actual SQL query ?
    The problem with me is that when I am executing this row -based OWB map, it is throwing me an error CursorFetchMapTerminationRTV20007 BUT ( plus taking a long time) when I am taking out the intermediate SQL insert query,it is working fine ( and also within a very short period of time)
    Execution status = COMPLETE
    message text = ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    message text = CursorFetchMapTerminationRTV20007
    No. task errors = 0
    No. task warnings = 2
    No. errors = 1
    Since this OWB map (Truncate Insert)  is row based hence I cannot cannot get the back end query from the OWB generated pl/sql package so wondering if I trace the session, check the trace file, may be I will able to see the exact SQL query generated. But wanted to confirm the same.

    Yes, the actual SQL run in the session will be in the trace file.

  • Sql query to get numbers from 0 to 99

    How can we write an sql query to get values from 0 to 99 ... this shouldn't be coming from any table

    Bawer wrote:
    Result: (quickly replied by oracle)
    SQL-Error: ORA-30009 (Not enough memory for connect by - 1GB reserved for oracle on VirtualBox)Oracle version? Takes about 8 seconds on my:
    SQL> select * from v$version
      2  /
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
    PL/SQL Release 10.2.0.4.0 - Production
    CORE    10.2.0.4.0      Production
    TNS for 32-bit Windows: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production
    Elapsed: 00:00:00.14
    SQL> select count(*) from (
      2  SELECT LEVEL - 1
      3  FROM DUAL
      4  CONNECT BY LEVEL <= 10000000
      5  )
      6  /
      COUNT(*)
      10000000
    Elapsed: 00:00:07.76
    SQL> SY.

  • SQL executing error differences with different tools, need help!!

    Hi everyone,
    I'm getting trouble with executing SQL query using SQLdevelopper.
    but it returns the error about bind value parameter in & out doesn't set.
    I also try to same SQL with other tools but only one tool could succeed
    executing which is Osqledit.
    SQL*Developper--------No
    Object Browser--------No
    Sql*Plus-------No
    Osqledit-----Yes
    does anybody know why some sql tools couldn't succeed executing
    same sql such as Osqledit?
    and also in this case often to cause or not?
    thank you for reading and helping me.

    thanks SHUBH I think so too.
    To Colin and others,
    here is my SQL query which wasn't written in English so
    it took me for a while to translate.
    and it very long SQL which my co-worker made it.
    sorry for it unable to read easily tho I need your help.
    in additon, we use bind-values on this SQL.
    select
         shopcode,
         JAN,
         h.lowestestprice,
         h.highestprice,
         h.amount,
         h.totalsales,
         h.retailprice,
         h.aprice,
         h.normalamount,
         h.normaltotalsales,
         h.normalretailprice,
         h.normalaprice,
         h.discountamount,
         h.discounttotalsales,
         h.discountretailprice,
         h.discountaprice,
         h.memberamount,
         h.memberretailprice,
         h.memberretailprice,
         h.memberaprice,
         h.normalreserveamount,
         h.discountreserveamount
    from
              select
              shopcode,
                   JANcode JAN,
                   min(lowestestprice) lowestestprice,
                   max(highestprice) highestprice,
                   sum(amount) amount,
                   sum(totalsales) totalsales,
                   sum(retailprice) retailprice,
                   sum(aprice) aprice,
                   sum(normalamount) normalamount,
                   sum(normaltotalsales) normaltotalsales,
                   sum(normalretailprice) normalretailprice,
                   sum(normalaprice) normalaprice,
                   sum(discountamount) discountamount,
                   sum(discounttotalsales) discounttotalsales,
                   sum(discountretailprice) discountretailprice,
                   sum(discountaprice) discountaprice,
                   sum(memberamount) memberamount,
                   sum(membertotalsales) membertotalsales,
                   sum(memberretailprice) memberretailprice,
                   sum(memberaprice) memberaprice,
                   sum(normalreserveamount)
    normalreserveamount,
                   sum(discountreserveamount)
    discountreserveamount
              from
                   testsalesfact
              where
                   salesdate between :begindate and :enddate
              group by
                   shopcode,
                   JANcode
         ) h
         full outer join
              select
                   shopcode,
                   JANcode,
    min(lowestestprice) lowestestprice,
                   max(highestprice) highestprice,
                   sum(amount) amount,
                   sum(totalsales) totalsales,
                   sum(retailprice) retailprice,
                   sum(aprice) aprice,
                   sum(normalamount) normalamount,
                   sum(normaltotalsales) normaltotalsales,
                   sum(normalretailprice) normalretailprice,
                   sum(normalaprice) normalaprice,
                   sum(discountamount) discountamount,
                   sum(discounttotalsales) discounttotalsales,
                   sum(discountretailprice) discountretailprice,
                   sum(discountaprice) discountaprice,
                   sum(memberamount) memberamount,
                   sum(membertotalsales) membertotalsales,
                   sum(memberretailprice) memberretailprice,
                   sum(memberaprice) memberaprice,
                   sum(normalreserveamount)
    normalreserveamount,
                   sum(discountreserveamount)
    discountreserveamount
              from
                   shopweeksaleswk
              where
                   salesdate between :begindate and :enddate
              group by shopcode, JAN
              union all
              select
                   a.shopcode,
                   a.JAN,
                   min(a.pricewiotax) lowestestprice,
              -     max(a.pricewiotax) highestprice,
                   sum(a.amount) amount,
                   sum(a.totalsales) totalsales,
                   sum(a.retailprice) retailprice,
                   sum(a.aprice) aprice,
                   sum(a.normalamount) normalamount,
                   sum(a.normaltotalsales) normaltotalsales,
                   sum(a.normalretailprice) normalretailprice,
                   sum(a.normalaprice) normalaprice,
                   sum(a.discountamount) discountamount,
                   sum(a.discounttotalsales) discounttotalsales,
                   sum(a.discountretailprice) discountretailprice,
                   sum(a.discountaprice) discountaprice,
                   sum(a.memberamount) memberamount,
                   sum(a.membertotalsales) membertotalsales,
                   sum(a.memberretailprice) memberretailprice,
                   sum(a.memberaprice) memberaprice,
                   sum(a.normalreserveamount)
    normalreserveamount,
                   sum(a.discountreserveamount)
    discountreserveamount
              from (
                   select
                        p.shopcode,
                        substr(p.makedate, 1, 8) salesdate,
                        p.JAN,
                        p.pricewiotax,
                        p.amount amount,
                        trunc(p.amount * p.pricewiotax)
    totalsales,
                        trunc(p.amount * decode
    (p.discountcode, '000000',
                        p.retailprice, p.discountretailprice))
    retailprice,
                        decode(decode
    (p.discountcode, '000000',
    p.retailprice,
                        p.discountretailprice), 0, 0,
                        trunc(p.amount * p.pricewiotax) -
                        trunc(p.amount * decode
    (p.discountcode, '000000',
                        p.retailprice, p.discountretailprice)))
    aprice,               
                        decode(p.discountcode, '000000',
    p.amount, 0) normalamount,
                        decode(p.discountcode, '000000',
                        trunc(p.amount * p.pricewiotax), 0)
    normaltotalsales,
                        decode(p.discountcode, '000000',
                        trunc(p.amount * p.totalsales), 0)
    normalretailprice,
                        decode(p.totalsales, 0, 0, decode
    (p.discountcode, '000000',
                        trunc(p.amount * p.pricewiotax) -
                        trunc(p.amount * p.retailprice), 0))
    normalaprice,
                        decode(p.discountcode, '000000', 0,
    p.amount) discountamount,
                        decode(p.discountcode, '000000', 0,
                        trunc(p.amount * p.pricewiotax))
    discounttotalsales,
                        decode(p.discountcode, '000000', 0,
                        trunc(p.amount *
    p.discountretailprice))
    discountretailprice,
                        decode(p.discountretailprice, 0, 0,
                   decode(p.discountcode, '000000', 0,
                   trunc(p.amount * p.pricewiotax)
                        trunc(p.amount *
    p.discountretailprice))) discountaprice,
                        case when substr(p.cardnumber,
    1, 7) in
    ('4986428', '4986429', '4986554')
                        then p.amount else 0 end
    memberamount,
                        case when substr(p.cardnumber,
    1, 7) in
    ('4986428', '4986429', '4986554')
                        then trunc(p.amount * p.pricewiotax)
    else 0 end membertotalsales,
                        case when substr(p.cardnumber,
    1, 7) in
    ('4986428', '4986429', '4986554') then
                        trunc(p.amount * decode
    (p.discountcode, '000000',
    p.retailprice,
                        p.discountretailprice)) else 0 end
    memberretailprice,
                        case when substr(p.cardnumber,
    1, 7) in
    ('4986428', '4986429', '4986554') then
                        decode(decode
    (p.discountcode, '000000',
    p.retailprice, p.discountretailprice),
    0, 0,
                        trunc(p.amount * p.pricewiotax) -
                        trunc(p.amount * decode
    (p.discountcode,'000000',
                        p.retailprice, p.discountretailprice)))
    else 0 end memberaprice,
                        decode(p.discountcode, '000000',
                   decode(p.discountsec, '07',
    p.amount, 0), 0)
    normalreserveamount,
                        decode(p.discountcode, '000000', 0,
                        decode(p.discountsec, '07',
    p.amount, 0)) discountreserveamount
                   from shopweeksaleswk p
                   where p.salesdate between :begindate
    and :enddate
                   and p.makedate not between
                   :begindate|| '000000 '
    and :enddate|| '000000 '
                   ) a
              group by a.shopcode, a.JAN
              union all     
              select
                   a.shopcode,
                   a.JAN,
                   min(a.pricewiotax) lowestestprice,
                   max(a.pricewiotax) highestprice,
                   sum(a.amount) amount,
                   sum(a.totalsales) totalsales,
                   sum(a.retailprice) retailprice,
                   sum(a.aprice) aprice,
                   sum(a.normalamount) normalamount,
                   sum(a.normaltotalsales) normaltotalsales,
                   sum(a.normalretailprice) normalretailprice,
                   sum(a.normalaprice) normalaprice,
                   sum(a.discountamount) discountamount,
                   sum(a.discounttotalsales) discounttotalsales,
                   sum(a.discountretailprice) discountretailprice,
                   sum(a.discountaprice) discountaprice,
                   sum(a.memberamount) memberamount,
                   sum(a.membertotalsales) membertotalsales,
                   sum(a.memberretailprice) memberretailprice,
                   sum(a.memberaprice) memberaprice,
                   sum(a.normalreserveamount)
    normalreserveamount,
                   sum(a.discountreserveamount)
    discountreserveamount
              from (
                   select
                        p.shopcode,
                        substr(p.makedate, 1, 8) salesdate,
                        p.JAN,
                        p.pricewiotax,
                        p.amount *- 1 amount,
                        trunc(p.amount * p.pricewiotax) *- 1
    totalsales,
                        trunc(p.amount * decode
    (p.discountcode, '000000',
                        p.retailprice, p.discountretailprice)) *-
    1 retailprice,
                        decode(decode
    (p.discountcode, '000000',
                        p.retailprice, p.discountretailprice),
    0, 0,
                        trunc(p.amount * p.pricewiotax) -
                        trunc(p.amount * decode
    (p.discountcode, '000000',
                        p.retailprice, p.discountretailprice))) *-
    1 aprice,
                        decode(p.discountcode, '000000',
    p.amount, 0) *- 1 normalamount,
                        decode(p.discountcode, '000000',
    trunc(p.amount * p.pricewiotax), 0) *
                        - 1 normaltotalsales,
                        decode(p.discountcode, '000000',
    trunc(p.amount * p.retailprice), 0) *
                        - 1 normalretailprice,
                        decode(p.retailprice, 0, 0, decode
    (p.discountcode, '000000',
                        trunc(p.amount * p.pricewiotax) -
                        trunc(p.amount * p.retailprice), 0)) *- 1
    normalaprice,
                        decode(p.discountcode, '000000', 0,
    p.amount) *- 1 discountamount,
                        decode(p.discountcode, '000000', 0,
    trunc(p.amount * p.pricewiotax)) *
                        - 1 discounttotalsales,
                        decode(p.discountcode, '000000', 0,
                        trunc(p.amount *
    p.discountretailprice)) *- 1
    discountretailprice,
                        decode(p.discountretailprice, 0, 0,
    decode(p.discountcode, '000000', 0,
                        trunc(p.amount * p.pricewiotax) -
                        trunc(p.amount *
    p.discountretailprice))) *- 1
    discountaprice,
                        case when substr(p.cardnumber,
    1, 7) in
    ('4986428', '4986429', '4986554')
                        then p.amount *- 1 else 0 end
    memberamount,
                        case when substr(p.cardnumber,
    1, 7) in
    ('4986428', '4986429', '4986554')
                        then trunc(p.amount * p.pricewiotax) *-                                                                1 else 0 end membertotalsales,
                        case when substr(p.cardnumber,
    1, 7) in
    ('4986428', '4986429', '4986554')
                        then trunc(p.amount * decode
    (p.discountcode, '000000',
                        p.retailprice, p.discountretailprice)) *-
    1 else 0 end memberretailprice,
                        case when substr(p.cardnumber,
    1, 7) in
    ('4986428', '4986429', '4986554')
                        then decode(decode
    (p.discountcode, '000000',
    p.retailprice,
    p.discountretailprice), 0, 0,
                        trunc(p.amount * p.pricewiotax) -
    trunc(p.amount *
                        decode(p.discountcode,'000000',
    p.retailprice, p.discountretailprice))) *
                        - 1 else 0 end memberamount,
                        decode(p.discountcode, '000000',
    decode(p.discountsec, '07',
    p.amount, 0), 0)*
                        -1 normalreserveamount,
                        decode(p.discountcode, '000000', 0,
    decode(p.discountsec, '07',
    p.amount, 0))*
                        -1 discountreserveamount
                   from shopweeksaleswk p
                   where p.salesdate not between :begindate
    and :enddate
                   and p.makedate
    between :begindate|| '000000 '
    and :enddate|| '000000 '
                   ) a
              group by a.shopcode, a.JAN
         ) m
         using (shopcode,JAN)
    where
         and h.lowestestprice != m.lowestestprice
         or h.highestprice != m.highestprice
         (h.amount != m.amount
         or h.totalsales != m.totalsales
         or h.aprice != m.aprice
         or h.normalamount != m.normalamount
         or h.normaltotalsales != m.normaltotalsales
         or h.normalretailprice != m.normalretailprice
         or h.normalaprice != m.normalaprice
         or h.discountamount != m.discountamount
         or h.discounttotalsales != m.discounttotalsales
         or h.discountretailprice != m.discountretailprice
         or h.discountaprice != m.discountaprice
         or h.memberamount != m.memberamount
         or h.membertotalsales != m.membertotalsales
         or h.memberretailprice != m.memberretailprice
         or h.memberaprice != m.memberaprice
         or h.normalreserveamount != m.normalreserveamount
         or h.discountreserveamount != m.discountreserveamount)
    order by shopcode, JAN ;

  • I just buy for 35 nis photoshop and lightroom and i can download only the photoshop. i need help

    i just buy for 35 nis photoshop and lightroom and i can download only the photoshop. i need help

    How to report an issue with Your iTunes Store purchase

  • Sql Query : Sum , all the possible combination of rows in a table

    SQL Server 2008 R2
    Sample Table structure
    create table TempTable
    ID int identity,
    value int
    insert into TempTable values(6)
    insert into TempTable values(7)
    insert into TempTable values(8)
    insert into TempTable values(9)
    insert into TempTable values(10)
    I actually want something like below,
    that It returns rows such that, it has the sum of all the remaining rows , as shown below.
    I will ignore the repeated rows.
    6+7
    6+8
    6+9
    6+10
    6+7+8
    6+7+9
    6+7+10
    6+8+9
    6+8+10
    6+9+10 
    6+7+8+9
    6+7+8+9+10

    This makes no sense. What about NULLs? What about duplicate values? You have rows, so you have many combinations. We know that this set totals to 39, so there is no subset totals to half of any odd number.  19.5
    in this case. 
    None of your examples are
    even close! 
     6+7 = 13
    the remaining rows 8+9+10 = 27, so you failed! 
    Want to try again?
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • Howto:  SQL query to obtain latest n rows

    Hey everyone,
    I want to query a subset of a table to return, at most, a certain number of rows. The problem is that 'rowcnt' is not going to be good enough. Here's the situation:
    I have a table with lots of data (15 days worth). It will accumulate over time, but I want to extract, say, only the last 10 rows that were placed into the table. That would be simple, but there is one other catch, there is a key column and I may want to select rows from each "key". So a simple row count won't work :(.
    Here's a sample table:
    key         varchar(10)
    val1        varchar(5)
    val2        varchar(5)
    epoch_time  number(30)Now, this would ALMOST work:
    select key, val1, val2, epoch_time from t where key='KEYVAL1' and rowcnt <= 10 order by epoch_time desc;
    Because the decending sort on epoch_time guarentees that the latest 10 rows will be returned. The problem arises when I want this:
    select key, val1, val2, epoch_time from t where key IN ('KEYVAL1', 'KEYVAL2') and rowcnt <= 10 order by epoch_time desc;
    I intend to get 10 rows (or less) returned for each 'KEYVAL1' and 'KEYVAL2', but since rowcnt is a global count of rows returned this obviously won't work as desired.
    Keep in mind that I could have 5 rows, 10 rows, 1000 rows for a key...so I could be selecting three keys, one with 5 rows, the next with 40 and the next with 7. Therefore, if I did something like assume I'll get back 30 rows (10 per key) this situtation would render that solution invalid because I'd get 5 from the first and 25 from the second and none from the 3rd.
    So, the actual question is, can I make some modification to the above query such that I can get the last 10 rows from both 'KEYVAL1' and 'KEYVAL2'. Possibly changing the order by clause to order by both key and epoch_time and then somehow switching the rowcnt to a value that would be capable of returning itself to 0 upon encountering a new key value?? I think I could do this with PL/SQL, but I'd have to use a static value to hold the row count and last passed key value, and that wouldn't work if more than one query executed at once unless there were some kind of "query specific data" (analogous to thread specific data) that I could utilize.
    If not, I guess I'll have to write a PL/SQL routine to simulate the entire query because I can't use unions (at least I don't think I can) because I have to be capable of using an IN list to determine the key values I desire.
    If anyone has a suggestion, I'd really appreciate it. THANKS!
    Brian Gregg
    Computer Sciences Corp.

    Are you running with 8.1.6 Enterprise Edition or higher? If so, you can solve your problem with Analytic functions, specifically RANK() or DENSE_RANK().
    SELECT * FROM
    ( SELECT key, val1, val2, epoch_time,
           DENSE_RANK() OVER (PARTITION BY key ORDER BY epoch_time DESC) AS ep_rank
    FROM   t
    WHERE  key IN ('KEYVAL1', 'KEYVAL2')
    ) WHERE ep_rank <= 10;Caveat: this code is typed without benefit of database so you may need to check the syntax.
    Cheers, APC

  • SQL Query does not have all rows in ResultSet

    I am using JDK 1.4.2 and an MS Access 2000 database where I attempt to retrieve data and I get in the ResultSet a maximum of around 280 rows, but there are more that seem to be ignored. In Access I can see them using the same query.
    The Statement.getMaxRows() returns 0 and I do not understand why I do not get the other ones. The query is issued using Statement.executeQuery(String), but using execute() does not make a difference, There is just one ResultSet returned.
    I would appreciate your help.

    I had this same problem once because in my Java code I was connecting to our alpha database and when I was running the query manually I was connecting to our production database.
    The only other thing I can think of that might cause this is if on the 281st row there is something wrong with the row and maybe the SQLException isn't being processed? Can't really say without seeing your code, but if you don't have a printStackTrace() or some indicator when a SQLExcpetion is thrown it could be breaking without your knowing. If those aren't the problems maybe you could post your code.

Maybe you are looking for

  • Digitial Signature issues after patching Acrobat 9.5 to 9.5.1.

    After installing the security patch on Acrobat 9.5 Digital signing is not working correctly.  Users are getting error - Bad Format.  Machine that was not patched works fine with a imported digital signature.  Is there a fix for this problem. 

  • How come the Adobe Wiki says Photoshop CS5 inclues Acrobat Pro 9.3 but mine doesn't include it?

    I was reviewing what is supposed to be included with Photoshop CS5 and it says it includes Acrobat Pro 9.3 but mine only contains Photoshop.  Mine is an upgrade from CS3 but should that matter?  The CS5 matrix shows CS5 contains 9.3 but mine doesnt?

  • DVD burner not reaching maximum write speed

    I have a PowerMac G4, and have purchased an internal PIONEER DVD-RW DVR-112D. After contacting the manufacturer and discovering what media to use, all fingers point towards VERBATIM. The drive writes DVD's at 18x speed. I have purchased 16x DVD-R med

  • CSS Balancing Laod too much connection

    Hi Need a feddback about this issue I have two CSS 11503 with load balancing active  on # 03 This machine register an high charge of connection and server Front end are very slow to log in  ( the ICMP reply works ) Did someone can see something wrong

  • E71 and Out of Memory Error

    If I go to Preferences, my E71 announces "Out of memory, try closing some applications", even if I don´t have anything open. Preferences hangs completely, I can get out with Home-button, but the Preferences remain active, and you can´t kill it. When