Help in sorting the data using Rank

Hello Experts
I am having trouble in selecting the record which is one less than the maximum rank  Please see the desired results part.
I am trying using the Rank or Dense_rank but I could not.
Please help in solving this issue.
The Oracle version I am working on is
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Thanks
RB
with t as
SELECT 29951 SOURCE_ID,'03/4/2013' CREATE_DATE, 'AAAA' NAME FROM DUAL UNION ALL
SELECT 29951 SOURCE_ID,'03/11/2013' CREATE_DATE, 'BBBB' NAME FROM DUAL UNION ALL
SELECT 29951 SOURCE_ID,'03/21/2013' CREATE_DATE, 'CCCC' NAME FROM DUAL UNION ALL
SELECT 29952 SOURCE_ID,'03/14/2013' CREATE_DATE, 'SSSS' NAME FROM DUAL UNION ALL
SELECT 29952 SOURCE_ID,'03/4/2013' CREATE_DATE, 'TTTT' NAME FROM DUAL UNION ALL
SELECT 29952 SOURCE_ID,'03/24/2013' CREATE_DATE, 'PPPP' NAME FROM DUAL UNION ALL
SELECT 29953 SOURCE_ID,'03/01/2013' CREATE_DATE, 'MMMM' NAME FROM DUAL UNION ALL
SELECT 29953 SOURCE_ID,'03/11/2013' CREATE_DATE, 'RRRR' NAME FROM DUAL UNION ALL
SELECT 29954 SOURCE_ID,'03/24/2013' CREATE_DATE, 'WWWW' NAME FROM DUAL
SELECT row_number() over (PARTITION BY source_id ORDER BY SOURCE_ID,create_date  ) rank, source_id,create_date
  FROM t
SOURCE_ID          CREATE_DATE            NAME
29951                    03/11/2013                   BBBB
29952                    03/14/2013                   SSSS
29953                    03/1/2013                     MMMM
29954                    03/24/2013                   WWWW

select  source_id,create_date,name
from (
SELECT row_number() over (PARTITION BY source_id ORDER BY create_date  ) rank, source_id,create_date,name
  FROM t
where rank = 1
Perhaps you want to handle the dates as dates.
Do you want the first or the second rank?
Could there be equal create_dates in one partition?
What do you expect as second for source_id=29954?
select  source_id,create_date,name
from (
SELECT row_number() over (PARTITION BY source_id ORDER BY to_date(create_date,'MM/DD/YYYY')  ) rank, source_id,create_date,name
FROM t
where rank = 2
SOURCE_ID
CREATE_DATE
NAME
29951
03/11/2013
BBBB
29952
03/14/2013
SSSS
29953
03/11/2013
RRRR
Ah, now i see, you want descending order
select  source_id,create_date,name
from (
SELECT row_number() over (PARTITION BY source_id ORDER BY to_date(create_date,'MM/DD/YYYY') desc ) rank, source_id,create_date,name
FROM t
where rank = 2
Finally i might have understood
with t as
SELECT 29951 SOURCE_ID,'03/4/2013' CREATE_DATE, 'AAAA' NAME FROM DUAL UNION ALL
SELECT 29951 SOURCE_ID,'03/11/2013' CREATE_DATE, 'BBBB' NAME FROM DUAL UNION ALL
SELECT 29951 SOURCE_ID,'03/21/2013' CREATE_DATE, 'CCCC' NAME FROM DUAL UNION ALL
SELECT 29952 SOURCE_ID,'03/14/2013' CREATE_DATE, 'SSSS' NAME FROM DUAL UNION ALL
SELECT 29952 SOURCE_ID,'03/4/2013' CREATE_DATE, 'TTTT' NAME FROM DUAL UNION ALL
SELECT 29952 SOURCE_ID,'03/24/2013' CREATE_DATE, 'PPPP' NAME FROM DUAL UNION ALL
SELECT 29953 SOURCE_ID,'03/01/2013' CREATE_DATE, 'MMMM' NAME FROM DUAL UNION ALL
SELECT 29953 SOURCE_ID,'03/11/2013' CREATE_DATE, 'RRRR' NAME FROM DUAL UNION ALL
SELECT 29954 SOURCE_ID,'03/24/2013' CREATE_DATE, 'WWWW' NAME FROM DUAL
, ranks as (
SELECT
row_number() over (PARTITION BY source_id ORDER BY to_date(create_date,'MM/DD/YYYY') desc ) rank
,source_id
,create_date
,name
FROM t
select
source_id
,create_date
,name
from ranks r
where rank =
case
when (select count(*) from ranks where source_id = r.source_id) = 1
then 1 else 2
end
SOURCE_ID
CREATE_DATE
NAME
29951
03/11/2013
BBBB
29952
03/14/2013
SSSS
29953
03/01/2013
MMMM
29954
03/24/2013
WWWW
Message was edited by: chris227 extended

Similar Messages

  • HT4203 I have turned off the Data use on the I-phone 4s but am told by AT&T that data is still being used for applications.  How is this possible. Can someone help me understand this.  Thanks.

    I have turned off the Data use on the I-phone 4s but am told by AT&T that data is still being used for applications.  How is this possible. Can someone help me understand this. 

    I assume you are including the STIX font as part of your epub files?     
    Perhaps the folks who do this blog might be able to help -- they have done some work with font embedding:
    http://www.pigsgourdsandwikis.com/2011/04/embedding-fonts-in-epub-ipad-iphone-an d.html

  • Getting error while importing the data using loadercli

    Hello,
    I want to copy the data using export/ import  via (loadercli).
    scenario:
    1) I have 2 server and i want to export data from old server and import in to new MAXDB server using loadercli.
    I have done this once and it went fine , but now I want to do the export import again so i will have te latest data on the new server.
    when i tried to do that it gives error that the table already exist . Can i use loadercli to import hte data again ?
    can any one help me in this ?
    Regards,
    Bhavesh

    > Do you want to add and/or update the data in the already existing tables or do you want to replace the content completely?
    >
    > so in that way :
    > bot the options are fine what ever take less time.
    Sorry mate, but YOU have to know what you want here.
    I gave you an easy to follow set of steps.
    As you don't seam to mind the outcome, just might just use them...
    > I wanted to know weathe i can use the  loadercli for thie export import or not? if yes then is there any new steps to do before i do the export import?
    We had this discussion before...
    >
    > For that the easiest option would be just to drop the tables of SAPR3 and run the import again.
    >
    > For ease of use you could also just do:
    > - logon as superdba
    > - drop user SAPR3
    > - create user SAPR3 password SOMEPW not exclusive dba
    >
    > After these steps you can easily pump the data into the database again.
    >
    > So here in th above given steps , i am creating a new SAPR3 user and why it is not exclusive dba ?
    >  i already have that user SAPR3 can i use the same.
    Yes, you do have the SAPR3 user.
    But you don't seem to like to read documentation or learn about how the tools work or anything like that.
    Therefore I gave you s simple way to reach your goal.
    Of course it's possible to reuse the user.
    But then you would have to deal with already existing tables, already existing data etc.
    You don't seem to be able to do that. So, the easy steps might be better suited for your needs.
    regards,
    Lars

  • Error while sending the data using input schedule

    Dear Friends,
    I am unable to send the data using input schedule due to following error is occur while sending the data.
    The Error Message : Member (H1) of dimension (ENTITY) is not a base member (parent or formula)
    Can anyone please help me to resolve the above error.
    Thanks and regards,
    MD.

    Hi,
    You are trying to send data to a parent/node, you can only send data in BPC to lowest-level children (base mamabers) of any dimension.
    "H1" is a parent in the entity dimension so you should try sending to a child.
    Tom.

  • Fetching the data using Db link through cursor

    Hi ,
    I created the cursor in which i want to access the data using Db link , can u tell the way to access the data
    Code Which gives error
    for rec in (
         SELECT ac_no as ac_code,drcr_ind as dr_cr,lcy_amount as amt
              FROM actb //"Public Synonym Of Db Link "
              Where period_code <> 'FIN'
              And AC_NO = '100130000'
              AND AC_BRANCH = '254'
    Its Urgent.
    With Regards,
    Anand

    An ORA-00600 error indicates an internal Oracle error. There are literally thousands of possible reasons that you might get one of these, but it generally boils down to Oracle bugs (you should never get this sort of error).
    - You'll need to log a support request with Oracle Support
    - There should be a number of parameters (generally rather cryptic) that come with the ORA-00600 error. You should look up those particular parameters in the Metalink ORA-00600 lookup tool
    - Hopefully, that tool will point you at a patch or a workaround. If not, you'll need to get support to figure out what's going on behind the scenes.
    Justin

  • TSV_TNEW_PAGE_ALLOC_FAILED error while loading the DATA using DTP

    Hi,
    While loading the data using DTP for 2  DSO's we are gettig the error
    TSV_TNEW_PAGE_ALLOC_FAILED
    can any one kindly help me out regarding the same.
    Thank You,
    Poornima.

    Hi Soundarya,
    Thanks a lot for the reply. But i found that its running fine in development, where as coming to quality its throwing an error. These happened for Two DSO's. In both the transformations i have identified that the Transformation names are different from Development and Quality..
    There are no routines written for them and no select statements have been used
    Can you please suggest me regarding the same.
    Edited by: Poornima Gayatri on Mar 22, 2010 7:00 AM

  • How to insert the data using matrix

    Hi
      All how to insert the data using matrix feild
      plz help me
       thanks
        Loy

    Hi Loy,
    The best way to fill an entire matrix is to use a DBDatasource, here is some sample code:
    Dim oForm As SAPbouiCOM.Form
    oForm = SBO_App.Forms.Item("YourFormTypeID")
            '1. Add a DBDataSource to the form
            oForm.DataSources.DBDataSources.Add("OUSR")
            Dim oColumnDBS As SAPbouiCOM.Column
            Dim oColumnUDS As SAPbouiCOM.Column
            Dim i As Integer
            Dim GenEdt As SAPbouiCOM.EditText
            Dim oMatrix As SAPbouiCOM.Matrix
            Dim oColumns As SAPbouiCOM.Columns
            Dim oDBDataSource As SAPbouiCOM.DBDataSource
            oMatrix = oForm.Items.Item("7").Specific
            oColumns = oMatrix.Columns
            '2. DBDataSource: Binding a field / alias of the table to a column
            oColumnDBS = oColumns.Item("V_1")
            oColumnDBS.DataBind.SetBound(True, "OUSR", "U_NAME")
            '3. getting the data sources bound to the form
            oDBDataSource = oForm.DataSources.DBDataSources.Item("OUSR")
            oMatrix.Clear()
            ' Querying the DB Data source
            oDBDataSource.Query()
            ' Adding the data to the matrix
            oMatrix.LoadFromDataSource()
    When you want to fill a specific field use :
    GenEdt = oMatrix.Columns.Item("V_2").Cells.Item(1).Specific
                    GenEdt.String = "Hello"

  • I have downloaded and installed the latest version of numbers on my mac. Everytime I save and then try to reopen that document, I receive a message telling me that I need a new version of numbers. Also, when I try to sort the date column, it sorts out of

    I have downloaded and installed the latest version of numbers on my mac. Everytime I save and then try to reopen that document, I receive a message telling me that I need a new version of numbers. Also, when I try to sort the date column, it sorts out of order. The last version sorted fine.

    Welcome to Apple Support Communities
    When you install the new iWork version, the old iWork version is kept, so it looks like you are opening your old version.
    To fix this, open a Finder window, choose Applications in the sidebar and drag the new Numbers version to the Dock, so you can access to it quickly. Open all documents from this version. I don't recommend you to delete the old Numbers version in case you need it.
    Respecting to the second question, you will get better answers in the Numbers for OS X forum

  • Error while uploading the data using FM"upload"

    Hi,
    I encountering an error while uploading the data using text file with FM " UPLOAD"
    The error is "File does not exist or cannot be opened "
    But there is a file with name and extenstion right.
    Regards
    Vishnu

    You have to create RC29P-IDNRK(var) using concatenate statement. Try this.
    DATA: new_mark TYPE bdcdata-fnam.
    CONCATENATE 'RC29P-IDNRK(' var ')' INTO new_mark.
    PERFORM bdc_field USING new_mark W_BOM-QTY

  • Need help in formatting the Date - Date does not

    Need help in formatting the Date - Date does not formats and give Not a valid month error in the below scenario.
    select oc.ST_PGM_MGR, r.ag_dnum, get_major_work_type(r.perf_eval_rtng_id) "v_work_code", r.ag_dnum_supp "supp", r.intfinal, to_char(r.formdate,'MM/DD/YYYY') "formdate", to_char(r.servfrom,'MM/DD/YYYY') "srv_from", to_char(r.servto,'MM/DD/YYYY') "srv_to", descript, add_months(to_char
    --- Bellow line of Code on trying to format it to mm/dd/yyyy gives the error
    (r.formdate, 'DD-MON-YYYY'),12) "formdate2"
    from  table REdited by: Lucy Discover on Jul 7, 2011 11:34 AM
    Edited by: Lucy Discover on Jul 7, 2011 1:05 PM

    Your syntax is wrong - look at the post above where this syntax is given:
    to_char (add_months(r.formdate,12), 'MM/DD/YYYY') "formdate2"Look at the formula from a logical perspective - "inside out" to read what is happening -
    take formdate, add 12 months
    add_months(r.formdate, 12)then apply the to_char format mask - basic syntax
    to_char(date, 'MM/DD/YYYY')Compare to your syntax:
    to_char(add_months(r.formdate, 'MM/DD/YYYY'),12) "formdate2"You will see your format string inside the call to add_months, and your 12 inside the call to to_char.
    Good luck!

  • Getting Error while accessing the data using odata service

    Hi All,
    Iam new to SAP FIORI. 
    Iam getting the below error while accessing the data using odata service.
    "Failed to load resource: the server responded with a status of 404 (Not found)"
    "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin "
    i have tried all the solutions like changing the url pattern "proxy/htttp".
    and disabled - security in chrome (Chrome is Updated version).
    i tried with IE still got the same problem.
    And installed all the required software in eclipse
    While installing GWPA plugin i got the following error.
    let me know if any one have idea.
    Thanks in advance.

    > Do you want to add and/or update the data in the already existing tables or do you want to replace the content completely?
    >
    > so in that way :
    > bot the options are fine what ever take less time.
    Sorry mate, but YOU have to know what you want here.
    I gave you an easy to follow set of steps.
    As you don't seam to mind the outcome, just might just use them...
    > I wanted to know weathe i can use the  loadercli for thie export import or not? if yes then is there any new steps to do before i do the export import?
    We had this discussion before...
    >
    > For that the easiest option would be just to drop the tables of SAPR3 and run the import again.
    >
    > For ease of use you could also just do:
    > - logon as superdba
    > - drop user SAPR3
    > - create user SAPR3 password SOMEPW not exclusive dba
    >
    > After these steps you can easily pump the data into the database again.
    >
    > So here in th above given steps , i am creating a new SAPR3 user and why it is not exclusive dba ?
    >  i already have that user SAPR3 can i use the same.
    Yes, you do have the SAPR3 user.
    But you don't seem to like to read documentation or learn about how the tools work or anything like that.
    Therefore I gave you s simple way to reach your goal.
    Of course it's possible to reuse the user.
    But then you would have to deal with already existing tables, already existing data etc.
    You don't seem to be able to do that. So, the easy steps might be better suited for your needs.
    regards,
    Lars

  • Upload the data using BAPI in LSMW

    Hi
      I want to upload the data using the BAPI in LSMW,but i dont know what value should be passed in
      1- Business Object
      2- Method
    fields.
    Ex- Suppose i want to upload the Material Master Data then what should be the value of these two fields.
    Thanks
    Mrutyunjaya Tripathy

    Hi
      Sorry to say that i am little bit confuse with your answer
    1-The business object already contains some object name wheather i have to give object names or different,As you have mentioned to give the tcode than what tcode that would be
    2-If i am giving any internal table name in the method field it is giving me error because this field is related to the business object field
    Thanks
    Tripathy m

  • Can I sort the images used in a book's browser by the # of times used?

    I'm also producing a DVDSP slideshow of the event's (selected) photos not included in the book.
    Q2: Can I sort the images used in a book's browser by the # of times used?
    That way I don't have to search thru hundreds of photos. When used in a book, each photo gets a badge showing the # of times used. Can this badge be used in a sort?
    Or must I apply a keyword to the used (or unused) photos?
    Thanks.

    But if the file has been embedded, the file name is lost.

  • How do I sort the data in the spreadsheet?

    This is a fairly basic question and something I do all the time.  I need to sort the data on a particular column or set of columns and am frustrated that it is this difficult to find.  In trying to figure this out I know also have a chart on my spreadsheet that I can't seem to delete. 

    If you don't see a gray bar above the table, then tap a cell to show the gray bar.
    Select a column by tapping the gray bar directly above the column. After the column is selected, tap the exact same spot again to show a menu.
    In the menu, tap on Sort... etc...
    Sort will sort the selected column, but will also update the entire table accordingly. If you don't want the rest of the table to update, then you have to copy that column (with the columns that you do want to update) and paste them onto the canvas so they appear in their own table. Sort the column then and paste it back into the original table.
    Delete the chart from a menu that appears similarly as Sort. Tap on the chart one time to select it. Tap it again to show the menu with Delete. If you are doing this and choosing Delete, but the chart is not disappearing, then there might be a problem with the file.

  • Transfering the data using OPEN DATASET

    Hi all,
    I am trying to get the data using OPEN DATASET to down load the data and using TRANSFER i am transfering.
    i got strucked while i am downloading.
    Please send coding how to use open dataset and transfer.
    Thanks in advance
    Venkat

    Hi ,
    you can try this code frangment.
    open the file to be created on application server.
      OPEN DATASET app_path FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
      IF sy-subrc NE 0.
        MESSAGE e001(zs) .
      ELSE.
    get header data
         PERFORM get_data_header.
    *transfer data to application server file.
        TRANSFER v_header TO app_path.
    get segment data
        PERFORM get_data_segment.
    *transfer data to application server file.
        TRANSFER v_segment TO app_path.
    *clear work areas and local variables.
        PERFORM clear_local.
    get trailer record.
        PERFORM get_data_trailer.
    *transfer data to application server file.
        TRANSFER v_trailer TO app_path.
      ENDIF.
      CLOSE DATASET app_path.
    regards,
    Sumit.

Maybe you are looking for