Given various date ranges, count the occurrences of dates in PL/SQL

i have a table of employees that have a start date and end date on a project. Some employees end date is null because they are still working on the project. So I want to find for each day in JAN, how many people on working on the project.
So I generate the dates JAN 1 - 31 with this:
Declare
  days DATE := To_Date('01-JAN-11','dd-mon-yy');
BEGIN
  FOR days IN 1..31
  LOOP
    Dbms_Output.Put_Line(TO_CHAR(days ,'dd-Mon-yy'));
    days  := days  + 1;
  END LOOP;
END; For employee X (ID 123), his start date is 1st and end date is 4th. So I use this query to list the dates he worked on (Jan1 jan2 jan3 jan4):
DECLARE
da Number;
days Date;     
sta emp.start%Type;
BEGIN
Select end-start+1
Into da
From emp
where emp_id = 123;
Select start
Into sta
From emp
Where emp_id = 123;
  days  := To_Date(sta,'dd-mon-yy');
FOR i IN 1..da
  LOOP
    Dbms_Output.Put_Line(TO_CHAR(days ,'dd-Mon-yy'));
    days  := days  + 1;
  END LOOP;
END;Now the problem I am having is trying to do a for loop to check the work days from each employee in the table. I guess I need to iterate over the employee IDs. The above code, I entered the empoyee ID myself to find his work days, but I am trying to loop over all the employees to find and count the days. Any help on this part?
Ultimately I am trying to get this:
Day Count
Jan 1 5
Jan 2 3
Jan 3 1
Jan 31 5

select  project_id,
        dt,
        nvl(count(emp_id),0)
  from  employee_projects,
        (select date '2010-12-31' + level dt from dual connect by date '2010-12-31' + level <= date '2011-01-31')
  where dt between start_date(+) and nvl(end_date(+),sysdate)
  group by project_id,
           dt
/SY.
Edited by: Solomon Yakobson on Jan 18, 2011 5:01 PM

Similar Messages

  • Report CO-PA: Count the occurrences of a characteristic

    In a report you want to count the occurrences of a characteristic relative to one or more other
    characteristics.
    For instance, we have a report whit two characteristics “customer” and “billing documents”, We have two columns, in the first one appears the "amount of sales", and in the second one "number of billing documents for each customer", I mean, we need a key figure that counts the number of “billing documents” per any other characteristic, such us customer.
    Has anyone any idea?
    Thanks in advance.
    Mariana Serrano

    Don't know why it does not work ... but I can say for sure that it worked fine on sys I've seen (both 2.x and 3.x): so check out SP for example.
    By the way I would like you to consider that working with exception aggregation at BEx level can be a performance pitfall, and even Virtual KeyF ...
    So be VERY carefull in evaluating the solution! With large InfoCubes you could have some surprise!
    Hope it helps
    GFV

  • How to count the occurrence of an unbounded segment of an idoc

    idoc ORDERS.ORDERS05 has segment  E1EDP01 which  has an occurrence(0-9999), how can we count the number of times E1EDP01 segment is coming in a particular ORDERS.ORDERS05 idoc

    Hello,
    You can use the pre-defined statistic function called count in your requirement
    E1EDP01 -> removeContext -> count -> targetField
    Hope this helps,
    Mark

  • Any method of ole2 to count the no. of rows in excel worksheet ??

    Dear all,
    want to generate a for 1..n loop structure to access the excel worksheet.
    iam using ole2 to upload data from excel to oracle.
    rest is fine..
    till now iam arbitrarily inputting a value as a count from forms to run the loop.
    want to know is there any method of ole2 to count the no. of rows in excel worksheet...???
    regards,

    If you have purchase order number in your cube then you can use the easiest method of all of counting -- a calculated key figure with exception aggregation.
    Create a CKF and add any basic key figure to it from your cube (basic means a key figure from the cube, not another CKF or RKF).  If you're using the 3.x query designer then click the Enhance button and set the exception aggregation to Counting All Values.  If you're using the 7.0 query designer then click on the Aggregation tab and switch the Exception Aggregation to Counter for All Detailed Values.  With either query designer set the reference characteristic to your PO number characteristic.  This CKF will count the number of PO documents.
    See this document for step-by-step instructions:  [How to... count the occurrences of a characteristic relative to one or more other characteristics|https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/7e58e690-0201-0010-fd85-a2f29a41c7af].

  • Count the occurences of a specific combination of chars

    Hi there
    I want to count the occurences of a specific combination of characteristic in a query.
    Example count the occurences of combination CHAR1 CHAR2 CHAR3
    CHAR1 CHAR2 CHAR3 Counter
    A     BB     CCC    1
    A     BB     CCC    1
    A     BB     DDD    1
    A     SS     DDD    1
    Query Result should be
    CHAR3 Count
    CCC    1
    DDD    2
    In the HowTo Paper: Count the occurrences of a
    characteristic relative to one or
    more other characteristics it is only a solution with one characteristic but not with a combination of chars.
    I don't know if this possible, because I think it is necessary to do a expeption aggregation based on several chars.
    Anyone an Idea?
    Thanks a lot in advance
    Joe

    Joe,
    I think in your case it is the case of multiple characteristics and I think the straight way is to do with exception aggregation.
    See the below link
    http://help.sap.com/saphelp_nw04/helpdata/en/d2/e0173f5ff48443e10000000a114084/frameset.htm
    See the below link for some more discussions using formulas which may help you to arrive a decision.
    Count elements in a characteristic
    Regs
    Gopi.

  • How to count the days between Date Range using OO ABAP?

    hi experts,
            i want to count the days between Date Range using OO ABAP for that  which class and method  can i use?.
    Thanks,
    Mahesh.

    Not sure I understand the requirement for it to be OO, but you could always write your own (i.e. use this):
    REPORT zz_date_diff.
    CLASS date_diff DEFINITION.
      PUBLIC SECTION.
        METHODS diff IMPORTING     i_date_fm TYPE d
                                   i_date_to TYPE d
                     EXPORTING     e_days    TYPE i.
    ENDCLASS."
    CLASS date_diff IMPLEMENTATION.
      METHOD diff.
        e_days = i_date_to - i_date_fm.
      ENDMETHOD."
    ENDCLASS."
    DATA: g_ref TYPE REF TO date_diff,
          g_days  TYPE i,
          g_date_fm  TYPE d VALUE '20080101',
          g_date_to  TYPE d VALUE '20090101'.
    START-OF-SELECTION.
      CREATE OBJECT g_ref.
      CALL METHOD g_ref->diff
        EXPORTING
          i_date_fm = g_date_fm
          i_date_to = g_date_to
        IMPORTING
          e_days    = g_days.
      WRITE g_days.

  • How do I count the number of occurrences of a string within a group of cells?

    Hello all, I'm trying to figure out how to count the number of times a child has completed certain tasks.  Here is a sample of the data (it is highly simplified here, but contains what I hope is needed to answer my question):
    Unnamed Table
    Objective
    John
    Ann
    Alex
    Dave
    Eric
    20a 20b 20c
    x
    20a 20b 20c
    x
    20a 20b 20c
    x
    x
    19b 20a
          x
           x
    20c 21b 22
         x
    What I am trying to do is count the number of times each child completed each objective - but I can't figure out how to go about splicing up the "objectives" fields for counting while still being able to compare them to whether or not the "child" has 'an x in the box' in that particular row.

    If I read your example correctly, John's counts should be 2 for 20a, 1 for 19b, 20b and 20c, and 0 for any other objectives listed.
    Here's an example that will work for your data set, assuming that any objective ma occur only once in each row of column A of the Main table. I've added an a to objective 22, but I think it is unnecessary, provided there is no objective 122, 221, etc.
    This uses a two step process.
    The table AUX extracts the objectives completed by each student, using the checkboxes in Main. Note that it also adds a space at the beginning and end of each string. This provides an extra character before the first objective code and after the last objective code in that row, used by the wildcard specification in the formula in the Summary table.
    Formula:
    Aux::B2: =IF(OFFSET(Data :: $A$1,ROW()-1,COLUMN()-1)," "&OFFSET(Data :: $A$1,ROW()-1,0)&" ","")
    Fill right to the last column and down to the last row of Aux.
    Summary uses COUNTIF to count the number of occurrences of each of the objectives listed in column A of that table.
    Formula:
    Summary::B2: =COUNTIF(Aux :: B,"=*"&$A2&"*")
    Fill right and down as the previous formula.
    Regards,
    Barry

  • Who worked with ICS' Model 4896 GPIB? I can not count the data from the module. Can prompt as it to make. It is desirable with examples (data read-out from the module and data transmission between channels. It is in advance grateful.

    I can not count the data from the module. Can prompt as it to make. It is desirable with examples (data read-out from the module and data transmission between channels. It is in advance grateful.

    Hello. Most of the engineers in developer exchange are more familiar
    with NI products. Contacting ICS for technical support is a better
    course of action.

  • How can I show a 0% range in the data value label on a bar chart thanks?

    How can I show a 0% range in the data value label on a bar chart thanks?

    I'm not sure what the question is. 
    I know that if you have a bar chart and one of the categories (X-axis) has bar (Y value) equal to 0%, no bar is plotted for that category. Even the addition of a stroke (line) around the bars doesn't make one appear for 0%.  The only automatic way I know of to make it look like there is data in that category is to add the value labels to the bars. Inspector/Chart/Series, select one of the bars on the chart, click on "value labels". Another method that is a workaround is to fudge the number a little in your table so that instead of 0% it is a very small %.  This will get you a thin line on the chart.
    But if your question is about the value labels (the numbers that display on or in the bars) and you are not getting one for a bar that is supposed to be 0%, it probably means your table doesn't actually have a 0% in the corresponding cell. A blank cell in the table will not get a value label.

  • Count the Number of Rows in Data form

    Hi,
    I want to count the number of rows in a data form.In my form there is a large number of rows.Is there any way to count it.
    Thanks.

    If you are on 11.1.2.x then you could look at adding a formula and using the CountA function - http://download.oracle.com/docs/cd/E17236_01/epm.1112/hp_admin/apcs03s06.html
    Otherwise it would be possible using custom javascript which you would have to had some experience in using.
    Cheers
    John
    http://john-goodwin.blogspot.com/

  • Need to count the number of times the Basic Finish data chages

    HI Expertes,
    I have a requirement I need to count number of times the Basic finish date chaged for PM work order. I went throug our forums I got some info like using a standard function module
    CHANGEDOCUMENT_READ_HDRS_ONLY
    CHANGEDOCUMENT_READ_HEADERS
    CHANGEDOCUMENT_READ_POSITIONS
    But all the above function module will not be suitable for my requirement since  CHANGEDOCUMENT_READ_HDRS_ONLY it gives whole changes but my requirement is just need number of changes occurred in Basic Finish date but CHANGEDOCUMENT_READ_POSITIONS can give the filed number which has been changed but still I need change id.
    So kindly suggest me wether there is any other Standard FM to get number of changes occurred in Basic Finish date?
    Thanks,
    Rajesh

    Hi Debbie,
         To count the number of groups please try the folling steps:
    1) Create a formula @reset and place this formula in the page header
        whileprintingrecords;
        numbervar i:=0;
    2) Create another formula @evalgroup place this in the group header where you want to count the values.
        whileprintingrecords;
        numbervar i:= i+1;
    3) Create another formula @display and place this in report footer.
        whileprintingrecords;
        numbervar i;
    In order to display the count of details which are printing in the detail section place the eval formula in the detail section and the @display formula in the group footer.
    Hope this helps!!!
    Regards,
    Vinay

  • Applescript - how can I count the number of records in a Word data source?

    I have written a script in AppleScript to open Word and then perform a mail merge from a data source and everything is working fine.
    One thing I want to do but am struggling with is to return the number of records in the mail merge.
    I am trying things like:
    get last record of datasource of mydatasource
    and also haven't been able to find anything in the AppleScript 2004 Word Reference Guide.
    Can anyone help as I have been searching the web and trying things all day and I am sure it can't be difficult.
    Thank you.

    I don't think there is an automatic way to get a line count, but I don't have any problem just counting the lines on a page. Maybe some more information about what you are trying to do and why you can't do it would help someone to come up with a helpful suggestion.

  • Count the displayed date rows on a column.

    Hi,
    I am trying to have a table with the following information and try to give a total count (of the dates visible) at the bottom of the table for the date columns. (not counting the rows)
    (E.g.- Under the Sent column there are 5 dates displayed and there are 6 numbers associated. On the received column there are 3 dates displayed for 6 numbers)
    Could you please advice me how to accomplish this. Thank you in advance.
    Rob.
    Name SENum SentReceived
    John      1234     12/22/06     
         5678     12/13/06     
         6565     12/19/06     1/6/07
         5656 - -
    Jane     9866 12/18/06     1/5/07
    Jim     5657 12/18/06 12/14/06           
    Total:           5      3

    I ma not sue what you want but
    here is some idea at sql plus
    SQL> compute count of ename on deptno
    SQL> compute count of sal on deptno
    SQL> select deptno,ename,sal from emp order by deptno
      2  ;
        DEPTNO ENAME                                                     SAL
            10 CLARK                                                    2450
               KING                                                     5000
               MILLER                                                   1300
    count                                                       3          3
            20 SMITH                                                     880
               ADAMS                                                    1100
               FORD                                                     3000
               SCOTT                                                    3000
               JONES                                                    2975
    count                                                       5          5
            30 ALLEN                                                    1600
               BLAKE                                                    2850
               MARTIN                                                   1250
               JAMES                                                    1045
               TURNER                                                   1500
               WARD                                                     1250
    count                                                       6          6
               DEV                                                      5000
    count                                                       1          1

  • Count the number of occurrence in a string

    is there any api for counting the number of occurrence in a string or should i write a method myself?
    for example. I want to count "." in "1.2.3.4.". i want to count the dot in this string.
    I am just wondering.
    Thanks in advance.

    is there any api for counting the number of
    occurrence in a string or should i write a method
    myself?The latter.

  • Count the number of days in the selected range using Customer exit

    Hi Experts,
    we have requirment where user is asking to add a column to report, which will have 'count of days for which key figure is having values' for each of the months and the Header would be 'Day Count'
    Please let me know if its possible using Customer exit?

    Hi,
    In our report we have two characteristics site no. and product and we have 6 key figures of type quantity and Input for the report is Fiscal year/period
    So in the report Key figures are populated with values for respective site no. and product combination
    Now the user wants new column in report which will have header u2018Day countu2019 and it should Simply count the number of days in the selected range that have a volume different than 0 for key figure
    Please let me know if more details are required

Maybe you are looking for