How to get the last (max of string) row with multiple records for same ID

Hi all,
I have a query that returns for each client ID the telephone nr, mobile phone nr, email etc... 
There is also a column FLG_DEFAULT that indicates if the phone is the default one or not (N= changed).
Basically what I would like my query to return is the phone number where FLG_DEFAULT = N, if this number is NULL or if FLG_DEFAULT =N does not exist I want to return the phone nr from DEFAULT_FLG = Y.
Anyone know how I can do this in SQL? I cannot get a maximum because the format is string (e.g. +32/xxx xx xx or xxx/xx xxx xx.
Thanks!

;With cte As
(Select ClientID, PhoneNumber,
Row_Number() Over(Partition By ClientID,
Order By Case When FLG_DEFAULT = 'N' And PhoneNumber Is Not Null Then 1
When DEFAULT_FLG - 'Y' Then 2 End) As rn
From YourTableName)
Select ClientID, PhoneNumber
From cte
Where rn = 1;
Replace column names and table name with your column names and table name.
Tom

Similar Messages

  • How to get the last transaction in a row in SQL Developer?

    What syntax would I use to get the last transaction of a row in SQL developer?
    The way I have my query set-up currently it is not returning the correct data, here is my current syntax:
    select ssn, max(tran_id), chng_type,tran_id
    from pda_tran
    where ssn = 'xxx-xxx-0011'
    and chng_type = 'C'
    group by ssn, chng_type,tran_id;
    It returns a 'C' chng_type but it is not the last one. when I query on this ssn this is what I get:
    ssn tran_id chng_type
    xxx-xxx-0011 001 A
    xxx-xxx-0011 002 E
    xxx-xxx-0011 003 C
    xxx-xxx-0011 004 S
    xxx-xxx-0011 005 C
    xxx-xxx-0011 006 T
    I only want to return the ssn's with a last transaction chng_type of 'C'. How can I get the correct information returned. Please advise.

    From what I see and read... there is one to many group by
    You wrote
    select ssn, max(tran_id), chng_type,tran_id
    from pda_tran
    where ssn = 'xxx-xxx-0011'
    and chng_type = 'C'
    group by ssn, chng_type,tran_id;
    If you want the max(tran_id), remove it from the "group by"
    select ssn, chng_type, max(tran_id)
    FROM
    (SELECT 'xxx-xxx-0011' ssn, '001' tran_id, 'A' chng_type FROM DUAL UNION
    SELECT 'xxx-xxx-0011' ssn, '002' tran_id, 'E' chng_type FROM DUAL UNION
    SELECT 'xxx-xxx-0011' ssn, '003' tran_id, 'C' chng_type FROM DUAL UNION
    SELECT 'xxx-xxx-0011' ssn, '004' tran_id, 'S' chng_type FROM DUAL UNION
    SELECT 'xxx-xxx-0011' ssn, '005' tran_id, 'C' chng_type FROM DUAL UNION
    SELECT 'xxx-xxx-0011' ssn, '006'tran_id, 'T' chng_type FROM DUAL )
    where ssn = 'xxx-xxx-0011'
    and chng_type = 'C'
    group by ssn, chng_type;

  • How to get the last day of the week?

    Hii
    i can get the calender week number for any given date using
    SELECT to_char(to_date('04/04/2011','MM/DD/YYYY'),'WW') FROM dual
    can any body tell me, how to get the last day of that week ?
    and the answer should be 04/08/2011(8th april )
    thanks
    San
    Edited by: sandeep9 on Apr 4, 2011 3:50 AM

    Hi, San,
    Here's one way:
    WITH     sample_data     AS
         SELECT  DATE '2011-04-04'     AS dt
         FROM     dual
    SELECT  dt
    ,     TO_CHAR (dt, 'WW')     AS week_num
    ,     NEXT_DAY ( dt - 1
               , TO_CHAR ( TRUNC (dt, 'YEAR') - 1
                      , 'Day'
               )          AS end_o_week
    FROM     sample_data;Another way is to use date arrithmetic:
    WITH     sample_data     AS
         SELECT  DATE '2011-04-09'     AS dt
         FROM     dual
    SELECT  dt
    ,     TO_CHAR (dt, 'WW')     AS week_num
    ,     TRUNC (dt, 'YEAR')
          + (7 * CEIL ( (dt - (TRUNC (dt, 'YEAR') - 1))
                / 7
          - 1               AS using_date_arithmetic
    FROM     sample_data;

  • How to get the last day of a month?

    HI,
    I want to know how to get the last day of a month.
    In my JClient form, I tried to get it by using oracle.sql.Date method, that is:
    lastday=oracle.sql.Date anydate.lastDayOfMonth();
    But it does not work. The result is lastday=anydate.
    Why?
    Stephen

    You can use the Calender class...
    Calendar c = Calendar.getInstance();
    and then something like...
    c.add(c.MONTH, 1);
    int dayOfMonth = c.get(Calender.MONTH);
    c.add(c.DAY_OF_MONTH, - (dayOfMonth-1) );
    other usefull functions are:
    System.out.println(" YEAR : " + c.get(Calendar.YEAR));
    System.out.println(" MONTH : " + c.get(Calendar.MONTH));
    System.out.println(" DAY_OF_MONTH : " + c.get(Calendar.DAY_OF_MONTH));
    System.out.println(" DAY_OF_WEEK : " + c.get(Calendar.DAY_OF_WEEK));
    System.out.println(" DAY_OF_YEAR : " + c.get(Calendar.DAY_OF_YEAR));
    System.out.println(" WEEK_OF_YEAR : " + c.get(Calendar.WEEK_OF_YEAR));
    System.out.println(" WEEK_OF_MONTH : " + c.get(Calendar.WEEK_OF_MONTH));
    System.out.println(" DAY_OF_WEEK_IN_MONTH : " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.println(" HOUR : " + c.get(Calendar.HOUR));
    System.out.println(" AM_PM : " + c.get(Calendar.AM_PM));
    System.out.println(" HOUR_OF_DAY (24-hour): " + c.get(Calendar.HOUR_OF_DAY));
    System.out.println(" MINUTE : " + c.get(Calendar.MINUTE));
    System.out.println(" SECOND : " + c.get(Calendar.SECOND));
    System.out.println();*/

  • How to get the last day according to fiscal period input in selection scree

    Hello expert
    how to get the last day of fiscal period input.
    the fiscal period inculdes 1-16
    when fiscal period is greater than 12, only calculate the last day of 12nd month
    your solution will be apprecaited, FM existing?
    thank you
    Kevin

    Hi,
    when you give a particular date in any month
    the following fm will give you the last date of that month
    here you can give
    R_FDATE-HIGH  as 01 and month as the period you wnat and year for current year
    concatenates '01'  month year  into r_fdate-high separated by '.'.
    then it will give g_ltdt for that month and year which wil be the last date of that month
        CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
          EXPORTING
            DAY_IN            = R_FDATE-HIGH
          IMPORTING
            LAST_DAY_OF_MONTH = G_LTDT
          EXCEPTIONS
            DAY_IN_NO_DATE    = 1
            OTHERS            = 2.
        IF SY-SUBRC <> 0.
          MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
        ENDIF.
    thanks & regards,
    Venkatesh

  • How to get the last error for while loop?

    How to get the last error for while loop? I just want transfer the last error for while loop, but the program use the shift register shift all error every cycle.

    What do you mean by "get" and "transfer"?
    If the shift register is not what you want, use a plan tunnel instead.
    Typically, programmers are interested in the first, not last, error.
    Can you show us your code so we have a better idea what you are trying to?
    LabVIEW Champion . Do more with less code and in less time .

  • How to get the last version of flash in MSI format automatically?

    How to get the last version of flash in MSI format automatically?
    Roberto Neigenfind
    Bravo Tecnologia
    www.bravotecnologia.com.br

    Hi Barbara,
    Flash Professional CS5.5 is a 32-bit application which can be installed on computers with either 32-bit or 64-bit operating systems.
    You can purchase this by Adobe's backward Licensing policy :
    " Adobe allows program members to order a current-version license but use a prior version. These members can contact Adobe Customer Service to request a serial number for the earlier version if they do not already have one. Prior-version software is available via ESD and can be purchased through standard resellers. The program member must follow all guidelines of the current-version EULA. "
    Please check the doc : http://www.adobe.com/volume-licensing/policies.html

  • How to get the "last changed by" for a set of function modules?

    How to get the "last changed by" for a set of function modules?
    is there any table to get it??

    See [this|Re: Date of creation of function module] I posted earlier.
    >TFDIR will give you the name of the function group program and the include number.
    >E.g. SAPLZFUNCGROUP Include 01.
    >From this you can construct the include name: LZFUNCGROUPU01.
    >You can look this up in TRDIR to find the creation date (CDAT) of the function module.
    In your case, you need unam and udat.
    matt

  • How to get the last page  SAP Script form

    How to get the last page  SAP Script form.
    I want to print a specific information in the last page of SAP form (Script). Please tell me how to get the last page number.
    Regards

    Hi
    You have to check the system variable &NEXTPAGE&, if it's 0 it means you're in the last page.
    From SAP Help:
    This symbol is used to print the number of the following page. The output format is the same as with &PAGE& .
    Note that on the last page of the output, in each window that is not of type MAIN, &NEXTPAGE& has the value 0.
    /: IF &NEXTPAGE& = '0'
       Last page
    /: ENDIF
    Max

  • How to get the last revision quote to SSRS report

    HI,
    My report current result like this
    Qoute ID
    Revision
    Q1
    1
    Q1
    2
    Q1
    3
    Q2
    1
    Q3
    1
    I would like to filter if the quote ID is same , only get the last revision quote into report
    Qoute ID
    Revision
    Q1
    3
    Q2
    1
    Q3
    1
    How can i compare the Quote ID and Revision in fetchxml or dataset filter ?
    Thank You

    <fetch distinct='false' mapping='logical' aggregate='true'> 
        <entity name='Quote'> 
           <attribute name='revisionnumber' alias='estimatedvalue_max' aggregate='max' /> 
           <attribute name='quotenumber' alias='quoteid' groupby='true' /> 
        </entity> 
    </fetch>"
    Regards, Saad

  • How to get the last series of an invoice- DebitMemo

    Hi,
    I need help to know how I can get the last series number of an Invoice with documentsubType = bod_DebitMemo. I can get the las series number of Invoices with the following code:
    private int getSeries(string documento)
                SAPbobsCOM.CompanyService oCompServ;
                SAPbobsCOM.SeriesService oSeriersService;
                SeriesCollection oSeriesCollection;
                Series oSeries;
                DocumentTypeParams oDocumentTypeParamas;
                int elAnterior = 0;
                try
                    //Get company service
                    oCompServ = vCmp.GetCompanyService();
                    //Get series service
                    oSeriersService = (SAPbobsCOM.SeriesService)oCompServ.GetBusinessService(ServiceTypes.SeriesService);
                    //Get series collection
                    oSeriesCollection = (SAPbobsCOM.SeriesCollection)oSeriersService.GetDataInterface(SeriesServiceDataInterfaces.ssdiSeriesCollection);
                    //Get document type params
                    oDocumentTypeParamas = (SAPbobsCOM.DocumentTypeParams)oSeriersService.GetDataInterface(SeriesServiceDataInterfaces.ssdiDocumentTypeParams);
                    //Set document type
                    oDocumentTypeParamas.Document = documento;
                    /*if (documento.Equals("13"))
                        oDocumentTypeParamas.DocumentSubType = "bod_DebitMemo";
                    //Get series collection
                    oSeriesCollection = oSeriersService.GetDocumentSeries(oDocumentTypeParamas);
                    oSeries = oSeriesCollection.Item(0);
                    elAnterior = oSeries.NextNumber;
                catch (Exception e)
                    MessageBox.Show("Se presento un error: " + e.Message);
                return elAnterior;
    And I try to use the DocumentSubType property of DocumentTypeParamas but it seems to be invalid,
    I'll appreciate if you can help me....
    Thanks.....

    Hi,
         Use describe statment.
    data: lv_line type i.
        Describe table itab lines lv_line.
        read table itab into wa_itab index lv_line.
    regards,
    Santosh Thorat

  • How to get the Last cell in HSSFCell (Excel sheet)

    Hello
    I am trying to convert an excel sheet to a tab limited file.
    I am using HSSF and unable to track how do i know if the cell encountered is a last filled cell in excel sheet
    Some of the cells in the sheet can be blank. In that case i am just using inputting a '\t' for that cell and read the next one. But coz of this when i get the last cell, a tab is included for that too.
    Can someone let me know how can i rectify this?
    it seems like HSSFCell does not have any methos like lastCell or so
    Thanx

    then use getLastCellNum() in org.apache.poi.hssf.usermodel.HSSFRow

  • How to Get the last data dymanic

    in my bussiness scenarios,I have a cube store some data by month
    but some material have no records in some month
    I need to check if this month have records,get it
    or if there is no records, get the last records by month
    how can i do this?

    You check in R3...whether you have material data for that month...if yopu have data, in BW, run a repair full request by giving this criteria i.e. month criteria.
    Thanks...
    Shambhu

  • How to get the last element of a structure in a textfield in DesignStudio

    Hello,
    how can I get the last element of a structure within a bex query?
    Within the structure we are working with dates that dynamically are buiId (input date - offsets for several months).
    I have to show the last value in a single textfield within DesignStudio.
    Regards
    Frank

    Hi Frank,
    in DS 1.3 you can use forEach function to get the last element of BEx structure (or n-th element in general using if condition)
    var array = DS_CROSSTAB1.getMembers("DHGY5D6XEFO1K45SB00BXFH7A",10);
    var lastMember="";
    array.forEach(function(element, index) {
           lastMember = element.text;
    TEXT_1.setText("Last structure element: "+lastMember);

  • How to get the last 3 digits of a Delivery Number

    Hi All,
    On the Transfer Order Collective form, the Delivery Number is printed. But I need to capture the last 3 digits of that delivery number and make the 3 digits to be printed on the same form with bigger font size. I would like to know how to capture the last 3 digits.

    If so, your form(subroutine) can look like this.
    form  get_last_3_digits tables co_sym_using   structure itcsy
                                   co_set_symbols structure itcsy.
      data: xvbeln type likp-vbeln.
    * Get the USING Values
      read table co_sym_using with key name = 'LIKP-VBELN'.
      check sy-subrc eq 0.
      shift co_sym_using-value left deleting leading space.
      xvbeln  = co_sym_using-value.
    * make sure it is internal format
      call function 'CONVERSION_EXIT_ALPHA_INPUT'
           exporting
                input  = xvbeln
           importing
                output = xvbeln.
    * Update the value
      read table co_set_symbols index 1.
      co_set_symbols-value = xvbeln+7(3).
      modify co_set_symbols index 1.
    endform.
    Regards,
    Rich Heilman

Maybe you are looking for

  • Error message: variable might not have been initialized

    i am doing java programming and keep getting error messages: variable might not have been intitialized, the variables are 'one', 'two', and 'three' ,but at the very beginning i already had typed in String one,two,three...how do i fix this? this is pa

  • IllegalArgumentException when going to connected status

    My MIDlet works beautifully inside the Palm emulator. It makes an HTTP request to my servlet, and correclty handles the binary data it gets back, using HttpConnection.openInputStream(). However, when I run the same MIDlet from my Visor Neo (after fir

  • Does anyone know how to pull songs off my iPod nano formatted for a Mac to a PC?

    I Have an old iPod nano that was originally formatted for a Mac that is no longer around.  I am trying to import the songs onto my pc but cannot because re formatting will erase everything.  Any suggestions?

  • How is BO used with BW

    I am a BW consultant with knowledge in Business objects 6.5 and Crystal reports 8.Can any one please explain me in detail how BO and Crystal reports are used with BW. When Crystal reports are used for reporting on BW data? when BO is used? how the un

  • Cd is not showing up in wmp

    When I rip a cd to Windows media player it does not show up in my library files