SUM of columns in lines

Dear members,

Suppose I have a command name table and this table has 3 columns by name order_number, travel and net_weight.

The data are as follows:
Trip    order#     net_weight
----------------------------------------------------
1876    1234          450
1876    5678          300
7865    6783          250
7865    9738          350
Of the above, you can see that there are 2 lines, I need to write a SQL in such a way that my data should like below:

Trip   order#  net_weight      sum
------------------------------------------------------
1876    1234          450         750
1876    5678          300         750
7865    6783          250         600
7865    9738          350         600
The new sum of column is the sum of net_weight based on the trip.

sum = 750-> (450 + 300 based on trip 1876)
sum = 600-> (250 + 350 based on trip 7865)


How can I write a SQL query to get the data as described above.

Thank you
Sandeep

Use analytical SUM:

with t as (
           select 1876 trip,1234 order#,450 net_weight from dual union all
           select 1876,5678,300 from dual union all
           select 7865,6783,250 from dual union all
           select 7865,9738,350 from dual
          )
-- end of on-the-fly data sample
select  trip,
        order#,
        net_weight,
        sum(net_weight) over(partition by trip) total_net_weight
  from  t
  order by trip
/

      TRIP     ORDER# NET_WEIGHT TOTAL_NET_WEIGHT
---------- ---------- ---------- ----------------
      1876       1234        450              750
      1876       5678        300              750
      7865       6783        250              600
      7865       9738        350              600

SQL> 

SY.

Tags: Database

Similar Questions

  • How to query the total number of columns and lines filled with data?

    How to get the number of rows and columns in Exel file data using Excel report?

    Since you have posted this question in the forum of LabWindows/CVI, I guess you want to know how to do with CVI.

    You need to know how to open and activate the Excel data file.

    Depending on the function returns the total number of columns and lines col_count row_count, respectively.

    -----------------------------------------------------------------------------------------------------------------------------------------

    int CountColumnsAndRows (void)
    {
    Error HRESULT = 0;
    CAObjHandle rangeCurrentRegionHandle = 0;
    CAObjHandle rangeColumnsHandle = 0;
    CAObjHandle rangeRowsHandle = 0;
      
    unsigned long col_count = 0, row_count = 0;
      
    Must use the 'A1' property and CruuentRegion count the total of columns and lines, including the drafts!
      
    error = CA_VariantSetCString (& MyCellRangeV, 'A1');
      
    error = Excel_WorksheetRange (ExcelWorksheetHandle, NULL, MyCellRangeV, CA_DEFAULT_VAL, & ExcelRangeHandle);
    If (error<0) goto="">
      
    error = Excel_GetProperty (ExcelRangeHandle, & ErrorInfo, Excel_RangeCurrentRegion, CAVT_OBJHANDLE, & rangeCurrentRegionHandle);
    If (error<0)  goto="" error="">
      
    error = Excel_GetProperty (rangeCurrentRegionHandle, & ErrorInfo, Excel_RangeColumns, CAVT_OBJHANDLE, & rangeColumnsHandle);
    If (error<0)  goto="" error="">
      
    error = Excel_GetProperty (rangeColumnsHandle, & ErrorInfo, Excel_RangeCount, CAVT_LONG, & col_count);
    If (error<0) goto="">
      
    error = Excel_GetProperty (rangeCurrentRegionHandle, & ErrorInfo, Excel_RangeRows, CAVT_OBJHANDLE, & rangeRowsHandle);
    If (error<0)  goto="" error="">
      
    error = Excel_GetProperty (rangeRowsHandle, & ErrorInfo, Excel_RangeCount, CAVT_LONG, & row_count);
    If (error<0) goto="">

    Error:

    CA_VariantClear (& MyCellRangeV);
    CA_VariantClear (& MyVariant);
    ClearObjHandle (& ExcelRangeHandle);
    ClearObjHandle (& rangeCurrentRegionHandle);
    ClearObjHandle (& rangeColumnsHandle);
    ClearObjHandle (& rangeRowsHandle);
      
     
    If (error<>
    ReportAppAutomationError (error);
      
    error return;
    }

  • I have the table of 3 columns A, B, C. I want to store the sum of columns A B in the C column without using the DML statements. Can anyone help please how to do. ?

    I have the table of 3 columns A, B, C. I want to store the sum of columns A B in the C column without using the DML statements. Can anyone help please how to do. ?

    11.1 and especially you have virtual column

    SQL> create table t
      2  (
      3     a number
      4   , b number
      5   , c generated always as (a+b) virtual
      6  );
    
    Table created.
    
    SQL> insert into t (a, b) values (1, 2);
    
    1 row created.
    
    SQL> select * from t;
    
             A          B          C
    ---------- ---------- ----------
             1          2          3
    

    Before that, a front insert - trigger

    SQL> create table t
      2  (
      3     a number
      4   , b number
      5   , c number
      6  );
    
    Table created.
    
    SQL> create or replace trigger t_default before insert on t for each row
      2  begin
      3    :new.c := :new.a+:new.b;
      4  end;
      5  /
    
    Trigger created.
    
    SQL> insert into t (a, b) values (1, 2);
    
    1 row created.
    
    SQL> select * from t;
    
             A          B          C
    ---------- ---------- ----------
             1          2          3
    
  • The Master Table column updated based on the sum of column Table detail


    With the help of JDev 11.1.1.6.

    I have a master-detail table based on a link to BC.

    The main table has a column that displays an InputText or an OutputText, based on the value in another column.

    If the InputText is displayed, the user can enter a value and the database will be updated with that value.

    If the OutputText is displayed, it must be a sum of a column in the secondary table.  Also, this value will be written in the database.

    Question:

    How can I fill the OutputText in the main table with the sum of the values in a column in the secondary table?

    The detail table column is a manually entered InputText field.

    Thank you.

    Create a spike in the main table and write in its expression as follows - DetailVoAccessorName.sum ("ColumnName");

    This will calculate the sum of column table detail and then you can set the value of the transient attribute to attribute DB on backup operation

    Ashish

  • sum of column that contains the values in the time format

    Hi all

    I give you a piece of my code below
    SELECT  dif.EMPLOYEE_NUMBER Employee#, dif.FULL_name EmployeeName,
     TO_CHAR(START_DATE,'dd-Mon-rrrr') DOJ ,dif.DEPT_NAME,POSITION , DATE_ENTRAY AttendanceDate,
    
     to_char(DATE_ENTRAY,'DY') day, TO_CHAR(LNE1,'hh24:mi')TimeIn1 ,TO_CHAR(LNE2,'hh24:mi')TimeOut1,
     
     TO_CHAR(LNE3,'hh24:mi')TimeIn2 ,TO_CHAR(LNE4,'hh24:mi')TimeOut2, TO_CHAR(LNE5,'hh24:mi')TimeIn3 ,
    
     TO_CHAR(LNE6,'hh24:mi')TimeOut3, to_char(decode(LNE8,null ,
    
     decode(LNE7,null, decode(LNE6,null, decode(LNE5,null, decode(LNE4,null, decode(LNE3,null,
    
     decode(LNE2,null,LNE2 ,LNE2 ),LNE3 ),LNE4 ),LNE5 ),LNE6 ),LNE7 ),LNE8 ),'hh24:mi')TimeOuts ,
     
     ACT_HOUR Work_Hrs, 
    
    decode(DLY_ABSENT_TYPE,'Late',DED_ABS ,'Late (Deduction)',DED_ABS,'00:00') Late_Hrs,ACT_OVT Over_Time
     FROM jjj_PUNCH_DATA_EMP_LIST trn , 
     jjj_emp_def dif,jjj_PUNCH_CARD_ELEG ele WHERE trn.EMPLOYEE_NUMBER =dif.EMPLOYEE_NUMBER and   
     ele.EMPLOYEE_NUMBER =trn.EMPLOYEE_NUMBER  and   DATE_ENTRAY between '23-Aug-2009' and  '24-Aug-2009' 
    
    I require to find the sum of column  wrk_hrs
    Act_hour or wrk_hrs belongs to table  jjj_PUNCH_DATA_EMP_LIST trn , and the datatype of act_hour is varchar(10 byte)
    the values of column act_hrs, (i require the sum of this column)
    08:00
    07:22
    06:08
    kindly help
    thanking in advance

    concerning
    Oracle user

    Hello

    Thanks for posting the CREATE TABLE and INSERT. That really helps.

    Do you want 19:33 ' as the output? Which makes it look like 19 hours and 33 minutes. Most people would represent 19.33 hours (i.e. 19 more than 1/3 hours) as 19:20 '. Should not the sum of
    ' x: 00 ' and
    'y: 20' be
    "z: 20?

    If you really want 19:33 ', see the solution of Hoek.

    If you really want 19:20 ', then you were on the right track.
    I think that you were trying to do:

    WITH  got_total_hours     AS
    (
         SELECT SUM (   TO_NUMBER (SUBSTR (act_hour, 1, 2))
                    + ( TO_NUMBER (SUBSTR (act_hour, 4, 2))
                      / 60
                      )
                    ) AS total_hours
         from      p
    )
    SELECT        TO_CHAR (FLOOR (total_hours))
           || ':'
           || TO_CHAR ( MOD (total_hours, 1) * 60
                   , 'fm00'
                   )     AS hh_mm
    FROM     got_total_hours
    ;
    

    Looks like you were trying calculate total_hours in a subquery, then use the total_hour alias in a query Super, which is quite accurate. You're just confused on how to write a subquery.
    There are two basic ways to write subqueries:

    (1) WITH clause:

    WITH  sub_query  AS
    (
         SELECT  ...
    )
    SELECT  ...
    FROM      sub_query
    ;
    

    (2) online review

    SELECT     ...
    FROM     (     -- Begin sub_query
         SELECT     ...
         )     -- End sub-query
    ;
    

    Looks like you tried a bit of each method.

    In most cases (this included problem) either one will work.
    Other problems are much easier by using a WITH clause and WITH clauses are usually easier to read and understand, I recommend that you always use a WITH clause rather than views online.

    You can also change the Hoek solution to get the result 19:20 ' without a subquery (or without using any expression complicated, more than once, which is the only reason why I have proposed a subquery). Since date arithmetic Oracle comes from the days, not hours, when the solution of Hoek has calculated the number of hours, you'll have to divide by 24 to get the number of days. If you are using TO_CHAR to fit the time, however, the results can be confusing if the total is 24 hours or more. You might be better off using NUMTODSINTERVAL.

  • convert a single column in lines

    Hi gurus,

    I have a GBA test table is id and name.

    Identification number
    name varchar2

    data are like

    name identity

    1 xy
    2 xyy
    3 mm
    4 pp

    Now my requirement is to convert the IDs of unique column in lines

    i, e my output should be Singel lines like:-1,2,3,4

    How to achieve this result.

    I have no idea to make this request.

    Please help guys.

    Thanks in advance.

    Vijay

    Oh, wait, now I see:

    I think that with clause will not work in this State

    You have not the clause at all.

    You can omit this part and change t in your own table name
    I only used it to generate sample data.
    That's why I commented :) - query real, based on the id is generated above: in the query, to indicate what is happening ;)

    As Alex has already pointed out:
    You only need the part from the line 7 (first example) or line 10 (second example)

    Hands a great coffee to Alex

  • IR - CubeQuery - no limit to the maximum number of columns or lines?

    In IR query processing cube, is there a limit to the maximum number of columns or lines? If so, how?

    When downloading of the results, are there limits to the maximum. lol the columns or lines as well?

    All documentation on its limits?

    Please help someone?

    Columns cannot be more than 256 upto Hyperion Interactive Reporting Studio 9.2.

    No, there is no limit to the maximum number of rows. It depends on the memory and available space on your machine.

    Please correct me if I'm wrong.

    I hope this helps.

    Kind regards

    Manmohan Sharma

  • The sum of 2 columns per line

    Hi all
    Please help how this could be done.

    < product >
    < ProductOption >
    Product1 < ProdName > < / ProdName >
    rate <>
    Modem < ratename > < / ratename >
    < > 15 rate1 < / rate1 >
    < / rates >
    < / ProductOption >
    < ProductOption >
    Product2 < ProdName > < / ProdName >
    rate <>
    Modem < ratename > < / ratename >
    rate < 1 > 12 < / rate1 >
    < / rates >
    < / ProductOption >
    < / product >

    You use above XML structure, how do I display it in this format of table:

    Rate name Product1, Product2 Total
    Modem 15 12 27

    Thanks in advance!

    Can you send me your sample xml

  • Sum of columns using Formcalc

    I created a form with 12 columns and 23 fixed lines. In line 1 of table is a value, the 22 remaining lines allow the user to enter a number, what I would like task is in the footer line power add up the figures in 22 columns and then multiple that by rank value for a total 1.

    Any help would be appreciated.

    Hi Steve,.

    for now, I'll send your pdf with the calculation script.

    You must use the following script in each column in the total field

    -Sterling is the objectname in the user input in column 1 in each row field

    -Various is the objectname in the user input in column 1 in each row field

    - ...

    Column $ = Sum(Row1[*].Sterling)* Row1.TTValue
    Columns $ = Sum(Row1[*].Other)* Row1.OtherValue
    Column $ = Sum(Row1[*].Banking)* Row1.BankingValue
    

    You understand that?

    You must use the [*] the repeated thing, in your, this is the Row1. You can see on the Row1 [0]... Row1 [1). And your amount, for example 4 oder 7.50 needs a static cross reference by column.

    I hope I could help?

    Mandy

  • sum of all the lines before and the line real (11g)

    Hello
    I would like to know how to summarize the value in a column of all the lines before the actual and real. A simple example

    Select the ID of
    (
    Select rownum, 1 double ID
    Union
    Select rownum, 2 double
    Union
    Select rownum, 3 double
    Union
    Select rownum, 4 double
    Union
    Select rownum, 5 double
    Union
    Select rownum, 6 double
    Union
    Select rownum, 7 double
    )

    offers

    ID
    1
    2
    3
    4
    5
    6
    7

    and I would have

    ID SUM
    1 1
    2 3
    3 6
    4 10
    5 15
    6 21
    7 28

    Thanks for any help
    Best regards

    Hello

    Here's a way to do it:

    with t as
    (
    select rownum r ,1 ID from dual
    union
    select rownum,2 from dual
    union
    select rownum,3 from dual
    union
    select rownum,4 from dual
    union
    select rownum,5 from dual
    union
    select rownum,6 from dual
    union
    select rownum,7 from dual
    )
    select id, sum(id) over (order by id) sum
    from t ;
    
            ID        SUM
    ---------- ----------
             1          1
             2          3
             3          6
             4         10
             5         15
             6         21
             7         28
    

    Kind regards
    Sylvie

  • Since the update message "XML Parsing Error: undefined entity location: chrome://browser/content/browser.xul 238, column 5 line number:" all - tried help!

    Since an automatic update ran I get the message

    "XML Parsing Error: undefined entity".

    Location: chrome://browser/content/browser.xul

    Number of the line 238, column 5:

    "< broadcaster-^" < br = "" id = "devtoolsMenuBroadcaster_ChromeDebugger" >
    When I try to open Firefox. I had to go back to Internet Explorer - tried all suggested include removing and then reinstalling Firefox, but still not get Firefox to open - help!
    < / diffuser >

    Can you reproduce in Firefox Safe mode?

    It may be an acceleration addon or material affecting (by making the error).

  • The default value of the cell reference to preserve columns and lines in function number 3.6.1

    I use:

    3.6.1 the numbers

    OSX 10.11.1

    For clarity: afterwards, I use the term 'address of the cell' is the name of the Table, the numbers of row and column that is used to specify a particular cell. Please let me know if there is a name for this.

    Am new on numbers and you want to create a database with two tables whose most variables in "Table 2" are determined by entries in 'Table 1' and vice versa. I use the functions to set up these relationships; However, it is not an obvious geographic relationship between the output in table 2 cell address and the address of the entry in table 1 cell, so I want the address of the cell called in any default function to "Preserve line - True" and "Preserve column - True" still, but don't be found anywhere to set this parameter. Is this possible?

    Thank you in advance your help.

    All the best,

    RA

    It is possible that you are trying to do things with numbers for which it is not designed. It's really good at crunching numbers, but this isn't a database program. If you look at the models in file > New in your menu, you will find good examples of use of numbers. To enter formulas usually you don't have to make a lot of seizure of addresses. You type = activate the formula editor, and then click the cell you want to reference. Numbers then inserts the address for you.

    SG

  • How to draw columns not lines of the spreadsheet?

    Hello world

    It is probably a fundamental issue; But how do I make a data column of graphical waveform drawn rather than data on line?

    I have a spreadsheetwith 6-columns/channel data with approximately 120 000 lines (file .lvm)... I can convert the 2D table to draw; but this screw with my logic when indexing tables etc...

    Do I have to use the XY Chart and manually bring the plot?

    All responses are greatly appreciated.

    Thank you

    Jack

    If you wire a 2D table in the graph, there is an option to right click on the chart to transpose the table for you.

  • How can I know the number of columns and lines on excel file

    How reading the number of columns and rows on an excel file after you open the file in excel with ActiveX.

    Using the example of excel200dem, I read that the file data is excellent.

    However, in this example, I just give the number of lines and columns.

    I want to know the number of lines and columns not giving those in C code automatically.

    Who knows this things? -?

    Help me please.

    Hello

    (1) this forum provides a search option, you must use in doing so, you will find this post

    (2) don't solve you your earlier questions? If so, you must mark responses that have helped you find a solution.

  • SUM OF COLUMN IN A TABLE.

    How can I make the sum of the rows in a table in the apex oracle 4.2.6.

    I want to perform validations on adj_amount column in a table. If the adj_amount column for the selected row is < = 0 then return false.

    How can I choose the name of the column of tabular presentation in validation.

    Hi Maxence,

    CORINE wrote:

    How can I make the sum of the rows in a table in the apex oracle 4.2.6.

    I want to perform validations on adj_amount column in a table. If the adj_amount column for the selected row is<=0 then="" return="">

    How can I choose the name of the column of tabular presentation in validation.

    Always provide the necessary information on your question.

    Members of the Forum cannot determine information such as the type of tabular presentation, you use (i.e. Assistant generated/manual based on APEX_ITEM).

    Now, the information you have provided, I concluded you want proof on a certain column the sum of this column must be greater than zero.

    Well, you can use a "PLSQL function return Boolean" type, the level of the Page using APEX_APPLICATION validation. G_FXX berries.

    Determine a column which is not null loop through this column and the adj_amount of the amount column. Determine if the sum is greater than zero and therefore trigger the validation.

    Validation sample code would be:

    DECLARE
    
      L_ADJ_AMOUNT_SUM NUMBER := 0;
    
    BEGIN
    
      FOR I in 1 .. APEX_APPLICATION.G_F01.COUNT LOOP
        L_ADJ_AMOUNT_SUM := L_ADJ_AMOUNT_SUM + TO_NUMBER(NVL(APEX_APPLICATION.G_F02(I),0));
      END LOOP;
    
      IF L_ADJ_AMOUNT_SUM <= 0 THEN
        RETURN FALSE;
      ELSE
        RETURN TRUE;
      END IF;
    
    END;
    

    Here APEX_APPLICATION. G_F01 is a column not null and APEX_APPLICATION. G_F02's adj_amount column for example.

    NOTE: You can use a code inspection tools in the browser to identify to which column is mapped to APEX_APPLICATION. Table G_FXX.

    Just inspect element in the column to determine the name attribute. If name = "f04" then it is mapped to APEX_APPLICATION. G_F04.

    I hope this helps!

    Kind regards

    Kiran

Maybe you are looking for

  • HP 5940 - color cartridge status light flashes with new HP 97 cartridge

    Just installed a new HP 97 cartridge and the light flashing color cartridge status. Put in old 95 HP cartridge and the lights are out, but this cartridge is empty. I have cleaned all connectors (inside the printer and the cartridge), tried reinstalli

  • Trying to connect to a homegroup, but I need to activate IPv6.

    Trying to connect to a homegroup, but I need to activate IPv6.

  • Microsoft / Internet problem

    OK, my mother was online on his computer and went to watch music on MSN video earlier today and a window pop up something called 'Point of thinking' and he had the logo of Microsoft. It leaves him online and he won't let him get on some applications

  • Sansa view 8 GB and firmware update

    HI I am a newbie so sorry if this has been posted before, I used my sansa view 8 gb mp3 and added lots of music, he loved, in any case now I decided to download the firmware update when I made it to my pc with windows 7, my mp3 player has been assign

  • problem opening excel and word

    Whenever I try to open word or excel, ms office home and Student 2007 should reinstall itself first.