Order rows on the basis of score

I have a table like this format:
ID
COL1
COL2
1
i am a boy
good is male
2
i am a good boy
good is male
3
i am a good girl
gender is female
Now the user will enter a keyword (in this example, 'good'). Based on that, if the keyword is found in column1, score will be given 2, and if the keyword is found in column2, score will be given 5.
And at the end, I have to display the ID column based on the score (max score at the top).
So the results should come like this:
ID
SCORE
2
5
1
7
3
2
How can I do?

For example like this
with sample_table as (
                       select 1 id,'i am a boy' col1,'good is male' col2 from dual union all
                       select 2,'i am a good boy','good is male' from dual union all
                       select 3,'i am a good girl','gender is female' from dual union all
                       select 4,'goodie girl','gender is female' from dual union all
                       select 5,'good girl','gender is female good' from dual
select  id,
CASE WHEN
         case
           when regexp_like(col1, '(^| )' || :keyword || '( |$)') then 2
           else 0
        end +
        case
          when regexp_like(col2, '(^| )' || :keyword || '( |$)') then 5
          else 0
        end  = 0
THEN 10
ELSE
         case
           when regexp_like(col1, '(^| )' || :keyword || '( |$)') then 2
           else 0
        end +
        case
          when regexp_like(col2, '(^| )' || :keyword || '( |$)') then 5
          else 0
        end
END score
  from  sample_table
  order by score desc

Similar Messages

  • Order by on the basis of last characters of the column

    hi
    i have this type of data in Column
    ABL - Allied Corp. services (XES) - Newyork
    ABL - Allied Corp. services (XES) - Chicago
    SBS - Logistics - Newyork
    ETC..
    As the last characters are for city and i want to perform order by on the basis of city.The naming convention is "the name always contain two hiphens(-)
    and the city name in the last".(means after second '-')
    Regards

    SQL> INSERT INTO dt_test VALUES('ABL - Allied Corp. services (XES) - Newyork');
    1 row created.
    SQL> INSERT INTO dt_test VALUES('ABL - Allied Corp. services (XES) - Chicago');
    1 row created.
    SQL> INSERT INTO dt_test VALUES('SBS - Logistics - Newyork');
    1 row created.
    SQL>
    SQL> select
    2 column_1
    3 FROM
    4 dt_test
    5 ORDER BY
    6 SUBSTR(column_1, INSTR(column_1,'-',1,2) + 2);
    COLUMN_1
    ABL - Allied Corp. services (XES) - Chicago
    ABL - Allied Corp. services (XES) - Newyork
    SBS - Logistics - Newyork

  • Order by on the basis of condition

    I want to make order by in sql query result with condition, if data will b numaric the it should be order by on the basis of to_number else on the basis of to_char.
    Edited by: Siddharth Singh on May 17, 2012 2:24 PM

    Kiran wrote:
    decode(LOWER(trim(col1)), UPPER(trim(col1)),to_number(trim(col1)) ,trim(col1));Did you read my reply to Ankit Rathi? Again, expression can't return values of different type. But in your case it is even worth. DECODE returns datatype based on first decoded expression. Therefore your decode will return number and as a result will fail on very first non-numeric value:
    SQL> with t as (
      2             select '1' col1 from dual union all
      3             select '2' from dual union all
      4             select 'A' from dual
      5            )
      6  select  col1
      7    from  t
      8    order by decode(LOWER(trim(col1)), UPPER(trim(col1)),to_number(trim(col1)) ,trim(col1));
      order by decode(LOWER(trim(col1)), UPPER(trim(col1)),to_number(trim(col1)) ,trim(col1))
    ERROR at line 8:
    ORA-01722: invalid number
    SQL> But even if you would come up with decode where first decoded expression is a string your decode wuold be useless since at the end all values would be converted back to strings. If by numeric OP means strings with digits only:
    ORDER BY CASE WHEN REGEXP_LIKE(col1,'^\d+$') THEN TO_NUMBER(col1) END NULLS LAST,
    col1;For example:
    with t as (
               select '10' col1 from dual union all
               select '2' from dual union all
               select '1A' from dual union all
               select '1B' from dual union all
               select '1C' from dual
    select  col1
      from  t
      ORDER BY CASE WHEN REGEXP_LIKE(col1,'^\d+$') THEN TO_NUMBER(col1) END NULLS LAST,
               col1
    CO
    2
    10
    1A
    1B
    1C
    SQL> SY.

  • Production List and Sales Order list on the basis of Sales Forecast - MRP

    Hi all,
    One of our user wants to use MRP. what he wants is that he puts sales forecast in the system and in the basis of that sales forecast, 2 reports should be generated from system , one will tell that when production should be started to meet sales forecast for every finished good and 2nd report will tell that when sales orders should be created for raw materials to achieve sales forecast for every finished good.
    Is that possible?? Please tell me in steps that how I should I proceed? what changes should be done in SPRO and in master data?
    Please respond.
    Best Regards,
    AI.

    Hi AL,
    As such, this is a basic functioanlity of MRP.
    Once you enter a forecast & take a MRP run system proposes, when to start the production based on the production leadtime &  purc.reqns for the raw materials based on the procurement lead time.
    And the same can be viewed from various reports..FOr example MD04.
    For this to work the MRP configurations should be valid.
    Could you please confine your question to a specific query..?
    Thanks & regards
    Mahesh

  • PM preventive PM Order confirmation on the basis of object list

    Dear Experts,
    In paintenance plan I have funtional location.In object list I have 5 Equipments.Now when order is generated there are 4 operations.All the operation is same to all equipments.Is it right.
    Now when I am going for its confirmation,I don't know how to do the same on the basis of Equipment i.e object list.Can any one help me out with the steps involved in the same.
    Regards,
    Rajesh.

    Hi ,
    1.In your case here we would do confirmation for each order operation and that will be applicable for all the eqpts in the oblect list of the order.
    2. If you want to generate separate order with same operations for different eqpts then use maintenance item in the maintenance plan .Thus maintenance order gets generated for each maintenance item in the maintenance plan.The same order would be applicable for the list of eqpts in the object list of maintenance item ..
    I hope this would clarify ..
    regrds
    pushpa

  • Block Order TECO on the basis of operation User Status

    Hi Experts,
    I am trying to restrict the TECO of maintenance order on the basis of order operation status. I have configured the user status profile and assigned it to order operation. This user status profile have two statuses, INCL - Incomplete and another is CMPL - Complete. I have prohibited the TECO for user status INCL. But when I create an order and try to TECO, the system allows me TECO it enenif the operation user status is INCL.
    Could you Pl. tell me, have I missed something in configuration?

    Dattatraya,
    Actually you can control TECO by completion confirmation of order operation as PCNF ( partial confirmation) for operation which you have mentioned in Order. So you can set up in configuration like allow TECO only once CNF ( Full confirmation ) for all operations mentioned in Order.
    For your mentioned scenario , I dont feel based on user status you can stop the TECO, anyway you can check with other experts as well.
    Riyaj

  • BDC uploading on the basis on first row

    hi experts,
    my requirement is, i have a flat file in excel with csv format, and i have the details of bank details with company name and debit account number and etc., in the first row,
    and from second row all the transactions done in that debit account had followed.
    my requirement is debit account is having hyphens in the number like,
    011-222545-434, i need to remove those hyphens and need to move to single string.
    like 011-222545-434 = 011222545435
    and then based on this, we need to check it out whether this number is available in the database of SAP, if it matches then,
    we need upload all the transactions under this account had to upload into database from the second row.
    so please help me out, how to remove hyphens and how to move to one string and how to validate with the sap system??
    and after this how to upload the data into database from the second row on the basis of first row details??
    it is very urgent, please help me out
    murali.

    Hi Check out this logic. It will work for ur requirement.
    DATA: w_data(20) TYPE c VALUE '011-222545-434'.
    WRITE:/1 'Before replacing', w_data.
    REPLACE ALL OCCURRENCES OF '-' IN w_data WITH space.
    CONDENSE w_data.
    WRITE:/1 'After replacing', w_data.
    If ur variable which hold this number is not character type then pass that value to character variable like above and then use REPLACE statement.
    Thanks,
    Vinod.

  • Error: One of the base documents has already been closed (-10)

    I have a web service to get and set pick lists and add delivery notes via DI Server. but I have one scenario that causes the error 'Error: One of the base documents has already been closed (-10)':
    i have a pick list with item A1 with a quantity of 10. I pick and delivery 3. now the pick list is closed and in the picklist creation dialog I can choose the remaining 7 of A1 for another picklist. but if I do so and then try to pick and delivery the remaining 7, I'm getting that error. The sales order, that is the base document of the pick list and the delivery, is still 'open'. so, what base document is meant here?

    I found the error. I tried to add the DocumentsAdditionalExpenses to the second delivery, too, and this is not possible.

  • How to find the number of idocs generated for a customer on the basis of his purchase order in a day ?

    How to find the number of idocs generated for a customer on the basis of his purchase order in a day ?

    Dear Friends,
    I am absolutely agree with your answer .
    But my question is,
    Lets say.....
    One customer sending X number of purchase orders in a day , so how many IDocs generated on that specific day for that specific customer .
    So, Question is , How can we find the no of sales orders(IDocs) generated for the customers on the specific day ?
    Hope you all understood my requirement .
    Thanks & Regards,
    Aditya

  • Error on Sales Order - u201Cyou cannot open rows in the document"

    Hi everyone,
    Does anyone have any idea why this error would be occuring when a user tries to add a sales order?
    u201Cyou cannot open rows in the document [sales order u2013Rows u2013 Rows status] [line0] [Message131 -194]?
    If the user closes the sales order and then recaptures it, the sales order goes through. As you can imagine, the order clerks are getting a bit angry when their large orders have to be closed.
    Thanks.
    Kind regards,
    Simon Carolin

    Hi
    If it is affecting your business severly ,log a support to SAP .Meanwhile ,
    Lets start some troubleshooting your system
    1.Are you using formatted search in Sales Order
    2.Are  you using sp_transaction notification
    3.Did you do any changes in your system recently like upgrade ,
    4.Is it happen only to certain item or BP
    Hint :
    1.Disable all addon
    2.Disable your formatted search
    3.Disable sp_transaction notification
    4.Try to create a sales order and .,
    Does error occur again? Please also specify your SBO version and Patch level
    <Note> Try test your issue  test environment
    This will help to narrow down
    Thank you
    Bishal

  • How to order CM25 profile on the basis of a field values?

    Hi,
    I'd like to order a capacity planning table on the basis of values (decreasing) of the field USR02, that is already present in the table. Where could I set that records must be shown in that order? I attach a screenshot to explain you better my scenario
    Thank you.
    Angelo

    Hi Angelo,
    Try to use t-code OPDT and define a sort criteria profile with field USR02, after that assign the sort profile into your list profile (With t-code OPD0). Hope it can help you!
    Best regards
    Tao

  • Cost on the basis of order quantity

    Can the cost be decided on the basis of order quantity ? For example when 100 pieces are taken the cost is 10 each but when 1 peice is purchased the cost is 50 each,
    Similarly this should reflect when we sell the item in customer service.

    hi,
    the scales would do that job for you. You do not need to explicitly mention a formulae at VK11 when maintaining condition records. And remember cost at sales order level has a different meaning. We use the term Cost to talk about VPRS condition. Normally the costs and prices at sales order level can be transferred to COPA for profitability analysis. We map these pricing conditions to COPA value fields for management reporting purpose.
    regards
    sadhu kishore

  • Need Reports  in decrreasing order on the basis of measure value

    Got ASO Cube (3 dimensions) and have some records on it.
    Product/Enity/Measure
    Prod1 ent1 678
    prod2 ent1 700
    prod3 ent1 500
    when i retrive reports ,
    Can i able to get a reports in the decreasing order on the basis of Measure values like below ? (using any code or scripts)
    Prod2 ent1 700
    Prod1 ent1 678
    prod3 ent1 500

    If you are using the classic addin or report scripts, you can get the values in decreasing order. To do it in the add-in you can either use freeform with a report script or better yet, use the query designer to create and save the report. IF you are using a report script, you can set a sort descending and also a top x to only bring back a certain number of members. I'm not sure if the same is available through smartview. Using MDX you can do a topcount function

  • Reg: How to view the PR on the basis of Re-order point based PR

    Hi All,
    How can i view the Purchase requisition on the basis of Re-order point based PR.
    Thanks & Regards
    Kannan Somasundaram

    Go to Transaction code ME53N
    Under Item Detail find the Tab "Contact Person "
    from that you can find Creation Indicator , which is generally B Material Requirement Planning for Re order level planning operation done through planning.
    For report You can look at transaction code : ME5A
    You can look at Table EBAN and field : ESTKZ
    Edited by: sudhansu satapathy on Mar 18, 2010 9:04 AM

  • How to preset the order of rows in the outer query of a correlated query ?

    Good morning,
    I have the following simple query:
    select empno,
           ename,
           sal,
           sum(case
                 when rn = 1 then sal
                 else -sal
               end) over (order by sal, empno) as running_diff
       from (
             select empno,
                    ename,
                    sal,
                    row_number() over (order by sal, empno) as rn
               from emp
              where deptno = 10
             );That calculates a running difference and uses "row_number() over (...)" which is an Oracle specific feature to do so. It yields the following result (which we will consider correct):
         EMPNO ENAME             SAL RUNNING_DIFF
          7934 MILLER           1300         1300
          7782 CLARK            2450        -1150
          7839 KING             5000        -6150I wanted to come up with a solution that was not Oracle specific different solution. I tried the following code:
    (EDIT: after additional thought, that code is totally different in meaning and will never come close, to the above result. Consider it wrong and ignore this attempt altogether.)
    select a.empno,
           a.ename,
           a.sal,
           (select case
                     when a.empno = min(b.empno) then sum(b.sal)
                     else sum(-b.sal)
                   end
              from emp b
             where b.empno <= a.empno
               and b.deptno = a.deptno) as running_diff
      from emp a
    where a.deptno = 10;but the result is
         EMPNO ENAME             SAL RUNNING_DIFF
          7782 CLARK            2450         2450
          7839 KING             5000        -7450
          7934 MILLER           1300        -8750which is a long way from the original result. I've tried everything I could think of to order the rows before the running difference is calculated but, have been unsuccessful.
    Is there a way to change this second query --(without using Oracle specific features)-- without using windowing features that would yield the same result as the first query ?
    Rephrase of the above question:
    Is there a way, using plain vanilla SQL (that is aggregate functions and set operations such as joins and unions) to create a query that yields the same result as the first one ?
    Also, this is not for production code. This is simply an exercise in set manipulation that I'd like to see a solution for.
    Thank you for your help,
    John.
    Edited by: 440bx - 11gR2 on Jul 18, 2010 12:50 AM - correct "ho w" to "How"
    Edited by: 440bx - 11gR2 on Jul 18, 2010 1:42 AM - struck out all references to row_number and windowing features being Oracle specific features.
    Edited by: 440bx - 11gR2 on Jul 18, 2010 3:51 AM - Noted that my try is woefully wrong and restated the objective to make it clearer.

    Hi, John,
    One way to get a running total (which is basically what you want) is to do a self-join. Join each row (let's call it the current row, or c) to itself and everything that came before it (let's call this the previous row, or p), and do a regular aggregate SUM, like this:
    WITH     got_base_sal     AS
         SELECT       deptno
         ,       2 * MIN (sal)     AS base_sal
         FROM       scott.emp
         GROUP BY  deptno
    SELECT       c.deptno
    ,       c.empno
    ,       c.ename
    ,       c.sal
    ,       b.base_sal - SUM (p.sal)     AS running_diff
    FROM       scott.emp     c
    JOIN       scott.emp     p     ON     c.deptno     = p.deptno
                        AND     (     c.sal     > p.sal
                             OR     (     c.sal     =  p.sal
                                  AND     c.empno     >= p.empno
    JOIN       got_base_sal     b     ON     c.deptno     = b.deptno
    WHERE       c.deptno     IN (10)
    GROUP BY  c.deptno
    ,       c.empno
    ,       c.ename
    ,       c.sal
    ,       b.base_sal
    ORDER BY  c.deptno
    ,       running_diff     DESC
    ;Output:
        DEPTNO      EMPNO ENAME             SAL RUNNING_DIFF
            10       7934 MILLER           1300         1300
            10       7782 CLARK            2450        -1150
            10       7839 KING             5000        -6150I said you basically want a runninng total. There are two differences between a running total and your requirements
    (1) You want to have a total of the negative of what's in the table. That's trivial: use a minus sign.
    (2) You want the first item to count as positive instead of negative. That's not so trivial. The query above counts all sals as negative, but adds an offset so that it appears as if the first item had been counted as positive, not negative.
    You didn't say what you want to do in case of a tie (two or more rows having the same sal). The query above uses empno as a tie-breaker, so that all sals are calculated as if they were distinct. This is similar to what analytic functions do when the windowing is based on rows. If you want something similar to windowing by range, that might actually be simpler.
    The query above calculates a separate running_diff for each deptno, similar to "PARTITION BY deptno" in analytic functions. You happen to be interested in only one deptno right now, but you can change the main query's WHERE clause, or omit it, and the query will still work. If you don't want this feature (analagoud to not having any PARTITION BY), it's easy to modify the query.
    You could also get these results using a recursive WITH clause. That meets the criteria of avoiding analytic functions and Oracle-specific features, but not the one about using only plain, simple SQL features.

Maybe you are looking for

  • Two Apple IDs on iPhone, can't accept password

    I changed my Apple ID and everything appeared to be fine, but now iPhone keeps asking for old ID password? forgot password won't work.

  • Smartforms output in HTML format

    Hi Experts, I want to save my smartforms in HTML format.Is there any Standard programe is available for it. Like as RSTXPDFT4 for converting output in PDF format.

  • Help! CS3 has stopped working

    I get an error message from windows vista premiumm home. It just says: Adobe Photoshop CS3 has stopped working correctly. Windows will close the program and notify you if a solution is available. The only option is a box that says "close program". On

  • Help - save smart folder window disappearing

    Dear friends I have a really big problem with saving smart folders : i do all the steps until I press "save" - at that point the save widow only appears for 1 second and than disappears so I am unable to specify the location where I want to same the

  • Guest Access Account Lifetime

    Hello,all. I would like to ask about Guest Access account lifetime on Prime Infrastructure. As my customer said, When you create Guest account on one by one ,you can set the account lifetime for 364 days. however, when you create it by using CSV file