Skip to Content

hana sql script help

There is a table called as OFFER

OFFER NAME

A

B

C

D

E

Now I have another transaction table called as DISCOUNTED SALES

REGION

OFFER NAME

SALES

ASIA PACIFIC

A

3450

ASIA PACIFIC

B

2560

NORTH AMERICA

E

3457

NORTH AMERICA

D

2500

There is a underlying logic needs to be maintained which has a preference on offer types .

The preference order is E,B,A,D.

The logic should be formed in such a manner that per region only most prioritized record is retrieved on output.

The output should be

REGION

OFFER NAME

SALES

ASIA PACIFIC

B

2560

NORTH AMERICA

E

3457

Thanks

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on Sep 06, 2016 at 07:49 PM

    Hello Tanush,

    somewhere the "preference order" information has to be available to be able to evaluate it in a statement. Either store it in a table (directly in the OFFER table or in a related table) or integrate it in the statement if it is fix.

    In the following example I mixed the "preference order" into the result using a CASE statement. This CASE statement inserts integer values which allows to sort the result (e.g. E -> 1, B -> 2, A -> 3, D -> 4). With the rank window function the rank is created per region and finally only the records with rank 1 are considered.

    Please replace the schema/table/column names with your values.

    select region, offer_name, sales
    from
    (
      select t1.region, t1.offer_name, t2.offer_name_prio, t1.sales, rank() over (partition by t1.region order by t2.offer_name_prio) as rank
      from "MISC"."DISCOUNTED_SALES" as t1
        inner join
        ( select offer_name,
             case offer_name
               when 'E' then 1
               when 'B' then 2
               when 'A' then 3
               when 'D' then 4
             end as offer_name_prio
          from "MISC"."OFFER") as t2
        on t1.offer_name = t2.offer_name
    )
    where rank = 1;
    
    

    That produces the following result for your example data.

    Regards,

    Florian


    tmp01.JPG (14.1 kB)
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.