<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: How to sort based on user input values in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaa-p/13715340#M4831098</link>
    <description>Thanks for your approach</description>
    <pubDate>Wed, 29 May 2024 04:51:59 GMT</pubDate>
    <dc:creator>shashikiran_r</dc:creator>
    <dc:date>2024-05-29T04:51:59Z</dc:date>
    <item>
      <title>How to sort based on user input values</title>
      <link>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaq-p/13711716</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;How to sort the data based on the user input values in the selection screen&lt;/P&gt;&lt;P&gt;for example below is the data in the database table TABLE T1&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;Column 1&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;Column 2&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;Column 3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;B&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;z&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;B&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;3&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;C&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;4&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;A&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;z&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;5&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;D&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;x&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;6&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;C&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;x&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;7&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;A&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;8&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;B&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;TD&gt;z&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%"&gt;10&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;A&lt;/TD&gt;&lt;TD width="33.333333333333336%"&gt;z&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;user input for sorting the data&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="50%"&gt;Column2&lt;/TD&gt;&lt;TD width="50%"&gt;Column3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;B&lt;/TD&gt;&lt;TD width="50%"&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;A&lt;/TD&gt;&lt;TD width="50%"&gt;z&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;D&lt;/TD&gt;&lt;TD width="50%"&gt;x&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;C&lt;/TD&gt;&lt;TD width="50%"&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Expected output.&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD height="30px"&gt;Column1&lt;/TD&gt;&lt;TD height="30px"&gt;Column2&lt;/TD&gt;&lt;TD height="30px"&gt;Column3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;2&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;B&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;8&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;B&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;1&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;B&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;z&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;7&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;A&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;4&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;A&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;z&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;10&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;A&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;z&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;9&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;D&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;z&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;5&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;D&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;x&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;3&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;C&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;6&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;C&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="30px"&gt;x&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;How this can be achieved in SQL ?&lt;/P&gt;&lt;P&gt;How sorting can be achieved if the data is available in the internal table ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Shashikiran.&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2024 09:30:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaq-p/13711716</guid>
      <dc:creator>shashikiran_r</dc:creator>
      <dc:date>2024-05-24T09:30:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort based on user input values</title>
      <link>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaa-p/13711946#M4830677</link>
      <description>&lt;P&gt;Nowhere you indicate the order so you can't sort. Add a column in a table to indicate the order (e.g. B = order 1, A = order 2, D = order 3, C = order 4) and do the join + ORDER BY.&lt;/P&gt;&lt;P&gt;If you have HANA, you should be able to join an internal table.&lt;/P&gt;&lt;P&gt;If not, there are lots of solutions: read and sort in memory, store the internal table into a database table and join + ORDER BY, etc.&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2024 12:53:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaa-p/13711946#M4830677</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2024-05-24T12:53:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort based on user input values</title>
      <link>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaa-p/13715088#M4831082</link>
      <description>&lt;P&gt;You can approach this the same way in SQL as in ABAP. Create a temporary table, loop over the conditions, read the data with the conditions as keys and append the data to the temp table. Note that there should be a more efficient way using joins etc.&lt;/P&gt;&lt;LI-CODE lang="abap"&gt;CLASS zcl_application DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    TYPES :
      ty_col_1 TYPE i,
      ty_col_2 TYPE c LENGTH 1,
      ty_col_3 TYPE c LENGTH 1.

    TYPES : BEGIN OF ty_data,
              col_1 TYPE ty_col_1,
              col_2 TYPE ty_col_2,
              col_3 TYPE ty_col_3,
            END OF ty_data.

    TYPES tt_data TYPE STANDARD TABLE OF ty_data WITH KEY col_1.

    TYPES : BEGIN OF ty_user_input,
              col_2 TYPE ty_col_2,
              col_3 TYPE ty_col_3,
            END OF ty_user_input.

    TYPES tt_user_input TYPE STANDARD TABLE OF ty_user_input WITH KEY col_2.

    INTERFACES if_amdp_marker_hdb.

    CLASS-METHODS sort
*      AMDP OPTIONS CDS SESSION CLIENT current
      IMPORTING
                VALUE(it_unsorted_table) TYPE tt_data
                VALUE(it_user_input)     TYPE tt_user_input
      EXPORTING VALUE(et_sorted_data)    TYPE tt_data.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.
CLASS zcl_application IMPLEMENTATION.

  METHOD sort BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY.

    declare lt_result, lt_stack table like :et_sorted_data;
    declare i, j, lv_cond_lines_1, lv_cond_lines_2 integer;
    declare lv_condition_col_2, lv_condition_col_3  nvarchar( 1 );

    lt_conditions_col_2 = SELECT col_2 FROM :it_user_input;
    lv_cond_lines_1 = ::ROWCOUNT;
    lt_conditions_col_3 = SELECT col_3 FROM :it_user_input WHERE col_3 &amp;lt;&amp;gt; '';
    lv_cond_lines_2 = ::ROWCOUNT;

    /* DML enabled data as a temp stack to handle duplication */
    INSERT INTO :lt_stack SELECT * FROM :IT_UNSORTED_TABLE;

    FOR i IN 1..LV_COND_LINES_1 DO
        FOR j IN 1..LV_COND_LINES_2 DO

            lv_condition_col_2 = :lt_conditions_col_2.col_2[ i ];
            lv_condition_col_3 = :lt_conditions_col_3.col_3[ j ];

            INSERT INTO :lt_result (
                    SELECT *
                    FROM
                        :lt_stack
                    WHERE
                        col_2 = lv_condition_col_2 and
                        col_3 = lv_condition_col_3
                 );

             DELETE FROM :lt_stack
             WHERE
                    col_2 = lv_condition_col_2 and
                    col_3 = lv_condition_col_3;

        END FOR;
    END FOR;

    et_sorted_data = select * from :lt_result;
  ENDMETHOD.
ENDCLASS.

CLASS ltc_application DEFINITION FOR TESTING
  RISK LEVEL HARMLESS
  DURATION SHORT.

  PRIVATE SECTION.

    METHODS test  FOR TESTING RAISING cx_static_check.

ENDCLASS.


CLASS ltc_application IMPLEMENTATION.

  METHOD test.

    DATA lt_user_input TYPE zcl_application=&amp;gt;tt_user_input.
    DATA lt_unsorted_table TYPE zcl_application=&amp;gt;tt_data.
    DATA lt_expected TYPE zcl_application=&amp;gt;tt_data.
    DATA lt_actual TYPE zcl_application=&amp;gt;tt_data.

    lt_user_input = VALUE #(
        ( col_2 = 'B' col_3 = 'y' )
        ( col_2 = 'A' col_3 = 'z' )
        ( col_2 = 'D' col_3 = 'x' )
        ( col_2 = 'C' col_3 = '' )
    ).

    lt_unsorted_table = VALUE #(
        ( col_1 = 1  col_2 = 'B' col_3 = 'z' )
        ( col_1 = 2  col_2 = 'B' col_3 = 'y' )
        ( col_1 = 3  col_2 = 'C' col_3 = 'y' )
        ( col_1 = 4  col_2 = 'A' col_3 = 'z' )
        ( col_1 = 5  col_2 = 'D' col_3 = 'x' )
        ( col_1 = 6  col_2 = 'C' col_3 = 'x' )
        ( col_1 = 7  col_2 = 'A' col_3 = 'y' )
        ( col_1 = 8  col_2 = 'B' col_3 = 'y' )
        ( col_1 = 9  col_2 = 'D' col_3 = 'z' )
        ( col_1 = 10 col_2 = 'A' col_3 = 'z' )
    ).

    lt_expected = VALUE #(
        ( col_1 = 2  col_2 = 'B' col_3 = 'y' )
        ( col_1 = 8  col_2 = 'B' col_3 = 'y' )
        ( col_1 = 1  col_2 = 'B' col_3 = 'z' )
        ( col_1 = 7  col_2 = 'A' col_3 = 'y' )
        ( col_1 = 4  col_2 = 'A' col_3 = 'z' )
        ( col_1 = 10 col_2 = 'A' col_3 = 'z' )
        ( col_1 = 9  col_2 = 'D' col_3 = 'z' )
        ( col_1 = 5  col_2 = 'D' col_3 = 'x' )
        ( col_1 = 3  col_2 = 'C' col_3 = 'y' )
        ( col_1 = 6  col_2 = 'C' col_3 = 'x' )
    ).

    zcl_application=&amp;gt;sort(
      EXPORTING
        it_unsorted_table = lt_unsorted_table
        it_user_input     = lt_user_input
      IMPORTING
        et_sorted_data    = lt_actual
    ).

    cl_abap_unit_assert=&amp;gt;assert_equals(
      EXPORTING
        act = lt_actual
        exp = lt_expected
    ).
  ENDMETHOD.
ENDCLASS.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 00:07:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaa-p/13715088#M4831082</guid>
      <dc:creator>Romanos</dc:creator>
      <dc:date>2024-05-29T00:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort based on user input values</title>
      <link>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaa-p/13715340#M4831098</link>
      <description>Thanks for your approach</description>
      <pubDate>Wed, 29 May 2024 04:51:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaa-p/13715340#M4831098</guid>
      <dc:creator>shashikiran_r</dc:creator>
      <dc:date>2024-05-29T04:51:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort based on user input values</title>
      <link>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaa-p/13715484#M4831122</link>
      <description>&lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/884751"&gt;@shashikiran_r&lt;/a&gt; The answer is about HANA SQL (SQLScript), but was your question about ABAP SQL or HANA SQL or other database SQL? You only mention ABAP in the tag of your question.</description>
      <pubDate>Wed, 29 May 2024 06:34:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-to-sort-based-on-user-input-values/qaa-p/13715484#M4831122</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2024-05-29T06:34:52Z</dc:date>
    </item>
  </channel>
</rss>

