How to make the sum of the time through SQL?

Dear friends

I have a sh_detail table and there are three fields hours1, hours2 hours3. all fields are of type varchar2.

data are like

hours1 hours2 total hours3

02:45 00:18 01:25

00:38 01:45 00:00

02:15 02:00 00:15

1. I want to add on three fields in the total column

2 and want to make the sum of all areas as select sum (hours1) of sh_detail.

Please help I will be grateful.

Kind regards.

As others have said, use the NUMBER to store times, or the type of data correct INTERVAL, even if the latter cannot be aggregated (using the SUM).

In the meantime, this will give you the output desired in a few minutes (it is trivial to convert it back in format hh: mm if you wish):

SQL> with sample_data (id, hours1, hours2, hours3) as (
  2    select 1, '02:45', '00:18', '01:25' from dual union all
  3    select 2, '00:38', '01:45', '00:00' from dual union all
  4    select 3, '02:15', '02:00', '00:15' from dual
  5  )
  6  select id
  7       , sum(h1) h1
  8       , sum(h2) h2
  9       , sum(h3) h3
 10       , sum(h1+h2+h3) as total
 11  from (
 12  select id
 13       , to_number(substr(hours1, 1, 2))*60 + to_number(substr(hours1, 4, 2)) as h1
 14       , to_number(substr(hours2, 1, 2))*60 + to_number(substr(hours2, 4, 2)) as h2
 15       , to_number(substr(hours3, 1, 2))*60 + to_number(substr(hours3, 4, 2)) as h3
 16  from sample_data
 17  )
 18  group by rollup(id) ;

        ID         H1         H2         H3      TOTAL
---------- ---------- ---------- ---------- ----------
         1        165         18         85        268
         2         38        105          0        143
         3        135        120         15        270
                  338        243        100        681

Tags: Database

Similar Questions

  • How to make the output of Sql Plus with an error Code

    Dear members,

    How can I define sql more exit with an error code.

    We have a simultaneous host program. The executable is a shell script for this program. This sql script Shell script request which in turn gives call to a custom database package that I built.

    The flow rate is:


    -> SQL-> CUSTOM PACKAGE SCRIPT SHELL SCRIPT

    Custom package has two PARAMETERS of the error indicator and the error message. If something fails in the package these two PARAMETERS is returned with a value.

    So my sql script code looks like this:

    WHENEVER SQLERROR EXIT 251
    WHEN the OSERROR EXIT 251
    Set serveroutput size 1000000
    set linesize 32767
    set verify off
    set the position
    Set feedback off

    DECLARE
    x_error_message VARCHAR2 (1000);
    x_sql_message VARCHAR2 (1000);
    x_exception EXCEPTION;
    e_value NUMBER;

    BEGIN
    dbms_output.put_line ('Inside sql file');


    XXpackage.xxproc (x_error_message, x_sql_message);


    IF (x_error_message IS NOT NULL) AND (x_sql_message IS NOT NULL) THEN
    RAISE x_exception;
    END IF;


    EXCEPTION
    WHEN X_EXCEPTION THEN
    e_value: = 1;
    DBMS_OUTPUT. Put_line (' Exit Code: ' | e_value);
    DBMS_OUTPUT. Put_line ('X_EXCEPTION: EXCEPTION IN SCRIPT xpoext04a.sql :'||) SQLERRM);
    DBMS_OUTPUT. Put_line ('x_error_message is: ' | x_error_message);
    DBMS_OUTPUT. Put_line ('x_sql_message is: ' | x_sql_message);
    DBMS_OUTPUT. PUT_LINE (' ERROR IS: ' |) SQLERRM);
    WHILE OTHERS THEN
    e_value: = 252;
    DBMS_OUTPUT. Put_line (' Exit Code: ' | e_value);
    DBMS_OUTPUT. Put_line (' OTHER: EXCEPTION IN SCRIPT xpoext04a.sql :'||) SQLERRM);
    DBMS_OUTPUT. Put_line ('x_error_message is: ' | x_error_message);
    DBMS_OUTPUT. Put_line ('x_sql_message is: ' | x_sql_message);
    DBMS_OUTPUT. PUT_LINE (' ERROR IS: ' |) SQLERRM);


    END;
    */*
    output
    */*


    As you can see from above, if the two PARAMETERS have value, then it goes to an exception.

    My problem is that if it goes with the exception of X_EXCEPTION, the sql is not come away with a mistake. I mean sql is not treat this case like a sqlerror.


    How can do us more sql dating a sqlerror so that the host program must fail.


    Thank you
    Sandeep

    Published by: user340 on July 28, 2009 12:51

    Published by: user340 on July 28, 2009 12:57

    the plu sql is not come away with a mistake. I mean sql is not treat this case like a sqlerror.

    This is because there is no error. You handled the exception.

    You can try adding a STIMULUS; at the end of your exception section. This re-triggers the exception so sql liked to see it.

  • How to make the subscription through Skype credit

    Please suggest me asap

    I have a balance sufficient subscription for the India but then hav no opt. To use Skype credit

  • Database demo of vision: how to access the database through SQL * more?

    I'm totally new in this space, so forgive my beginner questions.

    What I want to do, is to integrate EBS with APEX. To get there I downloaded (of delivery) and installed the Vision database unique node in a VirtualBox. To do this, I followed this video on YouTube: https://www.youtube.com/watch?v=8JgxMDPsd6s

    Now I want to install APEX and for this I need to access my database as SYS. Now for my questions:

    1. How can I use SQL * more in the VirtualBox?

    2. What is the default password for SYS with this demo database?

    Thanks, Alex

    File under $ORACLE_HOME and the owner is the user 'oracle '.

    You will need to source the file VISION_ebs.env , and then deliver "env | fate"and you should see the set env variables. You can connect as sysdba.

    Thank you

    Hussein

  • How to set the time between slides in a slideshow (iPhoto, el capitan)

    How to set the time to say - 5 seconds or 20 seconds - between slide show photos in iPhoto. I use el capitan.

    See the Settings button at the bottom right of the toolbar (at the bottom of the slide show). Click on it and you have a small window with two tabes, refers to all the slides, the other to the selected slide. You can make your choice

  • When I run a web browser immediately loaded two blank tabs. How to make the browser to load the only?

    When I run a web browser immediately loaded two blank tabs. How to make the browser to load the only?

    Have you checked the home page to make sure that if is not the vertical bar (|) symbols?

    See the following for a few suggestions:

  • How to display the time of growth of the autoextended oracle files?

    Hello

    How to display the time of growth of the autoextended oracle files?

    Hello

    1. do you have re - seen view data dictionary in the oracle documentation?

    DBA_DATA_FILES

    dba_free_space

    dba_Tablespaces

    -Regarding (updated)

    DBA_HIST_SEG_STAT

    DBA_HIST_SNAPSHOT

    DBA_TABLESPACE_USAGE_METRICS

    DBA_HIST_TBSPC_SPACE_USAGE

    2. If not, then re - discovers once and understand the use of columns and build the query.

    3. If Yes, then why you have not tried to build queries

    4. again, if you're lazy, search in google or forums. ton of translates queries

    5. What are the disadvantage of step 4 - you will not get to learn something (0% gain of knowledge).  If you really want to learn, then try to learn to build query, and try to understand the views of data dictionary

    6. When you spend above task at the end of the task you will get satistided with two things

    --> Understand how to use the Oracle docs for help and where to look in the future

    --> Your skills / knowledge gain - trust me it will make you another step to learn new things.

    7. If you don't know SQL, learn the basics takes hardly 2 to 3 hours and then you can build query.

    It may be useful

    -Pavan Kumar N

  • How to make the plugin that works in the background?

    Hi, I need to make the plugin working in parallel in the background thread. It should start when Photoshop starts and stops when closing Photoshop. I think that the plugin type should I choose is "Automation" but these plugins are executed only when the user calls the plugin in the menu. How to make the plugin that runs when photoshop works without the need to invoke it manually? I don't want this plugin to be visible in any menu. This plugin is designed to work with the extension of the CEP.

    I ended up making invisible CEP extension that performs background tasks in the timer event handler:
    CEP 5 Extension HTML Cookbook for CC 2014 · Adobe-CEP/CEP-resources Wiki · GitHub

  • How to make the transition to digital in 10g

    Hi all


    We have 10g database of primary and standby, I have never worked before sleep, customer wants me start switch, can you please let me know how to make the transition to digital in 10g.


    Thank you very much.

    It's the way I'm following passage in the
    ------------------------------------------------------------

    (1) check that there is no active users connected to databases.
    (2) select message from v$ dataguard_status;
    (3) select protection_mode from database v$.

    Step 1 check if it is possible to perform a failover.

    SQL > select database_role, db_unique_name, SWITCHOVER_STATUS, v database name $;

    DATABASE_ROLE DB_UNIQUE_NAME SWITCHOVER_STATUS NAME
    ---------------- ------------------------------ -------------------- ---------
    PRIMARY OFMSREGT_SIM UNAUTHORIZED OFMSREGT

    SQL >

    If you get NO allow status for SWITCHOVER_STATUS, verify that the standby database is connectivity.

    If the SWITCHOVER_STATUS column displays ACTIVE SESSIONS, you can successfully achieve a failover by adding the WITH clause LOGOFF to the statement ALTER DATABASE COMMETTRE through PHYSICAL standby

    Step 2 open the passage on the primary database.

    SQL > SHUTDOWN IMMEDIATE;
    SQL > STARTUP MOUNT;
    SQL > ALTER DATABASE COMMIT TO SWITCH STANDBY MODE PHYSICAL;

    Step 4, check the transition state in the view V$ DATABASE.

    SQL > SELECT SWITCHOVER_STATUS FROM V$ DATABASE;
    SWITCHOVER_STATUS
    -----------------
    TO_PRIMARY
    1 selected line

    Step 5 place yourself in the role of physical standby database target the main role.

    You can switch a standby database physical role ensures the primary role
    When the standby database instance is mounted mode redo apply or open
    for read-only access

    SQL > ALTER DATABASE COMMIT AT THE GRADE CROSSING;

    Step 6 complete the transition of the database ensures the main role.

    If the physical standby database has not been opened in read-only mode since the
    the last time it was started, run the SQL ALTER DATABASE OPEN to open
    the new primary database:

    SQL > ALTER DATABASE OPEN;

    If the physical database was opened read only since the last
    time it was launched, you must close the database waiting for target and restart:

    SQL > SHUTDOWN IMMEDIATE;
    SQL > STARTUP;

    Note: There is no need to close and restart other bases before (not involved in the digital switchover) who are online at the time of the failover. These databases on hold will continue to
    function normally once the failover is completed.

    Step 7 if necessary, restart log apply services on standby databases.
    He must deliver the command FRO repeat apply as:
    SQL > change database recovery managed standby database disconnect from the session.
    or
    And stop repeating applies to give
    SQL > alter database recover managed standby database cancel;

    Step 3 Shut down and restart the former primary instance.

    SQL > SELECT OPEN_MODE FROM V$ DATABASE;

    Step 9 check the synchronization between the primary and standby:

    Primary
    SQL > select thread #, max(sequence#) in v$ archived_log where archived = 'YES' group by thread #;

    Sleep mode
    SQL > select thread #, max(sequence#) in v$ archived_log in case of application = 'YES' group by thread #;

    Stop the former primary instance and restart and mount the database:

    Sense of SWITCHOVER_STATUS
    --------------------------------------------
    The column SWITCHOVER_STATUS of v$ database can have the following values:

    NOT ALLOWED - this is a database of pending and the primary database has
    not been first switched, or it's a back-end database and there is no data pending.

    ACTIVE SESSIONS - indicates that there are active sessions of SQL attached to
    the primary database or emergency that must be disconnected before the
    failover operation is allowed.

    Transition to THE PENDING - this is a database of relief and the primary database
    application of the digital switchover has been received but untreated.

    LATENT pass - the digital switchover has been waiting mode, but has not completed
    and I went back to the primary database.

    Primary SCHOOL - this is a database of pending, any sessions active, i.e.
    allows to switch to a primary database.

    In STANDBY mode - this is a primary database, any sessions active, i.e.
    allows to switch to a standby database.

    RECOVERY NEEDED - this is a database of pending that has not received the
    application of the digital switchover.

  • How to calculate the time to SELECT

    I want to know how to calculate the time taken by the SELECT query.

    For example: I run a select statement in TOAD (oracle version 9i) who takes 40 seconds to run, but in the output window, it displays few records. It says "list 1 of 500 records, more records exist". This table has total 20 million records.

    Now my question is: have you my SELECT query took 40 seconds? or it took 40 seconds to SELECT 500 records.

    If it took 40 seconds to SELECT 500 records then how to calculate the total amount?

    Any help would be much appreciated.

    To avoid the exit which is probably not really need you can make

    begin
      for c in (select * from xy /*your query */)
      loop
        null;
      end loop;
    end;
    /
    

    This gives an estimate of the time to recover all the lines.

  • Can you please tell me how to make the thicker arrows on the Pages.

    Can you please tell me how to make the thicker arrows on the Pages. Thank you very much. There is something incredibly simple to do, but I, having a disconcerting brain, am totally unable to understand. Please please help me. Thank you

    < re-titled by host >

    Try here > Pages for Mac: Add and edit forms

  • How to make the top sites page appears when I open a new tab? Why have I not of "buttons" to pin a top site of the page tab?

    When I open a new tab, NO best sites don't show up... shows just a search engine box. I have Firefox 33.0. Why is there no 'pin' button when I opened a new page? How to make the top sites page appears when I open a new tab?

    Here is some additional information on the configuration of the new tab page:

    (1) in a new tab, type or paste Subject: config in the address bar and press ENTER. Click on the button promising to be careful.

    (2) in the search above the list box, type or paste newtab and make a pause so that the list is filtered

    (3) double-click the preference browser.newtab.url and enter your favorite page:

    • (Default) page thumbnails = > subject: newtab
    • Blank tab = > subject: empty
    • Built-in Firefox homepage = > topic: welcome
    • Any other page = > full URL of the page

    Press Ctrl + t to open a new tab and check that it worked. Fixed?

    Some traps:

    If Firefox will not let you change this setting: you can have what is called SearchProtect on your system.

    Firefox if allows you to save your changes, but he doesn't know: one of your extensions may be the substitution of her. You can consult, disable and/or remove extensions on the addons page:

    "3-bar" menu button (or tools) > Add-ons > in the left column click on Extensions

    If the modification works during your session, but during the next startup is leads to: you could have a user.js file in your personal settings Firefox (your Firefox profile folder). This article describes how to track down and delete the file: How to fix preferences that will not save.

    A little luck?

  • I have associated my pencil to Apple for the iPad Pro; How to make the Widget of batteries displayed on the notification Center 'today '?

    I have associated my pencil to Apple for the iPad Pro; But how to make the Widget of batteries displayed on the notification Center 'today '?

    See if it works. Scroll to the bottom of the list in the display today. Tap on edit. Press on the + sign next to battery.

  • How to set the time that my screen stays on?

    How to set the time that my screen lights up.  I have an iPod Touch 6?

    Settings > general > auto-lock

  • How to make the snow fall go far in the background that I've recently added a newsletter on your part?

    How to make the snow fall go far in the background that I've recently added a newsletter of mozilla to? It appears on things, I feel.

    I can't talk about it. What is an extension?

    You can disable or remove unwanted page extensions modules. Either:

    • CTRL + SHIFT + a
    • Firefox orange (or the Tools menu) button > Add ons

    In the left column, click Extensions. If you don't it spot, perhaps in the appearance section?

Maybe you are looking for

  • How to thank two very Apple tech support of skilled workers in Ontario, Canada

    A big thank you to Dave, team members Support Apple and Sarah in Ontario, Canada for their patience and expertise to solve WiFi problems the first time I called. Dave has guided me to update the firmware on the router Apple GUI which restores Wi - Fi

  • Help update in boot camp video card driver

    Hi all. I read and read about it, but still can't get it fixed. I have a 2011 mbp 15 eairly.  and with training camp. My games stop that my driver is obsolete in windows.  so I tried everything that I can to get updated in windows amd card.   He alwa

  • How to make more space in my iCloud storage?

    I'm no longer backup of photos and videos to iCloud; I need not more than 5 GB of storage.  But I need to free up space in my storage by deleting older backups and all these photos.  How can I do this?  Remove photos from my phone did not at all affe

  • do not have recovery option

    Hello I recently had problems with my computer tx2-1250ea laptop and hp sent me a new hard drive, I gave my laptop to a friend and they put the new hard drive, but now I have several problems and there is more no Manager to get back on my laptop. I t

  • help me get back my acount

    I really need back my acount of please help me, I got the last password mail, secret question and answer