Returning latest records based on specific conditions

Hi Everyone, Thanks for everyone response for my queries. All responses are highly appreciated. I need one help in developing Sql query. I am
using Oracle 11.2.
I have table Test as below having columns as, ID;Name;Position;Level;Date_Added;Flag_Name;
Suppose I have below data in table:
ID       Name      Position      Level              Date_Added                Flag_Name
1       Jack       Manager        10         10/29/2013 11:00:00 PM          Match
2       Tom        Supervisor     20            10/31/2013 9:00:00 AM       NoMatch
1       Jack       Manager        15          11/1/2013  10:00:00 AM          Error
1       Jack       Manager        20         10/1/2013  9:00:00 AM           NoMatch
3       John       Salesman      5           10/4/2013  2:00:00 PM           Error
1       Jack       Manager        17         9/28/2013  2:00:00 AM           NoMatch
2       Tom        Supervisor     12         11/2/2013  10:00:00 AM          NoMatch
4       Remy       Accountant     12        11/4/2013  1:00:00 PM           Error
4       Remy       Accountant     19         11/3/2013  2:00:00 AM           Error
2       Tom        Supervisor     10            10/29/2013 7:00:00 PM           Error
I need to return latest data in query based on combination of (ID;Name;Position) and date range with below conditions:
1) Need to return latest records for Flag_Name other than Match.
2) If latest record for combination of (ID;Name;Position) will come with Flag_Name as Error in that specified date range, then we need to look for this combination has Flag_Name as NoMatch anywhere before in table.
3) If Flag_Name as NoMatch is found for this, then we need to return it. In this case also, if we found multiple records with Flag_Name as NoMatch for combination of (ID;Name;Position), then we need to return latest one.
4) Else we find no record for combination of (ID;Name;Position) with Flag_Name as NoMatch in table, then we can return the latest record with Flag_Name as Error.
While select ID;Name;Position should be unique combination, as we always return latest data for it.
As per above table, if I need to return data for date range between 29-Oct-2013 and 5-Nov-2013, expected result will be like below:
ID       Name      Position      Level         Date_Added                      Flag_Name    
1       Jack       Manager        20              10/1/2013  9:00:00 AM           NoMatch
2       Tom        Supervisor     12             11/2/2013  10:00:00 AM          NoMatch
3       John       Salesman        5              10/4/2013  2:00:00 PM           Error
4       Remy       Accountant     12           11/4/2013  1:00:00 PM           Error
Please let me know if anyone has any doubt about the logic. Is it possible if we can do it in select statement rather than pl/sql code?
All comments/suggestions will be highly appreciated.
Thanks in advance.
Regards
Dev    

First i didn't read topic exactly )))
here is second way. I get with stmt from Karthick (thanks)
with t
    as
    select 1 id, 'Jack' name, 'Manager'    position, 10 lvl, to_date('10/29/2013 11:00:00 PM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Match'   flag_name
      from dual union all
    select 2 id, 'Tom'  name, 'Supervisor' position, 20 lvl, to_date('10/31/2013 09:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'NoMatch' flag_name
      from dual union all
    select 1 id, 'Jack' name, 'Manager'    position, 15 lvl, to_date('11/1/2013  10:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Error'   flag_name
      from dual union all
  select 1 id, 'Jack' name, 'Manager'    position, 20 lvl, to_date('10/1/2013  09:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'NoMatch' flag_name
    from dual union all
  select 3 id, 'John' name, 'Salesman'   position,  5 lvl, to_date('10/4/2013  02:00:00 PM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Error'   flag_name
    from dual union all
  select 1 id, 'Jack' name, 'Manager'    position, 17 lvl, to_date('9/28/2013  02:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'NoMatch' flag_name
    from dual union all
  select 2 id, 'Tom'  name, 'Supervisor' position, 12 lvl, to_date('11/2/2013  10:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'NoMatch' flag_name
    from dual union all
  select 4 id, 'Remy' name, 'Accountant' position, 12 lvl, to_date('11/4/2013  01:00:00 PM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Error'   flag_name
    from dual union all
  select 4 id, 'Remy' name, 'Accountant' position, 19 lvl, to_date('11/3/2013  02:00:00 AM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Error'   flag_name
    from dual union all
  select 2 id, 'Tom'  name, 'Supervisor' position, 10 lvl, to_date('10/29/2013 07:00:00 PM', 'mm/dd/yyyy hh:mi:ss am') date_added, 'Error'   flag_name
    from dual
select ID,
       Name,
       Position,
       max(lvl) keep (dense_rank first order by с desc, Date_Added desc) lvl,
       max(Date_Added) keep (dense_rank first order by с desc, Date_Added desc) Date_Added,
       min(Flag_Name) keep (dense_rank first order by с desc, Date_Added desc) Flag_Name
from(
select  ID,
            Name,
            Position,
            lvl,
            Date_Added,
            Flag_Name,           
            decode(flag_name, 'Error', -1, 1) с
from T
where flag_name != 'Match'
group by ID,Name,Position
ID
NAME
POSITION
LVL
DATE_ADDED
FLAG_NAME
1
Jack
Manager
20
01.10.2013 09:00:00
NoMatch
2
Tom
Supervisor
12
02.11.2013 10:00:00
NoMatch
3
John
Salesman
5
04.10.2013 14:00:00
Error
4
Remy
Accountant
12
04.11.2013 13:00:00
Error
Ramin Hashimzade
Message was edited by: RaminHashimzadeh

Similar Messages

  • How to activate or deactivate a user-exit based a specific condition

    hi all,
    i want to activate or deactivate(make it trigger) a particular user-exit based in a condition.
    can i do that. if yes please tell me how.
    can we use COMMIT in user-exits or BADI's.
    Thanks & Regards,
    Saroja.

    Hello Saroja
    The solution provided by Rich should be used for testing purposes only in the the reverted sense:
    IF ( syst-uname ne '<specific user>' ).
      RETURN.
    ENDIF.
    " Execute user-exit for specific user
    However, for serious programming you should use a a better strategy. In principle, user-exits are either ON or OFF and, if they are ON, they are ON for <b>all </b>user which is usually not intended.
    The following example shows a (possible) strategy how to execute user-exits based on specific conditions.
    The SAP extension CATS0001 contains the component EXIT_SAPLCATS_001 with the following interface:
    FUNCTION EXIT_SAPLCATS_001.
    *"*"Lokale Schnittstelle:
    *"  IMPORTING
    *"     VALUE(SAP_TCATS) LIKE  TCATS STRUCTURE  TCATS
    *"     VALUE(SAP_PERNR) LIKE  CATSFIELDS-PERNR
    *"     VALUE(SAP_DATELEFT) LIKE  CATSFIELDS-DATELEFT
    *"     VALUE(SAP_DATERIGHT) LIKE  CATSFIELDS-DATERIGHT
    *"     VALUE(SAP_DATEFROM) LIKE  CATSFIELDS-DATEFROM OPTIONAL
    *"     VALUE(SAP_DATETO) LIKE  CATSFIELDS-DATETO OPTIONAL
    *"  TABLES
    *"      SAP_ICATSW STRUCTURE  CATSW
    *"      SAP_ICATSW_FIX STRUCTURE  CATSW OPTIONAL
      INCLUDE ZXCATU01.
    ENDFUNCTION.
    The include ZXCATU01 contains only the following coding:
      CALL FUNCTION 'Z_EXIT_SAPLCATS_001'
        EXPORTING
          sap_tcats            = sap_tcats
          sap_pernr            = sap_pernr
          sap_dateleft         = sap_dateleft
          sap_dateright        = sap_dateright
          SAP_DATEFROM         = SAP_DATEFROM
          SAP_DATETO           = SAP_DATETO
        tables
          sap_icatsw           = sap_icatsw
          SAP_ICATSW_FIX       = SAP_ICATSW_FIX.
    This function module is just a copy of the exit function module in the customer namespace.
    Let us assume that your condition at which the user-exit should be executed is that the employee (SAP_PERNR) belongs to a specific controlling area. Thus, we make another copy of the original exit function module and call this fm within the "general" customer-specific exit function module:
    FUNCTION z_exit_saplcats_001.
    *"*"Local Interface:
    *"  IMPORTING
    *"     VALUE(SAP_TCATS) LIKE  TCATS STRUCTURE  TCATS
    *"     VALUE(SAP_PERNR) LIKE  CATSFIELDS-PERNR
    *"     VALUE(SAP_DATELEFT) LIKE  CATSFIELDS-DATELEFT
    *"     VALUE(SAP_DATERIGHT) LIKE  CATSFIELDS-DATERIGHT
    *"     VALUE(SAP_DATEFROM) LIKE  CATSFIELDS-DATEFROM OPTIONAL
    *"     VALUE(SAP_DATETO) LIKE  CATSFIELDS-DATETO OPTIONAL
    *"  TABLES
    *"      SAP_ICATSW STRUCTURE  CATSW
    *"      SAP_ICATSW_FIX STRUCTURE  CATSW OPTIONAL
    " User-Exit specific for employees (SAP_PERNR)
    " belonging to controlling area 1000
      CALL FUNCTION 'Z_EXIT_SAPLCATS_001_1000'
        EXPORTING
          sap_tcats      = sap_tcats
          sap_pernr      = sap_pernr
          sap_dateleft   = sap_dateleft
          sap_dateright  = sap_dateright
          sap_datefrom   = sap_datefrom
          sap_dateto     = sap_dateto
        TABLES
          sap_icatsw     = sap_icatsw
          sap_icatsw_fix = sap_icatsw_fix.
    " User-Exit specific for employees (SAP_PERNR)
    " belonging to controlling area 2000
      CALL FUNCTION 'Z_EXIT_SAPLCATS_001_2000'
        EXPORTING
          sap_tcats      = sap_tcats
          sap_pernr      = sap_pernr
          sap_dateleft   = sap_dateleft
          sap_dateright  = sap_dateright
          sap_datefrom   = sap_datefrom
          sap_dateto     = sap_dateto
        TABLES
          sap_icatsw     = sap_icatsw
          sap_icatsw_fix = sap_icatsw_fix.
    ENDFUNCTION.
    Finally, within the specific exit function module we define the condition when the exit should be executed:
    FUNCTION z_exit_saplcats_001_1000.
    *"*"Local Interface:
    *"  IMPORTING
    *"     VALUE(SAP_TCATS) LIKE  TCATS STRUCTURE  TCATS
    *"     VALUE(SAP_PERNR) LIKE  CATSFIELDS-PERNR
    *"     VALUE(SAP_DATELEFT) LIKE  CATSFIELDS-DATELEFT
    *"     VALUE(SAP_DATERIGHT) LIKE  CATSFIELDS-DATERIGHT
    *"     VALUE(SAP_DATEFROM) LIKE  CATSFIELDS-DATEFROM OPTIONAL
    *"     VALUE(SAP_DATETO) LIKE  CATSFIELDS-DATETO OPTIONAL
    *"  TABLES
    *"      SAP_ICATSW STRUCTURE  CATSW
    *"      SAP_ICATSW_FIX STRUCTURE  CATSW OPTIONAL
      IF ( <user BELONGS to CONTROLLING area 1000> ).
        "   execute user-exit
      ELSE.
        RETURN.
      ENDIF.
    ENDFUNCTION.
    The alternative would be to place the entire coding including the conditions in the include ZXCATU01. However, in this case you can test the user exit <b>only in the context of the transaction</b> in which the user-exit is passed.
    Using the strategy I have devised you are able to test the user-exit in general and the specific user-exits <b>independent </b>of the transaction. For example, if you are already working on 6.40 or higher then you could use ABAP Unit Testing for this purpose.
    The same logic can be applied for BAdI where we can have only a single active implementation.
    Finally, I hope to convince that it makes sense to spend some time into a reasonable strategy for implementing user-exits.
    Regards
      Uwe

  • Displaying record based on the condition in Crystal report

    Hi All,
    I want to display certain records in crystal based on some condition.
    For example,
    If I am having 5 student records like,
    Name  Mark1 Mark2 Mark3 Total
    aaa      40       50       60      150
    abc      30       60       40       130
    tcr        25      25        25       75
    Now in table i have only 4 fields (STUNAME, MARK1, MARK2, MARK3).
    I have to calculate the total at runtime and if total is greater than 150, then actual record should display in report.
    For example, I can use formula to calculate total but I can calculate total only after each record prints. But i want to calculate total internally only it should display in report if condition satisfies. I dont want to keep one more field for total in crystal report.
    Please help me on this.

    If the filter is not "pushed" down to the database, then Crystal will pull all of the data into memory and then filter it there.  This is not a big deal if you only have a couple of records, but it can significantly slow down a report that is pulling in more than a few records.
    An alternative, if your report is just linking tables in the Database Expert, would be to create a SQL Expression that adds the three fields together.  Using this in the Select Expert WILL push the condition down to the database.
    If you're using a command instead of linking table, they you would add this to the Where clause of the comment.  If you're using a stored proc instead of tables or a command, you would want to do this filter in the code for the stored proc.
    -Dell
    Message was edited by: Dell Stinnett-Christy

  • Code for checking that all fields on a SubForm has a record (based on a condition)

    I have a Main Form called Brewing.
    On that main form there is a Sub Form (BrewingSubFormHops), see below.
    "Min" is the time in boiling wort. Figure comes from recipe
    "g/l" gram/liter, is the recommended amount of Hops based on wort in boiler. Figure comes from recipe
    "added" is the real amount of hops the brewer should add into the wort.
    "DryHop?" Yes / No. Data comes from recipe
    If the "DryHop?" is No (0 ) then the hops will be added later, weeks later, and a record in the "added" field is not
    required at this moment.
    If the "DryHop?" is Yes (-1) then all the fields "added" must be completed before closing the MainForm/SubForm.
    Picture 1 shows the SubForm that is right.
    Picture 2 shows a SubForm with one missing data in field "added". How can I avoid this by prompting the user to "remember" to complete "added" on row two?
    Best // Peter Forss Stockholm and Sigtuna GMT +1.00

    Your form doesn't seem to agree with your description as in the former it is the rows with a Dryhop? value of TRUE which are NULL in both examples.  I'll assume the description is the correct basis.
    In the subform's BeforeUpdate event procedure validate the data as follows, setting the return value of the Cancel argument to True if the validation criteria are not satisfied:
    Const MESSAGE_TEXT = "Your message text goes here."
    If Me.[DryHop?] Then
        If IsNull(Me.[Min]) Or _
           IsNull(Me.[g/l]) Or _
           IsNull(Me.[added]) Then
             MsgBox MESSAGE_TEXT, vbExclamation, "Incomplete Data"
             Cancel = True
        End If
    End If
    If it's the form which is correct rather than the description change the first line to:
    If Not Me.[DryHop?] Then
    You can, and in my opinion should, also enforce this in the table definition by using an appropriate expression as the table's ValidationRule property.
    From a presentational point of view would it not be better to use a check box as the Dryhop column's display control, or at least format the column as Yes/No?  Access's implementation of Boolean TRUE and FALSE values as -1 and 0 is unlikely to be evident
    to most users.
    Ken Sheridan, Stafford, England

  • Returning filtered records based on login password

    Hi,
    I'm using MySql / PHP / DW
    I have a 'login.php' page which has two fields USERNAME & PASSWORD, and a 'jobs.php' page which displays a list of Client's jobs.
    I need Client A to log in and only see Client A's jobs and so on for my various clients...
    I have two tables, USER and JOBS.
    USER TABLE
    client_id (primary key)
    username
    password
    access_level
    JOBS TABLE
    job_id (primary key)
    client_id
    client
    site
    install_date
    I need 'jobs.php' to display Client A's jobs based on the username and / or password he has entered.
    Do I have to create a query in MySql or can I achieve this within DW in recordsets or something? I'm very confused!
    Thanks

    I had to do something very similar using .asp and MSaccess many years ago, back then I had to create a query in Access which merged the two tables together and then I created a recordset from it in Dreamweaver. Can I replicate this using MySql / Phpmyadmin and have the query show up in Dreamweaver so I can create a recordset from it?
    Back to present day...How do I retrieve the id of a record?
    On the client records page I have entered this data below - can you tell me if this is correct? When I test it records are displayed but when I preview in browser the records page comes back empty.
    Simple view:
    Advanced view:

  • Query range must return one record based on the amount passed.

    Hi,
    Need to return the correct line_number based on the amout that falls within that range.
    Example;- if current amount value is 100 then line_number should return 1.
    if current amount value is 420 then line_number should return 4.
    Line_Num Amount
    *1 149.99*
    2 300
    3 400
    *4 500*
    5 9999999.99
    I need this in a query to resolve this, not PL/SQL.
    Thanks in advance.

    select min(line_num) from yourtable where amount >= &input_amount

  • Doubt in the value return by the function based on given condition

    Hi,
    Below function should return the value based on the value of in_row_number_high variable.
    I have used CASE but it does't return proper value.
    How could I give that condition?
    Please help.
    FUNCTION gen_total_lane_count_pct
        in_lane_id                  edr_rpt_by_ranges_output.lane_id%TYPE,
        in_direction_id             edr_rpt_by_ranges_output.direction_id%TYPE,
        in_interval_start_date_time edr_rpt_by_ranges_output.interval_start_date_time%TYPE,
        in_interval_end_date_time   edr_rpt_by_ranges_output.interval_end_date_time%TYPE,
        in_row_number               edr_rpt_by_ranges_output.range_low%TYPE,
        in_row_number_high          edr_rpt_by_ranges_output.range_high%TYPE,
        in_lane_min                    edr_lane_by_class_report_data.v_lane%TYPE,
        in_lane_max                    edr_lane_by_class_report_data.v_lane%TYPE   
    RETURN NUMBER
    IS
         my_total             NUMBER(18);
         my_count_pct         NUMBER(18);
         my_total_pct         NUMBER(18);
         my_each_count_result NUMBER(18);
         my_count_result            NUMBER(18);
         my_total_count             NUMBER(18);
      lane_start           edr_rpt_tmp_report_lanes.site_lane_id%TYPE;
      lane_end             edr_rpt_tmp_report_lanes.site_lane_id%TYPE;
      row_start            NUMBER(12);
      row_end              NUMBER(12);
    BEGIN
         my_count_pct       := 0 ;
           SELECT MIN(site_lane_id)
             INTO lane_start
          FROM edr_rpt_tmp_report_lanes
         WHERE edr_rpt_tmp_report_lanes.output_lane_id        = in_lane_id
           AND edr_rpt_tmp_report_lanes.output_direction_id   = in_direction_id;
         SELECT MAX(site_lane_id)
              INTO lane_end
           FROM edr_rpt_tmp_report_lanes
          WHERE edr_rpt_tmp_report_lanes.output_lane_id        = in_lane_id
            AND edr_rpt_tmp_report_lanes.output_direction_id   = in_direction_id;  
         SELECT MIN(range_low)
           INTO row_start
           FROM edr_rpt_by_ranges_output;
         SELECT MAX(range_low)
           INTO row_end
           FROM edr_rpt_by_ranges_output;
               my_each_count_result := edr_rpt_lane_by_class_package.gen_total_lane_count (
                                        in_lane_id,
                                        in_direction_id,
                                        in_interval_start_date_time,
                                        in_interval_end_date_time,
                                        in_row_number,
                                        in_row_number_high,
                                        in_lane_min,
                                        in_lane_max             
        my_count_result := edr_rpt_lane_by_class_package.gen_total_lane_count (
                                        in_lane_id,
                                        in_direction_id,
                                        in_interval_start_date_time,
                                        in_interval_end_date_time,
                                        row_start,
                                        row_end,
                                        in_lane_min,
                                        in_lane_max              
         my_total_count :=  edr_rpt_lane_by_class_package.gen_total_lane_count (
                                        in_lane_id,
                                        in_direction_id,
                                        in_interval_start_date_time,
                                        in_interval_end_date_time,
                                        in_row_number,
                                        in_row_number_high,
                                        lane_start,
                                        lane_end               
             IF (my_each_count_result > 0)   THEN                          
                   my_total := edr_rpt_lane_by_class_package.gen_total_lane_count (
                                        in_lane_id,
                                        in_direction_id,
                                        in_interval_start_date_time,
                                        in_interval_end_date_time,
                                        row_start,
                                        row_end,
                                        lane_start,
                                        lane_end              
                CASE
                WHEN in_row_number_high = row_end THEN                       
                     my_count_pct := ROUND((my_each_count_result / my_count_result ) * 100 );     
                WHEN in_row_number_high < row_end THEN                
                        my_count_pct := ROUND((my_each_count_result / my_total ) * 100 ) ;               
                ELSE
         RAISE_APPLICATION_ERROR(-20101, 'The row number specified is not recognized.');                 
                END CASE;     
             END IF;
             return my_count_pct;
    END gen_total_lane_count_pct;          Edited by: Indhu Ram on Jan 5, 2010 11:43 AM

    Hi,
    I have to find out the % value for each column value( For rx if there is only 2 columns).To calculate the % , each column value should get divided by the total of that column value.
    To calculate the % for the total row , each column total should get divide by the sum of column ( sum of column 1 + sum of column 2)values.
    ROUND((my_each_count_result / my_count_result ) * 100 ) ---- This calculaton gives me the % for the total row.
    ROUND((my_each_count_result / my_total ) * 100 ) ------- This calculation gives the % for the data row.
    The condition that I have tried is, if it reaches end of data row (to achieve that condition I have given this condition in_row_number_high < row_end )
    Now my result returns only values based on this condition -- in_row_number_high < row_end .
    It does't look for the first condition.
    CASE
    WHEN in_row_number_high = row_end THEN
    my_count_pct := ROUND((my_each_count_result / my_count_result ) * 100 );     
    WHEN in_row_number_high < row_end THEN
         my_count_pct := ROUND((my_each_count_result / my_total ) * 100 ) ;               
    ELSE
    RAISE_APPLICATION_ERROR(-20101, 'The row number specified is not recognized.');          
    END CASE;
    Hope , you have some good understanding now. Please help me how could I do this?

  • Delete duplicate records based on condition

    Hi Friends,
    I am scratching my head as how to select one record from a group of duplicate records based upon column condition.
    Let's say I have a table with following data :
    ID   START_DATE   END_DATE    ITEM_ID     MULT    RETAIL            |                      RETAIL / MULT
    1     10/17/2008   1/1/2009     83     3     7                 |                            2.3333
    2     10/17/2008   1/1/2009     83     2     4                 |                            2
    3     10/17/2008   1/1/2009     83     2     4                 |                            2
    4     10/31/2008   1/1/2009     89     3     6                 |                            2
    5     10/31/2008   1/1/2009     89     4     10                |                            2.5
    6     10/31/2008   1/1/2009     89     4     10                |                            2.5
    7     10/31/2008   1/1/2009     89     6     6                 |                            1
    8     10/17/2008   10/23/2008     124     3     6                 |                            2From the above records the rule to identify duplicates is based on START_DATE,+END_DATE+,+ITEM_ID+.
    Hence the duplicate sets are {1,2,3} and {4,5,6,7}.
    Now I want to keep one record from each duplicate set which has lowest value for retail/mult(retail divided by mult) and delete rest.
    So from the above table data, for duplicate set {1,2,3}, the min(retail/mult) is 2. But records 2 & 3 have same value i.e. 2
    In that case pick either of those records and delete the records 1,2 (or 3).
    All this while it was pretty straight forward for which I was using the below delete statement.
    DELETE FROM table_x a
          WHERE ROWID >
                   (SELECT MIN (ROWID)
                      FROM table_x b
                     WHERE a.ID = b.ID
                       AND a.start_date = b.start_date
                       AND a.end_date = b.end_date
                       AND a.item_id = b.item_id);Due to sudden requirement changes I need to change my SQL.
    So, experts please throw some light on how to get away from this hurdle.
    Thanks,
    Raj.

    Well, it was my mistake that I forgot to mention one more point in my earlier post.
    Sentinel,
    Your UPDATE perfectly works if I am updating only NEW_ID column.
    But I have to update the STATUS_ID as well for these duplicate records.
    ID   START_DATE   END_DATE    ITEM_ID     MULT    RETAIL    NEW_ID   STATUS_ID |   RETAIL / MULT
    1     10/17/2008   1/1/2009     83     3     7         2         1      |     2.3333
    2     10/17/2008   1/1/2009     83     2     4                                |     2
    3     10/17/2008   1/1/2009     83     2     4           2         1      |     2
    4     10/31/2008   1/1/2009     89     3     6           7         1      |     2
    5     10/31/2008   1/1/2009     89     4     10          7         1      |     2.5
    6     10/31/2008   1/1/2009     89     4     10          7         1      |     2.5
    7     10/31/2008   1/1/2009     89     6     6                            |     1
    8     10/17/2008   10/23/2008     124     3     6                            |     2So if I have to update the status_id then there must be a where clause in the update statement.
    WHERE ROW_NUM = 1
      AND t2.id != t1.id
      AND t2.START_DATE = t1.START_DATE
      AND t2.END_DATE = t1.END_DATE
      AND t2.ITEM_ID = t1.ITEM_IDInfact the entire where_ clause in the inner select statement must be in the update where clause, which makes it totally impossible as T2 is persistent only with in the first select statement.
    Any thoughts please ?
    I appreciate your efforts.
    Definitely this is a very good learning curve. In all my experience I was always writing straight forward Update statements but not like this one. Very interesting.
    Thanks,
    Raj.

  • Retrieve records based on condition from xml using XPATH

    Hi all,
    I have a table form_content where there are two columns - form_content_id (numeric) and xml_content (clob). The structure of xml_content is
    <RAF xmlns="http://www.abnamro.com/WCS/GCEG/KRT">
    <XBorderRAF>
    <ClientProfile isBranch="1" isBusinessAddressDifferent="1" isSubsidiary="0" regDate="2009-09-01">
    <RegisteredAddress city="test" country="GB" nameOfContactPerson="3454545" street1="test" telephoneNumber="34534545"/>
    </ClientProfile>
    </XBorderRAF>
    </RAF>
    I want to retrive all form_contant_id from form_content table where country is 'GB' inside RegisteredAddress tag.
    I am able to retrive all form_content_id with country value using below query,
    SELECT form_content_id,
    extractValue(xmltype(xml_content),'/RAF/XBorderRAF/ClientProfile/RegisteredAddress/@country', 'xmlns="http://www.abnamro.com/WCS/GCEG/KRT"')
    as registerAddress
    from
    form_content
    but I am finding it difficult to reterive records based on any condition..
    SELECT form_content_id,
    extractValue(xmltype(xml_content),'/RAF/XBorderRAF/ClientProfile/RegisteredAddress/@country', 'xmlns="http://www.abnamro.com/WCS/GCEG/KRT"')
    as registerAddress
    from
    form_content
    where
    (extractValue(xmltype(xml_content),'/RAF/XBorderRAF/ClientProfile/RegisteredAddress/@country', 'xmlns="http://www.abnamro.com/WCS/GCEG/KRT"')='GB'
    Please help me to resolve this problem. ....
    Thanks

    Ok, thaks to all who looked this query, I found the answer
    select form_content_id
    from xmltest
    where existsnode(xmltype(xml_content), '/RAF/XBorderRAF/ClientProfile/RegisteredAddress[@country="GB"]','xmlns="http://www.abnamro.com/WCS/GCEG/KRT"') = 1;

  • Retrive records based on condition

    HI,
    Need some clarification on getting the records based on the condition given.
    Here is my requirement. I have developed a function module to search the records based on some input given. For suppose i get some around 10000 records for this condition. Now i need the output records in multiples.
    Like first i execute the FM based on the some condition and i should get 100 records as output and the second time i execute the FM with the same search condition i need to get the records from 101 to 200 and the third time i execute and so on.
    Is there any way of fetching the records in multiles for the same search criteria. If so can anyone let me know hot to do the same.
    Thanks in advance.
    Regards,
    Ram

    HI Ram
    Please check this example to get some idea:
    tables: vbak.
    data: it_vbak type table of vbak,
          wa_vbak1 type vbak,
          wa_vbak2 type vbak.
    data: lin type i.
    select-options: s_erdat for vbak-erdat obligatory.
    select * into table it_vbak from vbak
           package size 10
           where erdat in s_erdat.
           describe table it_vbak lines lin.
           read table it_vbak into wa_vbak1 index 1.
           read table it_vbak into wa_vbak2 index lin.
           break-point.
    endselect.
    Check the entries in IT_VBAK whenever the break-point is reached. You will have a fresh set of entries for every collection.
    Hope this gives you some idea.
    Kind Regards
    Eswar

  • Latest record need to show in report

    Hi all,
    I have a requirment on shippment is as below.
    I want track a perticulat shippment based on shippment number.
    I am loading the shippment details to My ODS and the key's are 'Shippment number' and 'Date' and 'time'.
    Now my user don't want to see all the records that are relevent to perticulat shippment in bex report, he wants to see only latest record based on date and time by entering the shippment number only as input in the report.
    Can any one advice me on this, Points will be assigned.
    Regards,
    Raja reddy.

    Reddy,
    Did you try using a replacement Path variable on Date?
    If your req is to show all the shipment no.s for a date which is latest, you can do this by condition as TOPN.
    (If date is in KF list)
    But if Date is a charecteristic in the Provider, Use repl path and then put TOP N condition on it.
    Try it! You must get that.
    Cheers,
    Sri

  • Unable to filter a formula field based on two conditions

    hi,
    I have a report with 3 subreports in it. When i drill down onto first subreport i am getting correct records in details based on selection criteria but when i am unable to populate the same record count in summary.
    For example
    On summary i see : 60 when i drill down i see 20 records which is correct but not reflecting onto summary report
    the details selection criteria is based on two  fields where field1>=1 and field2= "Text"It is coming out good (20 records) but in summary i am unable to populate this based on same selection criteria
    The main issue i am facing was if i apply a filter on summary selection criteria all the subreport counts are getting affected. Is there a way i can apply filters only one one field
    Please suggest
    Thanks!
    Lucky

    Thanks! for the prompt response Abhilash
    I got part of what you told me. Yes i am calculating the counts in the summary report.
    Basically here is the selection criteria: i need to filter the records based on two conditions. One is x.field1= "Admin" and x.field2=act_pct>=1
    For example:
    Admin Returns         50  but when i dig into details it showing as 20 records ( based on selection criteira above)
    when coming back to summary . 50 what ever is returing here is based on a formula ( if x.field1="admin" then 1 else 0) ..in addition to this i need to filter that by x.field2>=1 which is not taking it     
    Is there any syntax to do this
    If i apply a filter on this particular 50 record its bee applying for whole summary report ...
    And from what you said :
    Why don't you create the summary in the Subreport and maybe use shared variables to bring it back to the Main Report?
    i did not get this
    Please suggest
    Lucky

  • Column data restriction based on join condition

    All,
    SELECT e.ename,
      d.dname,
      e.job,
      e.comm, e.sal
    FROM emp e,
      dept d
    WHERE e.deptno=d.deptno
    AND e.empno   =7788
    AND e.comm   IS NOT NULL;
    In this query, if the comm for the empno=7788 is not null, then i am able to retrive the record, If comm is not null, display the entire record along with comm details, if it is null, display the entire record without comm value(may be as null).
    If commission is not null, then display the entire record, if it is null, then for e.comm, e.sal I want to display null values.
    PS: I am in a situtation like this kind of. Kindly give me some idea.
    Thanks

    Thank you Frank and Sorry for not so clear in explanation.
    SQL> desc demois_wo_routing
    Name                                      Null?    Type
    ORG_ID                                             NUMBER
    ASSIGNED_TO                                        VARCHAR2(10)
    COMMENTS                                           VARCHAR2(55)
    DUE_DATE                                           DATE
    ENTER_DATE                                NOT NULL DATE               -- is the APPROVED_DATE
    ENTER_USER                                         VARCHAR2(30)       -- is the APPROVER_NAME
    PRIORITY                                           NUMBER
    START_DATE                                         DATE
    STATUS                                    NOT NULL VARCHAR2(10)
    WO_NUMBER                                 NOT NULL VARCHAR2(10)
    SQL> desc demois_sr_routing
    Name                                      Null?    Type
    ORG_ID                                             NUMBER
    ASSIGNED_TO                                        VARCHAR2(10)
    COMMENTS                                           VARCHAR2(55)
    DUE_DATE                                           DATE
    ENTER_DATE                                NOT NULL DATE
    ENTER_USER                                         VARCHAR2(30)
    PRIORITY                                           NUMBER
    REQ_NUMBER                                NOT NULL VARCHAR2(10)
    START_DATE                                         DATE
    STATUS                                    NOT NULL VARCHAR2(10)
    SQL> select enter_user,wo_number,enter_Date,status from demois_wo_routing where wo_number='WO000853';
    ENTER_USER                     WO_NUMBER  ENTER_DAT STATUS
    E60075334                      WO000853   14-MAY-13 APPROVED
    C10000001                      WO000853   09-APR-13 OPEN
    C10000001                      WO000853   09-APR-13 ASSIGNED
    E60075373                      WO000853   09-APR-13 PND APPRVL
    SQL> select enter_user,req_number,enter_date,status from demois_sr_routing where req_number=(select req_number from demois_req where wo_number='WO000853');
    ENTER_USER                     REQ_NUMBER ENTER_DAT STATUS
    E60075373                      SR000818   09-APR-13 SCHEDULED
    E60075334                      SR000818   14-MAY-13 SCHEDULED
    C10000001                      SR000818   09-APR-13 SCHEDULED
    C10000001                      SR000818   09-APR-13 SCHEDULED
    SQL> SELECT fm.first_name || ' ' || fm.last_name AS approver_name,
      2         fpc.communication_value AS approver_cell
      3    FROM demo_person fm, demo_person_communication fpc
      4   WHERE fm.person_code = 'E60075334'   --ENTER_USER from FSR/ FWR table
      5     AND fm.person_id = fpc.person_id
      6     AND fpc.communication_type = 'Telephone';
    APPROVER_NAME
    APPROVER_CELL
    Nicy Varghese
    44952937
    SQL> SELECT fm.first_name || ' ' || fm.last_name AS approver_name,
      2         fpc.communication_value AS approver_cell
      3    FROM demo_person fm, demo_person_communication fpc
      4   WHERE fm.person_code =
      5                         (SELECT enter_user
      6                            FROM demois_wo_routing
      7                           WHERE wo_number = 'WO000853' AND status = 'APPROVED')
      8     AND fm.person_id = fpc.person_id
      9     AND fpc.communication_type = 'Telephone';
    APPROVER_NAME
    APPROVER_CELL
    Nicy Varghese
    44952937
    ACTUAL QUERY :---- If i run the below query i will definitely get a record of WO_NUMBER along with the other details.
    SELECT   w.wo_number,
             DECODE (NVL (p.print_wo_barcode_flag, 'N'),
                     'Y', '*' || w.wo_number || '*',
                     NULL
                    ) barcode_wo_number,
             INITCAP (w.maint_type) maint_type, INITCAP (w.wo_status) wo_state,
             w.priority, w.modify_date, w.start_date, w.due_date,
             INITCAP (w.description) description, w.method, w.crew,
             INITCAP (w.craft) craft, w.assigned_to, w.est_hours,
             DECODE (w.req_type,
                     'S', 'Service',
                     'P', 'PM',
                     'W', 'Project',
                     'T', 'Standing WO'
                    ) req_type,
             w.req_number, INITCAP (w.requestor) requestor, w.telephone,
             w.mail_code, w.department, w.cp_number, w.site, w.building,
             NVL (w.phase, 0) phase, w.FLOOR, w.room, w.equipment,
             INITCAP (w.close_comments) close_comments, w.alt_requestor,
             w.alt_telephone, w.alt_department, w.alt_mail_code, w.schedule_user,
             DECODE (w.equipment,
                     NULL, NULL,
                     '*' || w.equipment || '*'
                    ) equip_barcode,
             INITCAP (e.NAME) employee_name, w.schedule_date, w.pl_number,
             INITCAP (q.nomenclature) nomenclature, INITCAP (q.mfr) mfr,
             INITCAP (w.nonavailable_time) nonavailable_time, w.enter_user,
             a.asset_class_code, INITCAP (a.description) asset_class_description
        FROM demois_emp e,
             demois_equip q,
             demois_req w,
             demois_fmm_config p,
             demois_asset_class a
       WHERE w.wo_number IS NOT NULL
         AND NVL (w.asset_type, 'N') = 'N'
         AND w.equipment = q.equipment(+)
         AND w.assigned_to = e.employee(+)
         AND (w.assigned_to = :p_assigned_to OR :p_assigned_to IS NULL)
         AND (   (:p_marked_to_print = 'Y' AND w.print_ticket = 'Y')
              OR (:p_wo_number IS NOT NULL)
         AND (w.site = :p_site OR :p_site IS NULL)
         AND (w.crew = :p_crew OR :p_crew IS NULL)
         AND (w.craft = :p_craft OR :p_craft IS NULL)
         AND (w.maint_type = :p_maint_type OR :p_maint_type IS NULL)
         AND (w.req_type LIKE NVL (:p_req_type, '%'))
         AND (w.wo_number LIKE NVL (:p_wo_number, '%'))
         AND q.asset_class_id = a.asset_class_id(+)
    ORDER BY wo_number;
    The above is the sample data for my logic. The last one is the original query which am modifying with the tables listed above. The last query will return a record based on WO_NUMBER. Now i have to passing the WO_NUMBER to FWR table checking whether it is in APPROVED status or not. If not need to in FSR table with the associated SR ( req_number) for that WO_NUMBER.
    If there is a record in 'APPROVED' state, need to display the approver name from FP and FPC tables by passing value of the ENTER_USER from FSR /  FWR. Till now it is fine , if the STATUS is 'APPROVED'.
    The problem here is, there may a situation where the WO_NUMBER ( or respective SR number) will not be in 'APPROVED' status in both the tables. In this case also i need to get the record as usual like if i run the last query (ACTUAL QUERY).
    There is no direct relation between the FSR and FWR tables here. Only DEMOIS_REQ table has REQ_NUMBER (SR000818) and WO_NUMBER ( WO000853 ).
    Again sorry, If i am not clear enough.
    Thanks in advance.

  • Latest record

    Hi All
    In a report i have the following set of records:
    DISTRICT     SUB_DISTRICT     REC_TIMESTAMP
    ‚Ö WAYNE     ˜ÖLA SUB     12/29/2009 16:20
    GREENFIELD     TIPTON SUB     12/30/2009 8:13
    GREENFIELD     TIPTON SUB     12/30/2009 9:05
    GREENFIELD     TIPTON SUB     12/30/2009 9:05
    LA PORTE     RENSSELAER SUB     12/29/2009 16:36
    LA PORTE     RENSSELAER SUB     12/29/2009 16:36
    VINCENNES     PAOLI SUB     12/29/2009 15:06
    VINCENNES     VINCENNES SUB     12/29/2009 16:23In the above set of records i want to show only the latest record based on the time stamp, so i used the following calculation:LAST_VALUE(Os Activity.Rec Timestamp) OVER(PARTITION BY Os Activity.Sub District )
    But for some reason its giving me the following set:
    DISTRICT     SUB_DISTRICT     CALCULATION     REC_TIMESTAMP
    FORT WAYNE     ANGOLA SUB     12/29/2009 16:20     12/29/2009 16:20
    GREENFIELD     TIPTON SUB     12/30/2009 9:05     12/30/2009 8:13
    GREENFIELD     TIPTON SUB     12/30/2009 9:05     12/30/2009 9:05
    GREENFIELD     TIPTON SUB     12/30/2009 9:05     12/30/2009 9:05
    LA PORTE     RENSSELAER SUB     12/29/2009 16:36     12/29/2009 16:36
    VINCENNES     PAOLI SUB     12/29/2009 15:06     12/29/2009 15:06
    VINCENNES     VINCENNES SUB     12/29/2009 16:23     12/29/2009 16:23Can anyone please advice me where i'm going wrong?
    Thanks

    Please see the following set of records after i added the calculation:
    DISTRICT     SUB     COUNT1     COUNT2     COUNT3     CALCULATION
    FORT WAYNE     ANGOLA SUB     0     7     0     12/29/2009 16:20
    GREENFIELD     TIPTON SUB     5     0     5     12/30/2009 9:05
    GREENFIELD     TIPTON SUB     0     0     0     12/30/2009 9:05
    GREENFIELD     TIPTON SUB     5     0     0     12/30/2009 9:05
    LA PORTE     RENSSELAER SUB     0     0     0     12/29/2009 16:36
    VINCENNES     PAOLI SUB     5     0     20     12/29/2009 15:06
    VINCENNES     VINCENNES SUB     0     0     0     12/29/2009 16:23I used the MAX function.
    Edited by: thinkingeye on Dec 30, 2009 10:59 AM

  • Returning only records that have no entry in a specific field

    Hello,
    I'm new to this forum and relatively new to Crystal Reports, so I apologize if this information is "plain as day" somewhere.  I've been hunting for this info for quite a while and can't find anything that makes sense!
    I am trying to put together a very simple report that will return only records where a certain field is blank.
    It is an Access database, in an information management program specific to our type of business.  CR 11 software was included with the LIMS (Laboratory Information Management System), as the reports generated from the LIMS software are in CR.  I've learned enough to be able to tweak reports and make some basic reports, but I can't find a simple answer to what I think is a simple question! 
    I have the report set up to access the following fields in the database:  Invoice number, Client name, Due date, Invoice amount, and Payment Received.  This will be used as a "cross check" - not as any major accounting tool - as the Payment Received field is simply the date that the payment was received.  If no payment has been received, the field is left blank.
    I want the report to return ONLY those records where that field is blank (i. e. a list of invoices/clients whose payment has not yet been received).
    Ideally, I would also like to be able to return only those records where that field is blank OR within a certain date range (i. e. less than 30 days from the date that the report is run).  However, we'll probably only use this report a couple of times a month, so that is not as critical.
    I know I need to filter the records according to the Payment Received field, but don't know how to construct a formula that says "just show the fields where there is nothing entered."
    Thank you!
    Brenda

    This is the hardest function to find:  IsNull
    If isNull(field) then  
        //  if the above fails on a numberic field or text field then see the following two-some databases are picky.
    If IsNull(field) or field=0 then  //this one maybe needed for a numeric field
    If IsNull(field) or field=""    //  this one maybe needed for a text field
    if IsNull(field) or field=<value>
    or
    if IsNull(field) or field like <value*>
    You always want the IsNull statement to be the first statement, and it is especially usefull when testing for not equal
    If IsNull(field) or field <>  <value>

Maybe you are looking for