Query sum of daily maximum

Hi!
I need to query the sum of a daily maximum value but I get an ORA-00936: missing expression with the following query. Why?
select to_char(a.mytimestamp, 'MM/YYYY'),
sum(select max(b.wert) from values b group by trunc(b.mytimestamp))
from values a
group by to_char(a.mytimestamp, 'MM/YYYY')
order by to_char(a.mytimestamp, 'MM/YYYY') desc;
The subselct within sum (select max(b.wert) from values b group by trunc(b.mytimestamp)) works as standalone.
Without the subselect the query works, too!
select to_char(a.zeitstempel, 'MM/YYYY'),
sum(a.wert)
from values a
group by to_char(a.mytimestamp, 'MM/YYYY')
order by to_char(a.mytimestamp, 'MM/YYYY') desc;
Can anybody help?

HI,
something like this
SELECT   to_char(a.mytimestamp, 'MM/YYYY'), (SELECT   sum(max(b.wert))
                                                 FROM values b
                                             GROUP BY trunc(b.mytimestamp))
    FROM values a
GROUP BY to_char(a.mytimestamp, 'MM/YYYY')
ORDER BY to_char(a.mytimestamp, 'MM/YYYY') DESC;edit:
Just to get the syntax right - not sure what you actually want
Regards
Peter
Message was edited by:
Peter Gjelstrup

Similar Messages

  • How to build query to give daily balance across bank accounts? (to then plot in a graph)

    How would one build a query to give daily balance across bank accounts? (to then plot in a graph)
    Assumptions:
    * There is a table TRANSACTIONS which includes columns TRANS_DATE, AMOUNT and BANK_ID. It does NOT include a column for balance. So current balance for a bank account is the sum of the AMOUNTs for that BANK_ID for example. Balance on date XX will be the sum
    of all AMOUNTS for that BANK_ID for all TRANS_DATE's prior and including the date XX.
    * There is not necessarily transactions on every day for each bank
    * Table BANKS which has BANK_ID and TITLE
    Would like a query that gives: Supply StartDate and EndDate for the query:
    Date Bank1Balance Bank2Balance Bank3Balance TotalBalance
    1/1/15 $100 $200 $100 $400
    1/2/15 $200 $200 $100 $500
    etc

    You'll find examples of queries for computing balances in various contexts in Balances.zip in my public databases folder at:
    https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169
    If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.
    The queries in this little demo file return balances per transaction, however, whereas you appear to wish to return balances at close of business per day.  This can easily be done by means of a subquery which sums all transactions to date.  To return
    balances for all dates regardless of whether or not any transactions have been undertaken on the day, an auxiliary calendar table can be introduced into the database to plug the gaps,  The Calendar.zip file in my same OneDrive folder has means of generating
    such a table.
    With the introduction of an auxiliary calendar table into the database a query can then be written to return the balance per customer at close of business per day over the period 2009 - 2012 covered by the data in the Transactions table:
    SELECT CustomerID, Firstname, LastName, calDate,
       (SELECT SUM(TransactionAmount)
         FROM Transactions
         WHERE Transactions.CustomerID = Customers.CustomerID
         AND Transactions.TransactionDate <= Calendar.calDate) AS Balance
    FROM Calendar,Customers
    WHERE calDate BETWEEN #2009-01-01# AND #2012-12-31#
    ORDER BY CustomerID, CalDate;
    Rows for each customer/date are returned by means of the Cartesian product of the Calendar and Customers tables (the latter analogous to your Banks table), and the subquery returns the balance at close of each day by correlating the Transactions table with
    the Customers and Calendar tables, returning the sum of all transactions per customer up to and including the date in question.  In this example credit and debit transactions are expressed as positive and negative values in a single column of course,
    but where separate credit and debit columns are used its merely a case of summing (Credit-Debit), as done in some of the examples in my demo.
    To return the data in a horizontal format per date I'd suggest the use of a report which returns one row per date, and within it a multi-column subreport in across-then down column layout, linking the subreport to the parent report on the date columns.
    Ken Sheridan, Stafford, England

  • Summing and Finding Maximum in 2d array

    So I have an 2d array of numbers and I am trying to find the maximum sum of the rows and give back the row number. So far I can get the sum and the maximum number but I need to give back the row number of the maximum value.
    public static void RowSumRow (int[][] table)
          static int sum[] = new int[5];
          int region=0;
          for (int row=0; row < table.length; row++)
                sum [row] = 0;
                for (int col=0; col < table[row].length; col++)
                   sum [row] = sum [row] + table[row][col];     
          int max = myMethods.maximumSearch(sum);
          System.out.println("The maximal revenue is $" + max + " at region " + region);
        }myMethods.maximumSearch
    public static int maximumSearch(int a[])
         int Max = a[0];
         for (int index=0; index < a.length; index++)
             if(Max < a[index])
              Max = a[index];
            return Max;
       }

    this is correct right?
    (the prarmeter rows is the rows that are to be tested)
    public static void RowSumRow (int[][] table, int rows)
          int sum[] = new int[rows];
          int region=0;
          for (int row=0; row < table.length; row++)
                sum [row] = 0;
                for (int col=0; col < table[row].length; col++)
                   sum [row] = sum [row] + table[row][col];     
          int max = myMethods.maximumSearch(sum);
          for(int x=0; x < sum.length; x++)
              if (max == sum[x])
                region = x-1;
          System.out.println("The maximal revenue is $" + sum[region] + " at region " + region);
        }This does what I want it to do right? Ive tested a few times and it is working, but is it logically correct?

  • 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 Sum

    Hi all, hope in your help.
    This is my table:
    +----+--------+--------+
    | id | field1 | field2 |
    +----+--------+--------+
    |  1 | A1     | 7      |
    |  2 | B1     | 9      |
    |  3 | C1     | 0      |
    |  4 | D1     | 3      |
    |  5 | A2     | 5      |
    |  6 | B2     | 6      |
    |  7 | C2     | 7      |
    |  8 | D2     | 8      |
    +----+--------+--------+I need this output:
    +--------+--------------------+
    | field1 | field2             |
    +--------+--------------------+
    | A2     | 19.230769230769200 |
    +--------+--------------------+
    | B2     | 23,076923076923100 |
    +--------+--------------------+
    | C2     | 26,923076923076900 |
    +--------+--------------------+
    | D2     | 30,769230769230800 |
    +--------+--------------------+and tried this query
    where calculate the value of single field1 (5,6,7,8) divided by the sum of field2 equal to A1, B2, C2 and D2 (26) :
    A = 5/26 * 100 = 19
    B = 6/26 * 100 = 23
    C = 7/26 * 100 = 26
    D = 8/26 * 100 = 30
    SELECT
         field1,
         field2/Sum(field2)*100 as field2
    FROM
         `tbl_t`
    WHERE
         1
    AND field1 IN ('A2', 'B2', 'C2', 'D2');
    +--------+--------------------+
    | field1 | field2             |
    +--------+--------------------+
    | A2     | 19.230769230769234 |
    +--------+--------------------+But the ouput is not what I want, can you help me?
    Thank you
    Any help would be greatly appreciated.
    Edited by: user13264840 on 25-mag-2013 10.33

    WITH data AS
      ( SELECT 1 id , 'A1' Field1 , 7 Field2 FROM dual
    UNION ALL
       SELECT 2 , 'B1' , 9 FROM dual
    UNION ALL
       SELECT 3 , 'C1' , 0 FROM dual
    UNION ALL
       SELECT 4 , 'D1' , 3 FROM dual
    UNION ALL
       SELECT 5 , 'A2' , 5 FROM dual
    UNION ALL
       SELECT 6 , 'B2' , 6 FROM dual
    UNION ALL
       SELECT 7 , 'C2' , 7 FROM dual
    UNION ALL
       SELECT 8 , 'D2' , 8 FROM dual
      Group_data AS
      (SELECT  id ,
        field1    ,
        field2    ,
        SUBSTR ( field1 , regexp_instr ( field1 , '[1234567890]' ) ) groupBy
          FROM data
      SELECT  Field1 ,
       ratio_to_report ( field2 ) over ( partition BY groupby ) * 100 "Field2"
         FROM group_data;

  • Inline Query Sum

    hi
    i am using 2 inline queries, from both queries am getting values in outer query
    i need to find out the sum of amount total.
    here is the query
    select  customer_name,branch, round((revenue)/100000,2) revenue,round(((sum(revenue)- sum(ap_amount))/100000),2) margin, (sum(revenue)- sum(ap_amount))/sum(revenue) as "Margin %" from
    (select                 'AP' Mod,
                            fvv.description branch,
                            glc.segment4 ap_won,
                            sum(aida.amount) ap_amount                          
                           FROM
                            apps.ap_invoice_distributions_all aida,
                            apps.ap_invoices_all aia,
                            apps.fnd_flex_values_vl fvv,
                            apps.gl_code_combinations glc,
                            apps.po_vendors pv
                         WHERE aia.invoice_id = aida.invoice_id
                         AND aida.dist_code_combination_id = glc.code_combination_id
                         AND pv.vendor_id = aia.vendor_id
                         AND aida.set_of_books_id = pv.set_of_books_id
                         AND aia.set_of_books_id = aida.set_of_books_id
                         AND aia.set_of_books_id = 2
                         AND glc.segment4 != '0000000'
                         AND aia.org_id = aida.org_id
                         AND aia.org_id = 92
                         AND aida.posted_flag = 'Y'
                         AND glc.segment2 = fvv.flex_value
                         AND fvv.flex_value_set_id = 1008015
                         AND aida.accounting_date between '01-jun-2010' and '10-jun-2010'
                         AND aia.invoice_amount != 0
                        and glc.SEGMENT1=16
                        group by
                           fvv.description,
                           glc.segment4
                            order by 2)a,
    (SELECT                 
                            'AR' Mod,
                            rc.CUSTOMER_NAME customer_name,
                            rct.interface_header_attribute1 ar_won,
                            sum(rctl.extended_AMOUNT) revenue,
                            fvv.description branch1   
                        FROM apps.ra_customer_trx_lines_all rctl,
                            apps.RA_CUST_TRX_LINE_GL_DIST_ALL rctg,
                            apps.fnd_flex_values_vl fvv,
                            apps.ra_customer_trx_all rct,
                            apps.ra_customers rc,
                            apps.RA_CUST_TRX_TYPES_ALL rctt,
                            apps.gl_code_combinations gcc
                      WHERE rctl.customer_trx_id = rct.customer_trx_id
                        and rct.BILL_TO_CUSTOMER_ID=rc.CUSTOMER_ID
                        and rct.CUSTOMER_TRX_ID=rctg.CUSTOMER_TRX_ID
                        and rctl.CUSTOMER_TRX_LINE_ID=rctg.CUSTOMER_TRX_LINE_ID
                        and rctt.CUST_TRX_TYPE_ID = rct.CUST_TRX_TYPE_ID
                        and gcc.CODE_COMBINATION_ID=rctg.CODE_COMBINATION_ID
                        AND gcc.segment2 = fvv.flex_value
                        AND fvv.flex_value_set_id = 1008015
                        and gcc.SEGMENT3 like '5%'
                        and rctg.ACCOUNT_CLASS='REV' 
                        and rctl.line_type =  'LINE'
                        and rct.complete_flag='Y'
                        and rctg.gl_date between '01-jun-2010' and '10-jun-2010'
                        and rct.ORG_ID=92
                        group by fvv.description,rct.interface_header_attribute1,rc.CUSTOMER_NAME
                        order by 2)b
    where a.ap_won=b.ar_won
    and a.branch = b.branch1
    group by branch,revenue,customer_name
    order by 2  I need to revenue total,
    can you let me know, how to calculate that.
    Thanks,

    Like this:
    select  customer_name,branch, round((revenue)/100000,2) revenue,
            round(((sum(revenue)- sum(ap_amount))/100000),2) margin,
            (sum(revenue)- sum(ap_amount))/sum(revenue) as "Margin %",
            SUM(revenue) OVER() tot_Rev from
    (select                 'AP' Mod,
                            fvv.description branch,
                            glc.segment4 ap_won,
                            sum(aida.amount) ap_amount                          
                           FROM
                            apps.ap_invoice_distributions_all aida,
                            apps.ap_invoices_all aia,
                            apps.fnd_flex_values_vl fvv,
                            apps.gl_code_combinations glc,
                            apps.po_vendors pv
                         WHERE aia.invoice_id = aida.invoice_id
                         AND aida.dist_code_combination_id = glc.code_combination_id
                         AND pv.vendor_id = aia.vendor_id
                         AND aida.set_of_books_id = pv.set_of_books_id
                         AND aia.set_of_books_id = aida.set_of_books_id
                         AND aia.set_of_books_id = 2
                         AND glc.segment4 != '0000000'
                         AND aia.org_id = aida.org_id
                         AND aia.org_id = 92
                         AND aida.posted_flag = 'Y'
                         AND glc.segment2 = fvv.flex_value
                         AND fvv.flex_value_set_id = 1008015
                         AND aida.accounting_date between '01-jun-2010' and '10-jun-2010'
                         AND aia.invoice_amount != 0
                        and glc.SEGMENT1=16
                        group by
                           fvv.description,
                           glc.segment4
                            order by 2)a,
    (SELECT                 
                            'AR' Mod,
                            rc.CUSTOMER_NAME customer_name,
                            rct.interface_header_attribute1 ar_won,
                            sum(rctl.extended_AMOUNT) revenue,
                            fvv.description branch1   
                        FROM apps.ra_customer_trx_lines_all rctl,
                            apps.RA_CUST_TRX_LINE_GL_DIST_ALL rctg,
                            apps.fnd_flex_values_vl fvv,
                            apps.ra_customer_trx_all rct,
                            apps.ra_customers rc,
                            apps.RA_CUST_TRX_TYPES_ALL rctt,
                            apps.gl_code_combinations gcc
                      WHERE rctl.customer_trx_id = rct.customer_trx_id
                        and rct.BILL_TO_CUSTOMER_ID=rc.CUSTOMER_ID
                        and rct.CUSTOMER_TRX_ID=rctg.CUSTOMER_TRX_ID
                        and rctl.CUSTOMER_TRX_LINE_ID=rctg.CUSTOMER_TRX_LINE_ID
                        and rctt.CUST_TRX_TYPE_ID = rct.CUST_TRX_TYPE_ID
                        and gcc.CODE_COMBINATION_ID=rctg.CODE_COMBINATION_ID
                        AND gcc.segment2 = fvv.flex_value
                        AND fvv.flex_value_set_id = 1008015
                        and gcc.SEGMENT3 like '5%'
                        and rctg.ACCOUNT_CLASS='REV' 
                        and rctl.line_type =  'LINE'
                        and rct.complete_flag='Y'
                        and rctg.gl_date between '01-jun-2010' and '10-jun-2010'
                        and rct.ORG_ID=92
                        group by fvv.description,rct.interface_header_attribute1,rc.CUSTOMER_NAME
                        order by 2)b
    where a.ap_won=b.ar_won
    and a.branch = b.branch1
    group by branch,revenue,customer_name
    order by 2

  • Sql query : sum of feild between two conditional date

    hi
    I have a table whit 3 field like below. I want to Find the
    total number of hours in mode "run" after the latest mode "decoke".
    field time is for every day and its unique.
    id -- time  --  mode -- hour
    1  --1/1/1 -- run  --  24
    2  -- 1/1/2  -- off  -- 24
    3  -- 1/1/3  decoke  -- 24
    4  -- 1/1/4  -- run  -- 24
    5  -- 1/1/5  -- run -- 24
     this query should add row 4 and 5 , the latest "run" after "decoke"

    Create table #temp
    [id] int identity(1,1)
    , [time] date
    , [mode] varchar(50)
    , [hour] int
    Insert into #temp
    Select '1/1/1' , 'run' ,24
    Union
    Select '1/1/2' , 'off' ,24
    Union
    Select '1/1/3' , 'decoke' ,24
    Union
    Select '1/1/4' , 'run' ,24
    Union
    Select '1/1/5' , 'run' ,24
    -----Here is the Query-------
    ;With Cte_TotalTime
    As
    Select Max(time)[Time] from #temp Where mode = 'decoke'
    Select SUM(a.hour) TotalRunHours from #temp a
    Inner join Cte_TotalTime b on a.[time] > b.[Time]
    And mode = 'run'
    Drop table #temp
    Please mark as answer if it is helpful to you.
    Thanks

  • 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

  • Query to find the maximum and second maximum

    i have a table temptab of 3 columns
    col1 col2 col3
    1 a 08-JAN-08
    1 a 09-JAN-08
    1 a 10-JAN-08
    1 b 10-JAN-08
    1 c 11-mar-08
    1 c 10-mar-08
    i want to select 1st maxm and 2nd maxm of col3 group by (col1,col2)
    o/p will be like
    1 a 10-jan-08 08-jan-08
    1 b 10-jan-08 null
    1 c 11-mar-08 10-mar-08
    select first.col1, first.col2, first.MAX_DATE, second.SEC_DATE from (select a.col1, a.col2, max(a.col3) as MAX_DATE
    from tab1 a group by a.col1, a.col2) first,
    (select b.col1, b.col2, max(b.col3) as SEC_DATE from tab1 b,
    (select a.col1, a.col2, max(a.col3) as MAX_DATE from tab1 a
    group by a.col1, a.col2) c
    WHERE c.col1 = b.col1
    AND c.col1=b.col1
    AND c.MAX_DATE > b.col3
    group by b.col1, b.col2) second
    where first.col1 = second.col1
    and first.col2 = second.col2;
    this is not working..
    please give a query which will do this.

    1. Your query have 3 subqueries where mine only one. Feel free to make your choice regarding the performance.
    2. You cannot have a column named date, unless you use double-quotes
    3. Your query doesn't work as well when you have not any second highest value
    4. Your query return a cartesian product of your table.
    Please check the results of your query (after applying the required modications) with the mine above, and make your choice :
    SQL> with tbl as
      2  (select 1 col1, 'a' col2, to_date('08-JAN-08','DD-MON-YY') col3 from dual union all
      3   select 1 col1, 'a' col2, to_date('09-JAN-08','DD-MON-YY') col3 from dual union all
      4   select 1 col1, 'a' col2, to_date('10-JAN-08','DD-MON-YY') col3 from dual union all
      5   select 1 col1, 'b' col2, to_date('10-JAN-08','DD-MON-YY') col3 from dual union all
      6   select 1 col1, 'c' col2, to_date('11-mar-08','DD-MON-YY') col3 from dual union all
      7   select 1 col1, 'c' col2, to_date('10-mar-08','DD-MON-YY') col3 from dual )
      8  --end of data sample
      9  select t1.col1 ,t1.col2 ,t1.col3,t2.col3
    10  from (select col1,col2,max(col3) col3
    11        from tbl
    12        group by col1,col2 ) t1,
    13       (select col1, col2, col3
    14        from (select col1, col2, col3,
    15                     row_number() over (partition by col1, col2 order by col3 desc) rn
    16              from tbl)
    17       where rn <= 2) t2
    18  where t1.col3 !=t2.col3;
          COL1 C COL3            COL3
             1 c 11-MAR-08       10-JAN-08
             1 a 10-JAN-08       09-JAN-08
             1 b 10-JAN-08       09-JAN-08[i] <-- obviously wrong, there is no 09-JAN-08 for 1b
             1 c 11-MAR-08       09-JAN-08[i] <-- obviously wrong, there is no 09-JAN-08 for 1c
             1 c 11-MAR-08       10-JAN-08[i] <-- obviously wrong, there is no 10-JAN-08 for 1c
             1 a 10-JAN-08       11-MAR-08[i] <-- obviously wrong, there is no 11-MAR-08 for 1a
             1 b 10-JAN-08       11-MAR-08[i] <-- obviously wrong, there is no 11-MAR-08 for 1b
             1 a 10-JAN-08       10-MAR-08[i] <-- obviously wrong, there is no 10-MAR-08 for 1a
             1 b 10-JAN-08       10-MAR-08[i] <-- obviously wrong, there is no 10-MAR-08 for 1b
             1 c 11-MAR-08       10-MAR-08[i] <-- obviously wrong, duplicate row
    10 rows selected.Nicolas.

  • Please help me at this query (sum function)

    hi every one
    if I have column and the datatype is varchar2 and in this column has data like
    number,number mix varchar2,and character
    i want use sum function to summarize then number only but i want to ignore any number with char
    this example
    12
    23
    1q2
    wer
    34rt
    the result=35
    thanks in advance

    Not sure what you need here.
    However here is a sample of what's possible:
    SQL> WITH test_data AS
      2  (
      3          SELECT '12' AS DAT FROM DUAL UNION ALL
      4          SELECT '23' AS DAT FROM DUAL UNION ALL
      5          SELECT '1q2' AS DAT FROM DUAL UNION ALL
      6          SELECT '34rt' AS DAT FROM DUAL
      7  )
      8  SELECT  SUM(DAT)
      9  FROM    test_data
    10  WHERE   NOT REGEXP_LIKE(DAT,'[^[:digit:]]')
    11  /
      SUM(DAT)
            35

  • Query to select Nth Maximum

    Hi,
    Can U tell me how to find the nth maximum salary of EMP table.....
    similarly i want to find out the employees with top 5 salaries from EMP table.
    Thanks in Advance
    Nanaji/.

    This is an old chestnut and you could have found the answer for yorself by using the Search forum facility.
    The easiest way of doing this, although not necessarily the most efficient, is to use inline views:
    SELECT * FROM
       ( SELECT empno, sal, rownum r  FROM
          ( SELECT empno, sal
            FROM   emp
            ORDER  BY sal DESC ) )
    WHERE r = 5
    SELECT * FROM
       ( SELECT empno, sal, rownum r  FROM
          ( SELECT empno, sal
            FROM   emp
            ORDER  BY sal DESC ) )
    WHERE r <= 5
    Cheers, APC

  • 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

  • Daily Payment Query

    Hi Experts,
    I have got query to see daily cheque payment history which only related prior date's invoice from today.
    However, I am stuck to get right result when there are multiple seperate payment history for same customer.
    I appreciate for any helps in advance.
    SELECT
    'Cheque',
    T0.DocDate as 'Posting Date',
    T0.DocNum as 'AR Invoice Number',
    T0.CardName,
    T1.DocDate as 'Payment Date',
    T1.DocNum as 'Incoming Payment Number ',
    T0.DocTotal as 'AR Invoice Total',
    (T0.DocTotal - T0.PaidToDate) as 'AR Sum',
    CASE
    WHEN T0.ObjType = 13 THEN 'ARIV'
    WHEN T0.ObjType = 14 THEN 'ARCR'
    ELSE 'N/A'
    END AS 'Doc Type',
    CASE
    WHEN T1.Series = 12 THEN 'IPAY'
    WHEN T1.Series = 15 THEN 'OPAY'
    ELSE 'N/A'
    END AS 'Doc Series',
    T0.ObjType,
    T1.Series,
    CASE
    WHEN T1.TrsfrSum > T0.DocTotal THEN T1.TrsfrSum
    ELSE T1.TrsfrSum
    END AS 'TrsfrSum',
    CASE
    WHEN T1.CashSum > T0.DocTotal THEN T0.PaidToDate
    ELSE T1.CashSum
    END AS 'CashSum',
    CASE
    WHEN T1.CheckSum > T0.DocTotal THEN T0.PaidToDate
    WHEN T1.CheckSum < T0.DocTotal THEN T1.CheckSum
    ELSE T1.CheckSum
    END AS 'CheckSum',
    CASE
    WHEN T1.CreditSum > T0.DocTotal THEN T0.PaidToDate
    ELSE T1.CreditSum
    END AS 'CreditSum',
    CASE
    WHEN T1.CheckSum > T0.DocTotal THEN T0.PaidToDate
    WHEN T1.CheckSum < T0.DocTotal THEN T1.CheckSum
    ELSE T1.CheckSum
    END AS  'Paid To Date',
    T2.CheckNum as 'Cheque Number'
    FROM OINV T0
    LEFT JOIN ORCT T1 ON T0.ReceiptNum = T1.DocEntry
    LEFT JOIN RCT1 T2 ON T1.DocNum = T2.DocNum
    Left Join OSLP T3 ON T3.SlpCode= T0.SlpCode
    WHERE T1.CheckSum>0 AND DateDiff(D,T1.DocDate,GetDate()) = 0 AND DateDiff(D,T0.DocDate,GetDate()) > 0

    Hi,
    Check this whether this is what you require :
    SELECT
    'Cheque',
    T0.DocDate as 'Posting Date',
    T0.DocNum as 'AR Invoice Number',
    T0.CardName,
    T1.DocDate as 'Payment Date',
    T1.DocNum as 'Incoming Payment Number ',
    T0.DocTotal as 'AR Invoice Total',
    (T0.DocTotal - T0.PaidToDate) as 'AR Sum',
    CASE
    WHEN T0.ObjType = 13 THEN 'ARIV'
    WHEN T0.ObjType = 14 THEN 'ARCR'
    ELSE 'N/A'
    END AS 'Doc Type',
    CASE
    WHEN T1.Series = 12 THEN 'IPAY'
    WHEN T1.Series = 15 THEN 'OPAY'
    ELSE 'N/A'
    END AS 'Doc Series',
    T0.ObjType,
    T1.Series,
    CASE
    WHEN T1.TrsfrSum > T0.DocTotal THEN T1.TrsfrSum
    ELSE T1.TrsfrSum
    END AS 'TrsfrSum',
    CASE
    WHEN T1.CashSum > T0.DocTotal THEN T0.PaidToDate
    ELSE T1.CashSum
    END AS 'CashSum',
    CASE
    WHEN T1.CheckSum > T0.DocTotal THEN T0.PaidToDate
    WHEN T1.CheckSum < T0.DocTotal THEN T1.CheckSum
    ELSE T1.CheckSum
    END AS 'CheckSum',
    CASE
    WHEN T1.CreditSum > T0.DocTotal THEN T0.PaidToDate
    ELSE T1.CreditSum
    END AS 'CreditSum',
    CASE
    WHEN T1.CheckSum > T0.DocTotal THEN T0.PaidToDate
    WHEN T1.CheckSum < T0.DocTotal THEN T1.CheckSum
    ELSE T1.CheckSum
    END AS  'Paid To Date',
    T2.CheckNum as 'Cheque Number'
    FROM OINV T0
    LEFT JOIN ORCT T1 ON T0.ReceiptNum = T1.DocEntry
    LEFT JOIN RCT1 T2 ON T1.DocNum = T2.DocNum
    Left Join OSLP T3 ON T3.SlpCode= T0.SlpCode
    WHERE T1.CheckSum >0
    AND DateDiff(DD,T1.DocDate,GetDate()) >= 0
    ORDER BY t0.docnum
    Kind Regards,
    Jitin
    SAP Business One Forum Team

  • Daily Sales Total Comparison Query

    Hi Experts,
    I'm trying to make a query to get daily sales total for week and wish to make a graph.
    However, if there is no figures in credit note or Down payment invoice or invoice then query seems not showing any figures for particular date.
    I would be appreciated if anyone help this.
    SELECT DISTINCT
    GetDate(),
    SUM (DISTINCT T0.DocTotal) AS 'Daily INV Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE DateDiff(D,T0.DocDate,GetDate())=0 AND DateDiff(D,T1.DocDate,GetDate())=0 AND DateDiff(D,T2.DocDate,GetDate())=0
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 1, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 1, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 1, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 1, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 2, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 2, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 2, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 2, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 3, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 3, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 3, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 3, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 4, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 4, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 4, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 4, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 5, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 5, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 5, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 5, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 6, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 6, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 6, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 6, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 7, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 7, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 7, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 7, 0))

    Could you let me know how to make pivot query?
                        AR INV TOTAL  |  AR Down Payment Total  | AR Credit Total  | (AR INV TOTAL+AR DP TOTAL-AR CREDIT TOTAL)
    Today's Sales
    Yesterday
    Until Week Before

  • How to Round on Daily bases in a Query.

    Hi All,
    I have a query which sum the columns between two dates I want to round the sum on the daily bases. My query is written below.
    SELECT sum(S_QTY * S_RATE), sum(S_QTY * S_TOT)
    FROM SAUDA
    WHERE S_PARTY = 'A080' AND S_CITY = 'Z' AND S_TYPE = 'S' AND S_BILL_TYPE = 'd' AND
    S_DATE BETWEEN '2005-03-30-00.00.00' AND '2006-03-31-00.00.00' AND UPPER(S_CITY) != 'F';
    SUM(S_QTY*S_RATE) SUM(S_QTY*S_TOT)
    3700 2.775
    What I want is to round the sum on daily bases, means the values which I am getting is the total sum but not the individual dates. Try to help me.
    SKM

    Try this:
    SELECT sum(round(S_QTY) * S_RATE), sum(round(S_QTY) * S_TOT)
    FROM SAUDA
    WHERE S_PARTY = 'A080' AND S_CITY = 'Z' AND S_TYPE = 'S' AND S_BILL_TYPE = 'd' AND
    S_DATE BETWEEN '2005-03-30-00.00.00' AND '2006-03-31-00.00.00' AND UPPER(S_CITY) != 'F';
    I hope this will solve ur problem.

Maybe you are looking for