Query to find out sum by using decode or case

Hi, I have data like below and required an output as given below, could you please help me to get the same by using decode / case .....Thanks for your help
INSNAME INSID STATUS
AAA 1000 Scheduled
AAA 1000 Processing
BBB 1001 Inspector
BBB 1001 Scheduled
BBB 1001 Processing
BBB 1001 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
Required Output...
INSNAME INSID sum_of_scheduled sum_of_Processing sum_of_Inspector
AAA 1000 1 1 0
BBB 1001 1 1 2
CCC 1002 2 2 2

And if you want it with CASE statement:
WITH test_table AS
   SELECT 'AAA' insname, 1000 insid, 'Scheduled'   status FROM DUAL UNION ALL
   SELECT 'AAA' insname, 1000 insid, 'Processing'  status FROM DUAL UNION ALL
   SELECT 'BBB' insname, 1001 insid, 'Inspector'   status FROM DUAL UNION ALL
   SELECT 'BBB' insname, 1001 insid, 'Scheduled'   status FROM DUAL UNION ALL
   SELECT 'BBB' insname, 1001 insid, 'Processing'  status FROM DUAL UNION ALL
   SELECT 'BBB' insname, 1001 insid, 'Inspector'   status FROM DUAL UNION ALL
   SELECT 'CCC' insname, 1002 insid, 'Scheduled'   status FROM DUAL UNION ALL
   SELECT 'CCC' insname, 1002 insid, 'Processing'  status FROM DUAL UNION ALL
   SELECT 'CCC' insname, 1002 insid, 'Inspector'   status FROM DUAL UNION ALL
   SELECT 'CCC' insname, 1002 insid, 'Scheduled'   status FROM DUAL UNION ALL
   SELECT 'CCC' insname, 1002 insid, 'Processing'  status FROM DUAL UNION ALL
   SELECT 'CCC' insname, 1002 insid, 'Inspector'   status FROM DUAL
SELECT insname
      ,insid
      ,SUM(CASE WHEN status = 'Scheduled'
                THEN 1
                ELSE 0
            END
          ) sum_of_scheduled
      ,SUM(CASE WHEN status = 'Processing'
                THEN 1
                ELSE 0
            END
          ) sum_of_processing
      ,SUM(CASE WHEN status = 'Inspector'
                THEN 1
                ELSE 0
            END
          ) sum_of_inspector         
  FROM test_table
GROUP BY insname
        ,insid;
Regards
Arun

Similar Messages

  • Query to find out the time used by an user for an application

    Hello All,
    I want to know the query to find out the whole time used by the user for an application. Please view the below data
    Employee:
    SNO EMP_ID EMP_NAME EMP_DATE LOGIN_TIME LOGOUT_TIME
    1 10 Visu 21-Nov-2010 06:30:00 07:30:00
    2 10 Visu 21-Nov-2010 06:40:00 07:20:00
    3 10 Visu 21-Nov-2010 06:50:00 07:50:00
    4 10 Visu 21-Nov-2010 07:30:00 08:30:00
    5 10 Visu 21-Nov-2010 09:30:00 10:30:00
    By checking the above data we can say that the total time Visu used the application is
    8.30 - 6.30 (From 1,2,3,4 records) = 2hrs
    10.30 - 9.30 (Based on 5th rec) = 1hr
    So the total time Visu used the application would be 3 hrs = 180 mins.
    Could you please help me in getting the result from that data using a query?

    odie_63 wrote:
    I think it may be solved with analytics too.
    with t1 as (
                select 1 sno,10 emp_id,'Visu' emp_name,'21-Nov-2010' emp_date,'06:30:00' login_time,'07:30:00' logout_time from dual union all
                select 2,10,'Visu','21-Nov-2010','06:40:00','07:20:00' from dual union all
                select 3,10,'Visu','21-Nov-2010','06:50:00','07:50:00' from dual union all
                select 4,10,'Visu','21-Nov-2010','07:30:00','08:30:00' from dual union all
                select 5,10,'Visu','21-Nov-2010','09:30:00','10:30:00' from dual
         t2 as (
                select  emp_id,
                        emp_name,
                        emp_date,
                        to_date(emp_date || login_time,'DD-MON-YYYYHH24:MI:SS') login_time,
                        to_date(emp_date || logout_time,'DD-MON-YYYYHH24:MI:SS') logout_time
                  from  t1
         t3 as (
                select  t2.*,
                        case
                          when login_time < max(logout_time) over(
                                                                  partition by emp_id,emp_date
                                                                  order by login_time
                                                                  rows between unbounded preceding
                                                                           and 1 preceding
                            then 0
                          else 1
                        end start_of_group
                  from  t2
         t4 as (
                select  t3.*,
                        sum(start_of_group) over(partition by emp_id,emp_date order by login_time) grp
                  from  t3
         t5 as (
                select  emp_id,
                        emp_date,
                        min(login_time) login_time,
                        max(logout_time) logout_time
                  from  t4
                  group by emp_id,
                           emp_date,
                           grp
    select  emp_id,
            numtodsinterval(sum(logout_time - login_time),'day') time_spent
      from  t5
      group by emp_id
      order by emp_id
        EMP_ID TIME_SPENT
            10 +000000000 03:00:00.000000000
    SQL> SY.

  • Query to find out tables using view

    Hi
    I have a views using three tables.Now i wanted a query to find out number of views using three tables
    I have written a query
    select * from dba_views where text like '%SELECT%;
    It is giving me error as ora error EXPECTED NUMBER GOT LONG.
    Appreciate your help?

    SELECT DECODE(referenced_type, 'NON-EXISTENT', '.....',
    referenced_type) || ' ' || referenced_owner ||
    '.' || referenced_name r_name, ' is referenced by: ' ||
    type || ' ' || owner || '.' || name name,
    ' Referenced Link: ' || DECODE(referenced_link_name,
    NULL, 'none', referenced_link_name) r_link
    FROM dba_dependencies
    -- WHERE owner NOT IN ('SYS', 'SYSTEM')
    -- and rownum < 50
    ORDER BY 1,2;
    add your own where clause to it

  • How to use ONE query to find out tree structure?

    ID------------upperID----------Name------------------------isFolder
    1------------ 0---------- Folder
    1------------------------------------1
    2------------ 1------------ Folder 1- Sub
    Folder--------------------1
    3------------ 2------------
    Folder1-Item1-A--------------------------0
    4------------ 1------------ Folder 1- Sub
    Item-----------------------0
    Hi all, if I have a table like above to demonstrate the
    folders and item relationship. This structure allows the user to
    create unlimited folders and items.
    Now I would like to use one query to find out the tree
    structure of this table, how could I do the query.
    Any help on this will be highly appreciated!
    Thanks,
    ez

    Also, see this thread:
    http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=1&catid=7&threadid=12 55788&enterthread=y

  • Query to find out which workflow(s) use a particular message ? wf_messages

    Hi guys
    Hoping someone can help.
    I've a list of particular notfication messages, which are stored in the WF_MESSAGES_TL and WF_MESSAGES table, and I need to find out which workflows use them.
    Can anyone tell me what table I can query to find this out ?
    There's no referential integrity which I can use to find this out, and google has turned up nothing so far.
    Any ideas ?
    Thanks a lot
    Scott

    You can use this and tweak it around:
    select p.process_item_type, p.process_name, p.activity_name, a.message
    from wf_process_activities p, wf_activities a
    where p.process_item_type=a.item_type and
    p.activity_name=a.name and
    a.end_date is null;-- means the active/current activity
    Regards,
    Alejandro

  • Need query to find out the sum till a period

    Hi,
    I need to write one query to find out the cumulative sum of raw_cost of table pa_budget_lines till the specified period, I tried to do this by analytical function, but that is not working in Report 10g. Can anybody help me in this.
    Thanks

    not familiar with report but could you do something like this
    with t as
      select 1 grp, 10 * level the_cost, add_months (sysdate,  level) effective_dt from dual connect by  level < 11 union all
      select 2 grp, 5 * level the_cost, add_months (sysdate,  level) effective_dt from dual connect by  level < 11
    input as
      ( select null grp, null the_cost, null running_total,  v_eff_dt effective_dt   from dual)
    select * from(
    select t.grp, t.the_cost, sum(t.the_cost) over (partition by t.grp order by t.effective_dt) running_total, t.effective_dt,
           lead(t.effective_dt) over (partition by t.grp order by t.effective_dt) next_effective_dt,
           input.effective_dt  inp_eff_dt
    from t, input
    where inp_eff_dt between effective_dt and next_effective_dtv_eff_dt would be the input date you are interested in

  • How to find out the updates using query which has done in Discoverer Admin

    Hi All,
    I have done some updates in Discoverer Admin like add field in some folders and rename...I want to know the updates which has been done in the front end using sql query.
    Could any one please tell me is there any query to find out the updates in Discoverer admin.
    Thanks,
    Kamal

    Hi Siddarth,
    Solution :
            1) Go to SE16 - Table Name : EKBE (PO History)
            2) Field - BUDAT (Posting Date)
            3) Transaction event Type:
                             - 1 (Only GR's )
                             - 2 (Both GR and IR)
            4) We can fetch both GR Numbers and also PO Numbers for which IR has not been done if transaction event - 1.
    In addition to MB5S, you can try this also...

  • Query to find out license products installed

    hai dba
    tell me the query to find out what r hte products installed r licensed??
    regards
    dba

    1) From OAM: Oracle Applications Manager > Site Map > License Manager > Reports > License Products/Shared Products
    2) Use $AD_TOP/sql/adutconf.sql
    3) Use the following query:
    SQL> select fa.APPLICATION_SHORT_NAME, fpi.PATCH_LEVEL, DECODE(fpi.STATUS, 'I','Installed',
    'S','Shared', 'N', 'Inactive', fpi.STATUS) Status , fpi.DB_STATUS
    from fnd_product_installations fpi,FND_APPLICATION fa
    where fpi.APPLICATION_ID in (
    select APPLICATION_ID from FND_APPLICATION)
    and fa.APPLICATION_ID=fpi.APPLICATION_ID;

  • Query to find out pertcular schema related datafiles free space????

    hai,
    whats the query to find out free space in datafiles and index files related free space for perticular schmea?
    Regards
    dba

    If you wish to get free space in index tablespace then use following query:
    select b.tablespace_name, tbs_size SizeMb, a.free_space FreeMb
    from
    (select tablespace_name, round(sum(bytes)/1024/1024 ,2) as free_space
    from dba_free_space group by tablespace_name) a,
    (select tablespace_name, sum(bytes)/1024/1024 as tbs_size
    from dba_data_files group by tablespace_name
    UNION
    select tablespace_name, sum(bytes)/1024/1024 tbs_size
    from dba_temp_files
    group by tablespace_name ) b
    where a.tablespace_name(+)=b.tablespace_name
    and a.tablespace_name='INDX';
    and if you get info for all tablespace then :
    select b.tablespace_name, tbs_size SizeMb, a.free_space FreeMb
    from
    (select tablespace_name, round(sum(bytes)/1024/1024 ,2) as free_space
    from dba_free_space group by tablespace_name) a,
    (select tablespace_name, sum(bytes)/1024/1024 as tbs_size
    from dba_data_files group by tablespace_name
    UNION
    select tablespace_name, sum(bytes)/1024/1024 tbs_size
    from dba_temp_files
    group by tablespace_name ) b
    where a.tablespace_name(+)=b.tablespace_name;

  • Query to find out the list of user who have delete access

    Hi,
    I need a query to find out the list of users who have delete access on perticular folder/universe/ reports  in infoview.
    Please advice.
    Regards,
    Neo.

    orton607 wrote:
    thanks for replying guys. But the thing is i am using dynamic sql execute immediate in my package, so i want those tables also and the schema name.
    thanks,
    ortonThis is not possible. The best you could do is to have a good guess.
    Or how would you parse some dynamic statement as this:
       v_suffix := 'loyees';
       v_sql := 'Select count(*) from (select ''nonsense'' col1 from emp'||v_suffix||') where col1 = ''Y'''';
       execute_immediate(v_sql);
    ...What is the table name? How do you want to parse that?
    Better rewrite all dynamic SQL statements into non dynamic ones. Or do the source control logic for those dynamic parts in an extra module. For example implement your own dependency table and force every developer to add there all dynamic parts.

  • Query to find out controls displayed as in UI for an applet,along with view

    Hi,
    I am looking for a query to find out list of all the controls as displayed in UI applet along with applet's view.
    Please note that in the query only mapped controls (those displayed in UI ) should come up.
    Regards,
    Kunal

    Hi,
    If the EUL is an apps mode (EBS) EUL then the eu_username column is the apps user id or apps resp id. If you want to show only the responsibilities and convert those ids to names then you need to use the EUL5_GET_APPS_USERRESP function like this:
    select ba_name, ba_developer_key, EUL5_GET_APPS_USERRESP(eu.eu_username, 'R') responsibility_name
    from eul5_bas ba
       , eul5_access_privs ap
       , eul5_eul_users eu
    where ba.ba_id = ap.gba_ba_id
    and ap.ap_type = 'GBA'
    and ap.ap_eu_id = eu.eu_id
    and eu.eu_role_flag=1
    order by 1,2,3Rod West

  • Query to find out which process consuming more cpu?

    Hi All,
    some one please tell me the query to find out which process is consuming more
    cpu resources?
    Thanks and Regards
    Srikanth

    Use V$SESSTAT to determine which sessions are using most CPU, and investigate what SQL these users/sessions are executing:
    SQL> select ss.sid, se.command, ss.value CPU, se.username, se.program
    from v$sesstat ss, v$session se
    where ss.statistic# in
    (select statistic#
    from v$statname
    where name = 'CPU used by this session')
    and se.sid=ss.sid
    order by ss.value desc;

  • Query to find out last quarter data

    Hi All,
    I need a SQL query to find out. I have an amount field like below
    Period | Amt | Amt2
    ====================
    Jul | 0 | 0+20+30
    Aug | 20 | 0
    Sep | 30 | 0
    Oct | 0 | 0+50
    Nov | 50 |
    My need is Amt2 which needs to calculating in Qtr wise. Can any one post me a query for this.
    Thanks,
    Kamal

    Hi Valli,
    Thanks, so we need to create a function and then call that from report. I have doubt in this, for example if the user enter 'SEP-12' then it will find which QTR from the case statement. But if the user have account number which is available in all the months from 2010 - 2012.
    So now user will give two inputs like period_name='SEP-12' and Account Number=101010 then it should give output like sum(Q3) in the year of 2012. Here how it will calculate year wise? or do we need to specify any condition here?
    Please advice,
    Thanks,
    Kamal

  • Writing a query to find out who is having a given responsibility name.

    Hi,
    I am new to Oracle Apps and have no idea about its tables. Can anyone please help me in writing a query to find out who is having a given responsibility name.
    Thanks in advance.
    Regards
    Himanshu

    Hi
    you can get required data by using this query
    select usr.user_name
    ,     res.responsibility_name
    from fnd_user usr
    ,     FND_USER_RESP_GROUPS_DIRECT usrrep
    ,     fnd_responsibility_tl res
    where usr.user_id=usrrep.user_id
    and usrrep.responsibility_id=res.RESPONSIBILITY_ID
    group by usr.user_name,res.responsibility_name
    Regards,
    Venky

  • Is there a way to find out which project using the record Z_student by sql

    Is there a way to find out which project using the record Z_student by sql
    use Find in in Application Designer is time consumer

    Try following SQL
    with all credits to the original poster of the greater portion of this sql
    http://srini-peoplesoft.blogspot.co.uk/2012/05/find-in-text-sql.html
    select projectname  from psprojectitem
    where (objectvalue1,objectvalue2,objectvalue3) in
    With Srch As
    Select Str, SubStr(Max(HexStr),3) Srch_Str
    From
    Select RNum, Str, Hexcode, Sys_connect_by_path(HexCode, '00') HexStr
    From (
    Select RNum, Str, Chr(Floor(Mod(Code/16, 16))
               + Decode(Floor(Floor(Mod(Code/16, 16))/10), 0, 48, 55)) ||
           Chr(Mod(Code, 16)
               + Decode(Floor(Mod(Code, 16)/10), 0, 48, 55)) HexCode
    From (        
    Select RNum, Str, Ascii(Substr(Str, RNum, 1)) Code
      From (Select Rownum RNum From Dual Connect By Level <= Length(:Str)),
           (Select :Str Str From Dual
            Union
            Select Upper(:Str) From Dual
            Union
            Select Lower(:Str) From Dual
            Union
            Select InitCap(:Str) From Dual
    Start With RNum = 1 Connect By Prior RNum = RNum - 1 And Prior Str = Str
    Group By Str
    select
    objectvalue1,objectvalue2,objectvalue3
    from pspcmprog p, srch
    where dbms_lob.instr(progtxt, srch_str)  > 0
    );

Maybe you are looking for

  • Apps don't launch or work after Copy Function

    Howdy, I'm having another issue with ARD 3. I've just rolled out StuffIt Expander 11 to all my Tiger Clients via ARD's Copy Function. The copy function works fine and it does copy the app to the destination, which is the Applications Folder. The app

  • EXS 24, importing AKAI samples...

    I want to import all of my AKAI CD-ROMs into EXS 24, and I want all of the sampler instruments and samples to live on my external FW drive. Having a heck of a time trying to understand the file management heirarchy, I started from scratch, and tried

  • Javascript and Actions

    This has been asked before. Just got off the phone with Adobe Tech support, which told me "We aren't trained in Scripting, you'll need to post on the forums". So here I am, posting on the forums hoping to get an official response from Adobe. Why can'

  • Save ALV Reporrt

    Hi i need to save ALV layout in  grid display. wat to do. Regards Rasheed

  • Joins between two schemas

    Hi I need data from two different schemas. Can develop one subject area taking 2 tables from 2 different schemas or we need to have these two tables from one schema only ? Can any one help on this. Thanks Manu