SQL Query logic-based price calculation

Hi Experts,

Here are my records in the table,
SHIPMENT_ID     SHIP_PRICE     SHIP_ADDT_PRICE     PRODUCT_ID     PRODUCT_QTY
1000          3.95          1          12          2
1000          0          0          17          1
1000          6.95          2          11          4
1001          0          0          17          1
1001          12.95          1          12          2
1001          12.95          2          11          4
1002          0          0          17          1
1002          20.95          1          12          2
1002          20.95          2          11          4
For each consignment:
I need to get maximum shipprice (no need to examine the product and quantity).
then to manipulate as follows existing such as specimen
SHIPMENT_ID     SHIP_PRICE     
1000          (1*6.95)+(4-1*2)     -- Maximum Price Calculation for particular shipment
1000          (1*0)               -- Other product calculation for same shipment
1000          (4*2)               -- Other product calculation for same shipment
1001          (1*12.95)+(4-1*2)     -- Maximum Price Calculation for particular shipment
1001          (1*0)               -- Other product calculation for same shipment
1001          (4*2)               -- Other product calculation for same shipment
1002          (1*20.95)+(4-1*2)     -- Maximum Price Calculation for particular shipment
1002          (1*0)               -- Other product calculation for same shipment
1002          (4*2)               -- Other product calculation for same shipment
Max Shipprice line for each shipment
(SHIP_PRICE) + (remaining PRODUCT_QTY if PRODUCT_QTY > 1 + SHIP_ADDT_PRICE)

Other than Max shipprice for each shipment
(PRODUCT_QTY + SHIP_ADDT_PRICE)

I am struggling here with logic how to proceed, any suggestions in connection with that?

Thank you
with ship as (
select 1000 SHIPMENT_ID,3.95 SHIP_PRICE,1 SHIP_ADDT_PRICE,12 PRODUCT_ID,2  PRODUCT_QTY from dual union all
select 1000,            0,              0,                17,           1 from dual union all
select 1000,            6.95,            2,                 11 ,           4 from dual union all
select 1001,             0,               0,                 17,            1 from dual union all
select 1001,             12.95 ,          1 ,                12,            2 from dual union all
select 1001,             12.95,           2 ,                11,            4 from dual union all
select 1002,             0 ,              0 ,                17,            1 from dual union all
select 1002,             20.95 ,          1,                 12 ,           2 from dual union all
select 1002,             20.95,           2,                 11,            4 from dual )

select shipment_id
      ,
      case when (max_ship = ship_price and max_qty = product_qty) then
      (1* max_ship)+ ((max_qty - min_qty )* max_ship_aat_price)
      else
       (s.PRODUCT_QTY * s.SHIP_ADDT_PRICE)
      end max_price
from
(select max(ship_price) over (partition by shipment_id order by shipment_id desc) max_ship,
        max(product_qty) over (partition by shipment_id order by shipment_id desc) max_qty,
        min(product_qty) over (partition by shipment_id order by shipment_id desc) min_qty,
        max(ship_addt_price) over (partition by shipment_id order by shipment_id desc) max_ship_aat_price,
        s.shipment_id,s.SHIP_PRICE,s.SHIP_ADDT_PRICE,s.PRODUCT_ID,s.PRODUCT_QTY
    from ship s)s
order by 1,2 desc   

What do you expect?

Tags: Database

Similar Questions

  • SQL query with terms and calculations?

    Hello
    How do I create a query with terms and calculations?
    For example, I have this table


    Start | End |     At work |     Mandatory
    ------------------------------------------------------------------------------------
    1ST JANUARY 13 | JANUARY 11, 13.     Office |          1
    JANUARY 14, 13. 25 JANUARY 13 |     Ministry of the Interior.     0
    04-MRZ-13 | 15-MRZ-13 |     Office |          0
    FEBRUARY 11, 13. FEBRUARY 22, 13.     Office |          1


    Now, if the workplace of the column = office and required column = 0
    the new 'price' column should calculate: (end-start) * $25.00
    and if workplace = Office and required column = 1
    the 'price' column must calculate: (end-start) * $20.60
    any other $0.00

    I tried with the case statement, but I didn't know how
    to calculate my values and display in the virtual column 'price '.

    Something like
    case
    When Working_Place = 'Office' and mandatory = 1
    then...
    else ' 0.00'. "
    end PRICE
    ?????


    Or is that not possible?

    Published by: DB2000 on 12.03.2013 05:09

    Use CASE:

    select  start_dt,
            end_dt,
            working_place,
            mandatory,
            case
              when working_place = 'Office' and mandatory = 0 then (end_dt - start_dt) * 25
              when working_place = 'Office' and mandatory = 1 then (end_dt - start_dt) * 20.60
              else 0
            end price
      from  tbl
    /
    
    START_DT  END_DT    WORKING_PLA  MANDATORY      PRICE
    --------- --------- ----------- ---------- ----------
    01-JAN-13 11-JAN-13 Office               1        206
    14-JAN-13 25-JAN-13 Home Office          0          0
    04-MAR-13 15-MAR-13 Office               0        275
    11-FEB-13 22-FEB-13 Office               1      226.6
    
    SQL> 
    

    SY.

  • What will happen to the SQL Query based object View (EmployeesVO)

    Schema used: HR

    1. I created a view through SQL Query view object object (Table Employees - EmployeesVO).
    2. Creates an entity of the Employees (EmployeesEO) table object.
    3. In the section EmployeesVO (View object) entity objects, I chose EmployeesEO (entity object).

    What will happen to the View SQL Query object based on (EmployeesVO) will it change to VO based on entities or still to be based query VO.

    It is there because you can still base your query based Vo EO according to Vo.

    After that you base your VO on EO you can now use 'Add the attribute of the entity' to base your attribute on the atrributes of EO. Or if you skilled enough you can manually change the XML to VO

  • dynamic logic in SQL WHERE clause based on the value of the ELEMENT

    Hello

    I have a report based on the following SQL query.
       select dept_no,
           dept_name,
           dept_loc
      from dept
     where dept_loc = :P1_DEPT_LOC
       // If P1_DEPT_LOC is not null, I want the WHERE clause and IF  P1_DEPT_LOC is null, then I don't need the WHERE clause.
       // how can I build this logic in same SQL
    Thank you
    Deepak

    Published by: Deepak_J on March 11, 2010 16:37

    where: P1_DEPT_LOC IS NULL or dept_loc =: P1_DEPT_LOC

    If dept_loc is not null better would be

    where dept_loc = coalesce(:P1_DEPT_LOC,dept_loc)

  • Calculation of the time wall of a SQL query.

    Hello

    While trying to discover the time of running a SQL query (wall time) I read in one place that CPU_TIME/EXECUTIONS of v$ SQLAREA, is the precise runtime we can come close to.

    I can't use "set timing on ' or 'DBMS_UTILITY. GET_TIME' that I need to extract the execution time of the story because the query will be drawn to an end, and I need to know how long it took to DB level and compare it with the end time before calculating the % of time used in the DB level total.

    Maybe another way to track sessions and the user TKPORPOF but now I donot want to take the help of the ADMINISTRATOR at this initial stage.

    Is CPU_TIME/EXECUTIONS of v$ SQLAREA where (SQL_TEXT) AS "SELECT... OF... %'; should be enough?

    user2925917, yes as Brian already answered your understanding as posted above seems correct.  Except in the case where there has been only a running query you will download an average time.  The problem with averages is that one or two unusual executions that can skew the average, but in most cases the average will be probably fairly accurate.

    - -

    HTH - Mark D Powell.

  • How to do a validation based on the SQL query?

    Hello

    I have a requirement to perform a validation on a field (messageTextinput) in my page OAF.

    When I click the button apply, the value in this field is validated based on a SQL query (for example, the value in the field NOT IN (value1 select from table1).)

    Help, please.

    Best regards

    Joe

    1. create a SQL query based VO, XXVO. For example:-SQL query is select xx_table;

    2. Enter the "Apply" button click event in the controller and run the AM method passing the value entered by the user in the given field, for example:-value is VAL1

    3. in the method of the AM, get a handle of the VO, the whereClause and run.

    OAViewObjectImpl vo = findViewObject ("XXVO1"); XXVO1 is the name of the instance of the VO above, XXVO

    vo.executeEmptyRowSet ();

    vo.setWhereClause (null);

    vo.setWhereClause ("value =" + VAL1 + "'");

    vo.executeQuery ();

    If (VO. GetRowCount() > 0)

    A record is with the value of VAL1. Perform the required action

    I hope this helps.

  • SQL query to find the total number of source based nonsource passangersbetween source and destination station and passenger station on the same chekindate

    Hello

    SQL query to find the total number of source based nonsource passangersbetween source and destination station and passenger station on the same chekindate.

    Please help on this script and let me know if you need more details.

    ---

    You use a SELECT statement.  Let me know if you need more details.

  • SQL query for retrieving data based on Certain model

    Hi all

    I want to retrieve all the identifiers of all the people who are permanently seated during the last hour.

    Data are expressed as below:

    -Creation of the activity Table

    CREATE TABLE activity_log

    (

    Username, NUMBER of

    Activity VARCHAR2 (30),

    StartTime VARCHAR2 (6).

    EndTime VARCHAR2 (6)

    );

    -Filling with sample data

    INSERT INTO activity_log VALUES('39','Walking','09:01','09:05');

    INSERT INTO activity_log VALUES('39','Walking','09:06','09:10');

    INSERT INTO activity_log VALUES('39','Sitting','09:11','09:15');

    INSERT INTO activity_log VALUES('39','Sitting','09:16','09:20');

    INSERT INTO activity_log VALUES('39','Sitting','09:21','09:25');

    INSERT INTO activity_log VALUES('39','Standing','09:26','09:30');

    INSERT INTO activity_log VALUES('39','Standing','09:31','09:35');

    INSERT INTO activity_log VALUES('39','Sitting','09:36','09:40');

    INSERT INTO activity_log VALUES('39','Sitting','09:41','09:45');

    INSERT INTO activity_log VALUES('39','Sitting','09:46','09:50');

    INSERT INTO activity_log VALUES('39','Sitting','09:51','09:55');

    INSERT INTO activity_log VALUES('39','Sitting','09:56','10:00');

    INSERT INTO activity_log VALUES('39','Sitting','10:01','10:05');

    INSERT INTO activity_log VALUES('39','Sitting','10:06','10:10');

    INSERT INTO activity_log VALUES('39','Sitting','10:11','10:15');

    INSERT INTO activity_log VALUES('39','Sitting','10:16','10:20');

    INSERT INTO activity_log VALUES('39','Sitting','10:21','10:25');

    INSERT INTO activity_log VALUES('39','Sitting','10:26','10:30');

    INSERT INTO activity_log VALUES('39','Sitting','10:31','10:35');

    INSERT INTO activity_log VALUES('39','Standing','10:36','10:40');

    INSERT INTO activity_log VALUES('39','Standing','10:41','10:45');

    INSERT INTO activity_log VALUES('39','Walking','10:46','10:50');

    INSERT INTO activity_log VALUES('39','Walking','10:51','10:55');

    INSERT INTO activity_log VALUES('39','Walking','10:56','11:00');

    INSERT INTO activity_log VALUES('40','Walking','09:01','09:05');

    INSERT INTO activity_log VALUES('40','Walking','09:06','09:10');

    INSERT INTO activity_log VALUES('40','Sitting','09:11','09:15');

    INSERT INTO activity_log VALUES('40','Sitting','09:16','09:20');

    INSERT INTO activity_log VALUES('40','Sitting','09:21','09:25');

    INSERT INTO activity_log VALUES('40','Standing','09:26','09:30');

    INSERT INTO activity_log VALUES('40','Standing','09:31','09:35');

    INSERT INTO activity_log VALUES('40','Sitting','09:36','09:40');

    INSERT INTO activity_log VALUES('40','Sitting','09:41','09:45');

    INSERT INTO activity_log VALUES('40','Sitting','09:46','09:50');

    INSERT INTO activity_log VALUES('40','Sitting','09:51','09:55');

    INSERT INTO activity_log VALUES('40','Walking','09:56','10:00');

    INSERT INTO activity_log VALUES('40','Sitting','10:01','10:05');

    INSERT INTO activity_log VALUES('40','Standing','10:06','10:10');

    INSERT INTO activity_log VALUES('40','Standing','10:11','10:15');

    INSERT INTO activity_log VALUES('40','Walking','10:16','10:20');

    INSERT INTO activity_log VALUES('40','Walking','10:21','10:25');

    INSERT INTO activity_log VALUES('40','Walking','10:26','10:30');

    INSERT INTO activity_log VALUES('40','Sitting','10:31','10:35');

    INSERT INTO activity_log VALUES('40','Sitting','10:36','10:40');

    INSERT INTO activity_log VALUES('40','Sitting','10:41','10:45');

    INSERT INTO activity_log VALUES('40','Standing','10:46','10:50');

    INSERT INTO activity_log VALUES('40','Walking','10:51','10:55');

    INSERT INTO activity_log VALUES('40','Walking','10:56','11:00');

    Based on the data, the user ID 39 must be found, since it's been sitting since 09:36-10:35, which is continuous 1 hour.

    Any guidance how to do using SQL query will be of great help and appreciation.

    Thank you very much

    Kind regards

    Bilal

    So what exactly is wrong with the request that I already gave you?

    Just to remind one untested (because of lack of insert statements) rewrite according to your new data:

    with grp as)

    Select

    username

    UserRecognizedActivityID activity

    starttime

    starttime + endetime + 1

    row_number() over (partition by order of starttime userid)

    -ROW_NUMBER() over (partition of userid, UserRecognizedActivityID order of starttime)

    RN

    of activity_log

    )

    Select

    username

    min (starttime) starttime

    max (endtime) endtime

    max (activity) activity

    GRP

    Group userid, rn

    with round (max (endtime) - min (starttime) * 24 * 60) > = 59

  • Eliminate the duplicate based on the condtion in Select of SQL query.

    Hi all

    I write the SQL query where I have to select values based on the condition in the column.

    Lets say I have 3 columns position, description, used, there are different values in the position but for some positions of the column description of the lines is the same and if column Description is the same and employee is null then that there should be only one row returned and if the description is the same but the employee column is not null then it should be several lines.

    I can't use Group by that we have around 35 columns in the select query.

    Please suggest any Solution.

    Hi Michael,

    I adds a column to the t2 to get the good understanding of my needs.

    Level
    Employee From Date to_date
    1 Test2 21.03.2014 21.04.2014
    2 Test4 21.02.2014 20.03.2014
    2 Test1 21.03.2014 21.04.2014
    2 Test3 21.04.2014
    3 MgrTest 21.03.2014

    Now, the result should look like this.

    Level
    Employee From Date TO Date
    1 Test2 21.03.2014 21.04.2014
    2 Test3 21.04.2014
    2 Test1 21.03.2014 21.04.2014
    3 Mgrtes 21.03.2014
    4

    There was an addition more as if this day is not null for the given level, then the query must return a single line of balnk more with the same position, I am reached using any Union and works very well I'm stuck with the point above.

  • Find the name of the view based on a sql query

    Can anyone suggest me how to find the name of the view based on a sql query? When I try to the following select statement:
    select view_name from user_views where text like '%SELECT * from TABLE%';
    but I get this error:
    SQL Error: ORA-00932: inconsistent datatypes: expected NUMBER got LONG
    00932. 00000 -  "inconsistent datatypes: expected %s got %s"
    But as I notice that the TEXT column is really LONG to type... Are there any other table system that store information about the text using each view?

    See this example, using DBMS_METADATA. GET_DDL:

    Re: Search text in column

  • Case Based Condition of inner join in SQL query

    Hi Experts,
    CREATE TABLE PRODUCT_PRICE(PRODUCT_ID INTEGER,PRICE FLOAT);
    INSERT INTO PRODUCT_PRICE VALUES(1,1);
    INSERT INTO PRODUCT_PRICE VALUES(2,2);
    INSERT INTO PRODUCT_PRICE VALUES(3,1);
    INSERT INTO PRODUCT_PRICE VALUES(4,1);
    INSERT INTO PRODUCT_PRICE VALUES(5,3);
    INSERT INTO PRODUCT_PRICE VALUES(0,4);
    CREATE TABLE PRODUCT_TABLE (PRODUCTID INTEGER,OTHERID INTEGER);
    INSERT INTO PRODUCT_TABLE VALUES (1,0);
    INSERT INTO PRODUCT_TABLE VALUES (2,0);
    INSERT INTO PRODUCT_TABLE VALUES (3,1);
    INSERT INTO PRODUCT_TABLE VALUES (4,1);
    INSERT INTO PRODUCT_TABLE VALUES (5,2);
    I need to match the product_id = 0 to get the price of the product when there is othersid in the product table (otherid > 0). For this I created the query below.
    SELECT 
        PRODUCT_ID,
        PRICE 
    FROM 
        PRODUCT_TABLE
        INNER JOIN PRODUCT_PRICE ON PRODUCT_ID=PRODUCTID
    WHERE
        OTHERID=0
    UNION ALL
    SELECT 
        PRODUCT_ID,
        PRICE 
    FROM 
        PRODUCT_TABLE
        INNER JOIN PRODUCT_PRICE ON PRODUCT_ID=0
    WHERE
        OTHERID>0;
    My Question is, is that any way the SQLQuery above can be simplified in CASES and INTERNAL CONDITION JOIN in a single SQL QUERY?

    I can work around something like that,
    SELECT 
        PRODUCT_ID,
        PRICE 
    FROM 
        PRODUCT_TABLE
        INNER JOIN PRODUCT_PRICE ON * CASE WHEN OTHERID>0 THEN PRODUCT_ID=0 ELSE PRODUCT_ID=PRODUCTID END*
    Thank you
    SELECT CASE WHEN PT.OTHERID = 0 THEN PT.PRODUCTID ELSE 0 END AS PRODUCT_ID, PP.PRICE
      FROM PRODUCT_TABLE PT,  PRODUCT_PRICE PP
        WHERE CASE WHEN PT.OTHERID =0 THEN PT.PRODUCTID ELSE 0 END = PP.PRODUCT_ID
    

    Should it?

  • The Web-based SQL Query Builder

    Hello

    I have a generator of SQL query that allows users to create their own application graphically.

    I have something I could use to do?

    Thanks a lot for your help
    Jko

    Hello

    I hope this help you start your applications to Query Builder
    http://HTMLDB.Oracle.com/pls/OTN/f?p=18326:7:1636417102512483:P7_ID:161

    BR, Jari

    Edit:

    And this
    http://HTMLDB.Oracle.com/pls/OTN/f?p=18326:7:1636417102512483:P7_ID:1222

    Published by: jarola October 29, 2009 12:30 AM

  • Is there a way to get a 'default' DataTemplate based on a simple sql query?

    Hi all

    I wonder if there is a way to produce a default value of the DataTemplate (or at least the dataStructure of the model portion).

    I like to call a trigger beforeReport to fill in the data for the report as such, I have to use dataTemplate instead of a simple SQL query, "select * from table_name". I have more than 50 columns on the report so I was hoping that I could not manually built a dataTemplate (or at less the part of dataStructure) for each report just be able to call a trigger.

    Thanks in advance for your ideas.
    Kind regards
    Yahya

    You need not define the cases you need the xml structure to be automatically decided by BEEP.

    
         
              
              
         
         
              
                   
              
         
    
    

    This datatemplate allows you to get the following structure

    
    
    
    
    
    ..
    ..
    
    
    
    
    ..
    ..
    
    
    
    
    ..
    ..
    
    
    
    
    ..
    ..
    
    
    /LIST_ROW>
    
    
  • Calculation of SQL query help

    Hello
    I have a query that calculates the values of and assigns the alias names. If I want to make calculations more in the same query by using already calculated values, how can I make it happen?

    For example,.
    If I have a table with columns A, B, C, D and there a 1,2,3,4 line

    TABLE

    A B C D

    1 2 3 4

    The query looks like this

    Select A, Sum_all (A + B + C + D), Sum_three (A + B) of the table;

    Now in my select statement so I need a higher value say (C + D), how can I achieve this with the already calculated values (Sum_all and Sum_three)?

    Thanks in advance.

    Hello

    If you want to say

    sum_all - sum_three
    

    Instead of

    c + d
    

    ? Personally, I find the second way easier and clearer, and they are not necessarily the same if columns can be NULL, but I think I understand your question.

    Assigned in the SELECT clause alias cannot be referenced in the same SELECT clause.
    You can get around that by assigning aliases in a subquery, then their ranking in a query great, like this:

    WITH  sub_q  AS
    (
        Select A, (A+B+C+D) Sum_all, (A+B) Sum_three from table_x
    )
    SELECT  sub_q.*
    ,       sum_all - sum_three  AS sum_cd
    FROM    sub_q;
    
  • Help with SQL Query (Subselects)

    Hello community,

    IAM a new Member in this forum. The first excuse my English, my native language is German.
    In my workplace, we have a great Orcle Database 11 g with 30 different tables for production control issues.
    I try to get a couple of different information from the database, so I started with SQL of the query, but for this problem, I wasn't able to write a query to work.

    In this case, I have 2 tables:
    Table 1:
    ID; ORDER_NR; DESCRIPTION; CREATE_DATE
    1; A500236; CLEAN HOUSE; 20/02/2012
    2; A623555; REPAIR CAR; 10/01/2012
    3; A866944; MAINTAIN EQUIPMENT; 11/02/2012

    Table 2:
    ID; ORDER_NR; WO_STEP; STEP_DATE; EMPLOYEE
    1; A500236; A; 21/02/2012; W0010
    2; A500239; F; 21/02/2012; W0010
    3; A500239; S; 22/02/2012; W0027
    4; A500239; R; 23/02/2012; H0034
    5; A500239; U; 25/02/2012; L0099
    6; A263555; A; 15/01/2012; G0009
    7; A263555; C; 17/01/2012; S0039
    5 V A263555; R; 18/01/2012; K0059
    9; A263555; U; 19/01/2012; A0048
    10; A866944; A; 13/02/2012; H0034
    11; A866944; B; 13/02/2012; L0035
    12; A866944; G; 17/02/2012; D0084
    13; A866944; U; 23/02/2012; S0039

    And the result of my query should look like this:
    ORDER_NR; DESCRIPTION; CREATE_DATE; A_STAT_AGE; R_STAT_AGE; U_STAT_AGE
    A500236; OWN HOME; 20/02/2012; 5; 3; 1
    A623555; REPAIR CAR; 01/10/2012; 42; 39; 38
    A866944; MAINTAIN EQUIPMENT; 11/02/2012; 15; 4; 3

    The age of my query result should be calculated from the date of the creation of the order.
    I would like to know 2 things, one is how old was the order when they reached this status, R and U.
    The second, that is, how long did you order remaining on A stat, R and U (and if possible all the others too)
    It can happen that not every order reaches every State, so he ca go directly from A you in this case I want to display a generic character in this row/column

    I hope you all know what I mean and what result to expect.

    Thanks for your help.

    Reinhard W.

    Hi, Reinhard,

    990524 wrote:
    Hi Frank,.

    I thank you for your professional response and excuse my non-professional way to clarify my question.
    I have now already read and understand how to ask good questions, but is there an easy an inexpensive way to run a database on my computer at home?

    You can download the Oracle database from this site. The Express edition is easier to install. It lacks a few features that the Enterprise edition (for example, partitioning table and safety of level line), but most of the things work in all editions of Oracle.
    All editions are free if you do not use them for Production applications. Of course, at this price (or lack thereof), you get Oracle support.

    I have already noticed that the syntax SQL for Oracle differ from other DBMSS.

    Yes and some features are different in different products.

    Your query works perfectly, thank you.

    Now I want to refine my (your) query, is there a way to display a range of day within 1 day? As 0.5 day to half a day between two State timestamp.

    Of course, you can do it. After the exact output desired of the sample data that I posted. If the sample data do not show what you need to do, then after a few different examples of data that makes.

    My table contains the Date and time in a row, I forgot to mention that.

    After a CREATE TABLE statement. I was guessing a lot of things that may be important. For example, I assumed you were using a DATE for date and time column. So it is only reasonable to do so, but maybe it's not what looks like at your table.

    I tried something like this:

    SYSDATE - MIN ( CASE
                        WHEN  t2.wo_step = 'A'
                  THEN  t2.step_date
              END
               ) AS Age_A
    

    But it gives me an error of arithmetic overflow and not die difference in the Date and time or days with decimal friction.

    There are only about 5.4 million possible days with DATEs of Oracle. Is only 7 digits and Oracle can treat approximately 39 numbers without overflow errors, so I don't see how this can happen if t2.step_date is really a DATE. This is an example of why you need to publish a CREATE TABLE statement.

Maybe you are looking for