How to calculate this average cost ?

Hello,
I've been thinking on this for problem for a while, but could not come up with a solution. In excel it is easy to do, but in SQL ?
Tried with analytics but no success.
I need to recalculate the AVG_PRICE when QUANTITY > 0, ie when I buy, else AVG_PRICE = prior AVG_PRICE.
these are the rules:
QTY_ON_HAND is a running total of QUANTITY
TOTAL_COST = AVG_PRICE * QTY_ON_HAND
AVG_PRICE = ( TOTAL_COST (preceding row) + TRADE_AMOUNT (current row) ) / QTY_ON_HAND
Group is FUND_CODE, INSTRUMENT_TYPE, INSTRUMENT_CODE
Order is by TRADE_DATE
the numbers I need are on the last row of the .CSV file
AVG_PRICE = 797.0237644
QTY_ON_HAND = 1'327.137600
TOTAL_COST = 1'057'760.21
Any help is more than welcome
thank you
Claudio
this is the data:
CREATE TABLE t (
"FUND_CODE" NVARCHAR2(3),
"INSTRUMENT_TYPE" NVARCHAR2(4),
"INSTRUMENT_CODE" NVARCHAR2(15),
"TRADE_DATE" TIMESTAMP,
"QUANTITY" NUMBER,
"TRADE_AMOUNT" NUMBER
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('01-10-2007 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 115.2502, -92411.183116);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('05-12-2007 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 318.4801, -336101.285453);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('11-01-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), -64.6781, 67647.083502);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('25-01-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 72.5117, -109197.254334);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('01-02-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 87.1223, -123386.870253);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('29-02-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 110.869, -93957.707954);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('06-03-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), -55.2563, 49721.994761);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('12-03-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 164.4146, -135834.245667);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('18-03-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), -37.6276, 41342.685831);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('27-03-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 159.1288, -112293.21594);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('01-05-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 64.6638, -46458.289084);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('23-05-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 137.3465, -85979.32104);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('04-06-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 156.0562, -86183.26);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('01-07-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), -342.5453, 265491.46);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('11-08-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), -175.8868, 170185.92);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('26-08-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 143.9074, -124927.73);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('28-10-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), -321.3085, 472031.39);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('03-11-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), -85.2822, 73202.14);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('01-12-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 38.1563, -27117.03);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('02-12-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 168.331, -135004.83);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('04-12-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 606.1362, -461853.36);
INSERT INTO t ("FUND_CODE", "INSTRUMENT_TYPE", "INSTRUMENT_CODE", "TRADE_DATE", "QUANTITY", "TRADE_AMOUNT")
VALUES ('CLF', 'ifus', 'CLCX', TO_DATE('10-12-2008 12:00:00 AM','DD-MM-YYYY HH:MI:SS AM'), 67.3483, -47610.26);
COMMIT;
this is the result in .CSV format, so that you can "see it" in a spreadsheet
FUND_CODE;INSTRUMENT_TYPE;INSTRUMENT_CODE;TRADE_DATE;QUANTITY;TRADE_AMOUNT;AVG_PRICE;QTY_ON_HAND;TOTAL_COST
CLF;ifus;CLCX;01.10.07;115.250200 ;-92411.18312;801.831;115.250200 ;92'411.18
CLF;ifus;CLCX;05.12.07;318.480100 ;-336101.2855;987.9698711;433.730300 ;428'512.47
CLF;ifus;CLCX;11.01.08;-64.678100 ;67647.0835;987.9698711;369.052200 ;364'612.45
CLF;ifus;CLCX;25.01.08;72.511700 ;-109197.2543;1073.02637;441.563900 ;473'809.71
CLF;ifus;CLCX;01.02.08;87.122300 ;-123386.8703;1129.586093;528.686200 ;597'196.58
CLF;ifus;CLCX;29.02.08;110.869000 ;-93957.70795;1080.679646;639.555200 ;691'154.29
CLF;ifus;CLCX;06.03.08;-55.256300 ;49721.99476;1080.679646;584.298900 ;631'439.93
CLF;ifus;CLCX;12.03.08;164.414600 ;-135834.2457;1024.790089;748.713500 ;767'274.17
CLF;ifus;CLCX;18.03.08;-37.627600 ;41342.68583;1024.790089;711.085900 ;728'713.78
CLF;ifus;CLCX;27.03.08;159.128800 ;-112293.2159;966.4362121;870.214700 ;841'007.00
CLF;ifus;CLCX;01.05.08;64.663800 ;-46458.28908;949.2840914;934.878500 ;887'465.29
CLF;ifus;CLCX;23.05.08;137.346500 ;-85979.32104;907.8734486;1'072.225000 ;973'444.61
CLF;ifus;CLCX;04.06.08;156.056200 ;-86183.26;862.6915958;1'228.281200 ;1'059'627.87
CLF;ifus;CLCX;01.07.08;-342.545300 ;265491.46;862.6915958;885.735900 ;764'116.92
CLF;ifus;CLCX;11.08.08;-175.886800 ;170185.92;862.6915958;709.849100 ;612'380.85
CLF;ifus;CLCX;26.08.08;143.907400 ;-124927.73;863.605235;853.756500 ;737'308.58;
CLF;ifus;CLCX;28.10.08;-321.308500 ;472031.39;863.605235;532.448000 ;459'824.88;
CLF;ifus;CLCX;03.11.08;-85.282200 ;73202.14;863.605235;447.165800 ;386'174.73;
CLF;ifus;CLCX;01.12.08;38.156300 ;-27117.03;851.582394;485.322100 ;413'291.76;
CLF;ifus;CLCX;02.12.08;168.331000 ;-135004.83;838.8189176;653.653100 ;548'296.59;
CLF;ifus;CLCX;04.12.08;606.136200 ;-461853.36;801.8403917;1'259.789300 ;1'010'149.95;
CLF;ifus;CLCX;10.12.08;67.348300 ;-47610.26;797.0237644;1'327.137600 ;1'057'760.21;

this is how the numbers have to be:
fund    instr   instr   trade_date       quantity        trade_amount    avg_price     qty_on_hand        total_cost
code    type    code   
CLF     ifus     CLCX     01.10.2007      115.2502      -92'411.1831       801.8310       115.2502        92'411.18
CLF     ifus     CLCX     05.12.2007      318.4801     -336'101.2855       987.9699       433.7303       428'512.47
CLF     ifus     CLCX     11.01.2008      -64.6781       67'647.0835       987.9699       369.0522       364'612.45
CLF     ifus     CLCX     25.01.2008       72.5117     -109'197.2543     1'073.0264       441.5639       473'809.71
CLF     ifus     CLCX     01.02.2008       87.1223     -123'386.8703     1'129.5861       528.6862       597'196.58
CLF     ifus     CLCX     29.02.2008       110.869      -93'957.7080     1'080.6796       639.5552       691'154.29
CLF     ifus     CLCX     06.03.2008      -55.2563       49'721.9948     1'080.6796       584.2989       631'439.93
CLF     ifus     CLCX     12.03.2008      164.4146     -135'834.2457     1'024.7901       748.7135       767'274.17
CLF     ifus     CLCX     18.03.2008      -37.6276       41'342.6858     1'024.7901       711.0859       728'713.78
CLF     ifus     CLCX     27.03.2008      159.1288     -112'293.2159       966.4362       870.2147       841'007.00
CLF     ifus     CLCX     01.05.2008       64.6638      -46'458.2891       949.2841       934.8785       887'465.29
CLF     ifus     CLCX     23.05.2008      137.3465      -85'979.3210       907.8734     1'072.2250       973'444.61
CLF     ifus     CLCX     04.06.2008      156.0562      -86'183.2600       862.6916     1'228.2812     1'059'627.87
CLF     ifus     CLCX     01.07.2008     -342.5453      265'491.4600       862.6916       885.7359       764'116.92
CLF     ifus     CLCX     11.08.2008     -175.8868      170'185.9200       862.6916       709.8491       612'380.85
CLF     ifus     CLCX     26.08.2008      143.9074     -124'927.7300       863.6052       853.7565       737'308.58
CLF     ifus     CLCX     28.10.2008     -321.3085      472'031.3900       863.6052       532.4480       459'824.88
CLF     ifus     CLCX     03.11.2008      -85.2822       73'202.1400       863.6052       447.1658       386'174.73
CLF     ifus     CLCX     01.12.2008       38.1563      -27'117.0300       851.5824       485.3221       413'291.76
CLF     ifus     CLCX     02.12.2008       168.331     -135'004.8300       838.8189       653.6531       548'296.59
CLF     ifus     CLCX     04.12.2008      606.1362     -461'853.3600       801.8404     1'259.7893     1'010'149.95
CLF     ifus     CLCX     10.12.2008       67.3483      -47'610.2600       797.0238     1'327.1376     1'057'760.21As you can see on the 3rd line we have a Sell -64.67, thus we take the previous AVG_PRICE.
Keep in mind that:
AVG_PRICE = (TOTAL_COST preceding row + TRADE_AMOUNT current row) / QTY_ON_HAND is calculated only on Buy QUANTITY > 0
TOTAL_COST = QTY_ON_HAND * AVG_PRICE
this is the output of the query kindly suggested by Alex:
FUND_CODE INSTRUMENT_TYPE INSTRUMENT_CODE                TRADE_DATE                     QUANTITY         TRADE_AMOUNT          QTY_ON_HAND           TOTAL_COST            AVG_PRICE
CLF       ifus            CLCX                           01.10.2007 00:00               115.2502        -92411.183116             115.2502        -92411.183116    -801.830999998265
CLF       ifus            CLCX                           05.12.2007 00:00               318.4801       -336101.285453             433.7303       -428512.468569    -987.969871067343
CLF       ifus            CLCX                           11.01.2008 00:00               -64.6781         67647.083502             369.0522       -360865.385067    -987.969871067343
CLF       ifus            CLCX                           25.01.2008 00:00                72.5117       -109197.254334             441.5639       -470062.639401    -1064.54046492705
CLF       ifus            CLCX                           01.02.2008 00:00                87.1223       -123386.870253             528.6862       -593449.509654     -1122.4985816804
CLF       ifus            CLCX                           29.02.2008 00:00                110.869        -93957.707954             639.5552       -687407.217608    -1074.82077795318
CLF       ifus            CLCX                           06.03.2008 00:00               -55.2563         49721.994761             584.2989       -637685.222847    -1074.82077795318
CLF       ifus            CLCX                           12.03.2008 00:00               164.4146       -135834.245667             748.7135       -773519.468514    -1033.13145617649
CLF       ifus            CLCX                           18.03.2008 00:00               -37.6276         41342.685831             711.0859       -732176.782683    -1033.13145617649
CLF       ifus            CLCX                           27.03.2008 00:00               159.1288        -112293.21594             870.2147       -844469.998623    -970.415690085447
CLF       ifus            CLCX                           01.05.2008 00:00                64.6638        -46458.289084             934.8785       -890928.287707      -952.9883163502
CLF       ifus            CLCX                           23.05.2008 00:00               137.3465         -85979.32104             1072.225       -976907.608747    -911.103181465644
CLF       ifus            CLCX                           04.06.2008 00:00               156.0562            -86183.26            1228.2812      -1063090.868747    -865.510982946739
CLF       ifus            CLCX                           01.07.2008 00:00              -342.5453            265491.46             885.7359       -797599.408747    -865.510982946739
CLF       ifus            CLCX                           11.08.2008 00:00              -175.8868            170185.92             709.8491       -627413.488747    -900.493486542659
CLF       ifus            CLCX                           26.08.2008 00:00               143.9074           -124927.73             853.7565       -752341.218747    -881.212873632002
CLF       ifus            CLCX                           28.10.2008 00:00              -321.3085            472031.39              532.448       -280309.828747    -881.212873632002
CLF       ifus            CLCX                           03.11.2008 00:00               -85.2822             73202.14             447.1658       -207107.688747    -526.454843941568
CLF       ifus            CLCX                           01.12.2008 00:00                38.1563            -27117.03             485.3221       -234224.718747    -482.617047002393
CLF       ifus            CLCX                           02.12.2008 00:00                168.331           -135004.83             653.6531       -369229.548747    -564.870798818211
CLF       ifus            CLCX                           04.12.2008 00:00               606.1362           -461853.36            1259.7893       -831082.908747    -659.699926604393
CLF       ifus            CLCX                           10.12.2008 00:00                67.3483            -47610.26            1327.1376       -878693.168747    -662.096506607152 As I said I'll try to elaborate on the idea of Alex, does not mean that I don't need any other help.
ON THE CONTRARY !
Any help is more than welcome.
Sorry again if I am not clear in my posting, this is my first time, have patience please.
thanks again
Claudio

Similar Messages

  • How to calculate the average inventory in ABAP

    Dear All,
    Please find the below formula and this formula how to calculate the Average inventory at value.Please let me know the abap base tables and the corresponding fields.
    Formula
    Inventory Turnover = Cost of Goods Sold (COGS) / Average Inventory at value.
    Thanks
    Regards,
    Sai

    Hi Arivazhagan,
    Thanks for your quick response .
    The field MBEWH from the table is fulfill the average inventory at value.
    For Eg :I want to calculate Inventory Turnover = Cost of Goods Sold (COGS)/
    Average Inventory at value.
    so shall i take Inventory Turnover = Cost of Goods Sold (COGS)/MBEWH
    The above formula will meet my requirement to find the average inventory Turnover.
    Thanks
    Regards,
    Sai

  • How to calculate moving average price?

    hi,
    i need someone to explain to me how to calculate moving average price

    Hi,
    Follow the Link,
    Re: moving average price

  • How to calculate moving average price(MAP) when price variance in IV

    Hi All,
    My current stock for a material is 39 and MAP is 0.8 GBP.
    A purchase order is created for the material for a quantity of 1 at price of 0.8 GBP per unit.
    Invoice is posted for the above PO at price of 1.74 GBP.
    What will be the new MAP for the material now??How to calculate MAP when there is a price variance in Invoice?
    Please suggest ASAP.

    Hi,
    The Procedure for calculating MAP is :-
    Case 1 :- PO at different price
    If in Material Master your MAP is 0.8GBP & closing stock of 30Nos, value is 24GBP
    In PO if the price is 0.9GBP & qty is 10nos, then after Goods Receipt Total Value of the GR i.e. Qty * Price + any other delivery cost will be added to the material stock value.  Qty is also updated.  New Total Value / New Stock Qty will be MAP.
    In this case Qty received 10 & price is 0.9 that means value would be 9GBP.
    Total Value becomes 24GBP (Present Stock) + 9GBP (Goods Receipt) = 33GBP
    Total Qty becomes 30Nos + 10Nos (Goods Receipt) = 40Nos
    MAP becomes = 33GBP / 44Nos = 0.75
    Case 2 : Invoice Posting (MIRO) at different price than PO
    While doing MIRO is we post at different price system will check whether sufficient stock posted in GR is available.  In the above case it is 10Nos,
    If it is there, then the above logic as in case 1 will take place
    If not available, to the extent of stock availability system will post the different value & adjust MAP & remaining is posted to Price Difference Account.
    In your case it would be like this :
    1. MAP 0.8 for 30Nos (Value would be 24GBP)
    2. Invoice Posting 1.74GBP for 1No.  That means value would be 24GBP + 1.74GBP = 25.74GBP
         Qty would be 30+1 = 40
         MAP = 25.74 / 40   = 0.64GBP
    Hope this clarifies your doubt
    regards,
    JP

  • Help..! ..how to calculate the average interest rate in discoverer?

    help..!
    I want to calculate average interest rate in discoverer, question describes below:
    1. the data have 4 columns, 1 account number, 2 balance, 3.interest rate, 4. product type 1, product type 2
    2.average interest rate =
    sum (product type1 account bal * rate / sum(product bal)).
    a*sum(product bal) is sum every records column 2 according the type of product
    b*every record need to calculate using balance to divide sum(product bal) for calculate the rate of the sum(product bal) of type of product
    c* sum(b*) is what my answer rate.
    3.
    I need to display average interest rate in discoverer, and each product have a one average rate according user drill-up or drill-down product dimension.
    *product dimension have two level
    please tell me how to let every record divide by group sum(product bal)
    or tell me how to solve this problem.
    thanks!
    david

    Hi David
    Try using the AVG analytic function.
    Best wishes
    Michael

  • How to calculate an average of YTD measures?

    Hi all,
    I'm working on BPC 10 for NW.
    We use an account-type dimension for Personnel inventory (Personnel to fixed-term contracts, to permanent contracts, number of executives, staff etc.).
    The corresponding ACCTYPE is AST because people are seen as a balance sheet account measure. So the measure, even if we're in periodic in our model, is stored as a YTD value.
    I want to calculate a average with those measures, for instance the average number of contractors from january to the current month, with a member formula. I can't manage to do that with a calculation because I can't add values of YTD measures: when I try a formula like ([ACCOUNT].A1 , [MEASURES].[YTD]) it returns the same value as the periodic value. Whereas the formula works with EXP account type.
    The formula I'd like to have in the end would be something like that:
    iif(isleaf([TIME].currentmember),avg(YTD(),([Measures].[PERIODIC],[ACCOUNT].[G102])),[TIME].currentmember.lastchild),solve_order=5
    This formula works in a previous version of BPC (5.1) but not in BPC 10.
    Any idea?
    Thanks very much,
    Sébastien.

    Assuming the job always starts at mid-might, you can try something like this:
    WITH t AS
    (SELECT to_date('01-JUL-2011 07:25:31', 'dd-mon-yyyy hh:mi:ss') AS end_date
    FROM dual
    UNION
    SELECT to_date('02-JUL-2011 07:10:58', 'dd-mon-yyyy hh:mi:ss') AS end_date
    FROM dual
    UNION
    SELECT to_date('01-AUG-2011 07:16:24', 'dd-mon-yyyy hh:mi:ss') AS end_date
    FROM dual)
    SELECT trunc(end_date, 'mon') AS MONTH,
            trunc(end_date, 'mon') + avg(end_date - TRUNC(end_date)) AS AVERAGE_TIME
    FROM t
    GROUP BY TRUNC(end_date, 'mon');

  • How to calculate this column (Last Week of month)?

    I have a request where it shows some calculations month wise
    ex. This report is for Dec-2010
    reporting line | Declined Rate | Approved Rate | Last-Week Declined Rate |
    Report Asia---| ------20%----- | -----45%-------- | --------4%------ |
    Report Africa-| ------44% -----| -----21%-------- | ------12%-------|
    I am trying to calculate the Last Week Declined Rate column. The logic is just not coming to my mind.
    Can anybody suggest me how should I calculate this?

    Let's see. Running this report anytime after the previous month has closed...
    1) TIMESTAMPADD(SQL_TSI_DAY, (DayOfMonth(CURRENT_DATE))*-1, CURRENT_DATE) will give you the last day of the previous month...
    2) TIMESTAMPADD(SQL_TSI_DAY,DAYOFWEEK(TIMESTAMPADD(SQL_TSI_DAY, (DayOfMonth(CURRENT_DATE))*-1, CURRENT_DATE))*-1,TIMESTAMPADD(SQL_TSI_DAY, (DayOfMonth(CURRENT_DATE))*-1, CURRENT_DATE)) will give you the Saturday prior to the last week of the month.
    3) TIMESTAMPADD(SQL_TSI_DAY,1,TIMESTAMPADD(SQL_TSI_DAY,DAYOFWEEK(TIMESTAMPADD(SQL_TSI_DAY, (DayOfMonth(CURRENT_DATE))*-1, CURRENT_DATE))*-1,TIMESTAMPADD(SQL_TSI_DAY, (DayOfMonth(CURRENT_DATE))*-1, CURRENT_DATE))) will give you the Sunday of the last week of the previous month.
    Now that you have the beginning of the last day of the previous week, you can build your filter... It will be "between 3) and 1)" above...

  • How do calculate the average of a group of percentages excluding percentages listed as zero on Numbers?

    Hey all
    This is driving me crazy!
    I need to calculate the average of a group of percentages excluding cells listed as '0%'.
    Please see screen print below, numbers highlighted need to be averaged, when the table is filled out correctly some will remain '0%' whilst others will have a value, I want the average to be calculated only using numbers with a value greater than '0'.
    FYI I am looking for the result to display in 'X2'
    Thanks :-)

    you can use the averageif() function. Here is a simple example to get you started:
    In this example the average if() function averages values that are non-zero
    C2=AVERAGEIF(A1:E1,"<>0",A1:E1)
    this is shorthand for select cell C2, then type (or copy and paste from here) the formula:
    =AVERAGEIF(A1:E1,"<>0",A1:E1)

  • HT5527 How much does this subscription cost

    How much does the new icloud subscription cost

    Same as before > iCloud: Managing your iCloud storage
    iCloud: Storage upgrades and downgrades

  • How we calculate product cost  without taken activity type

    Respected sir,
    pls tell me without taking activity type costing can we calculate product cost? is it possible through Overhead costing to  calculate product cost?
    or if we taken quantity base costing how we calculate each product cost?
    please help me.......
    Than'x
    Dhananjay

    Hi
    Without Activity type you cannot calculate Operation cost. It is not good practice. Why you want to do like this. Please explain.
    I think Over head cost can be calculated without operation cost.
    Regards
    J. Saravan

  • How to change average cost of an item?

    We have a scenario where the average cost of the goods has gone completely off of what we think it should be for a few items. Many reasons. But question is how do we change average cost assigned to these items without adding to the inventory?

    Hi,
    You can change the average cost of an item by performing an average cost update.
    The inventory quantity remains unaffected, but depending on whether you are increasing or decreasing the unit cost, the inventory value will get affected to that extent.
    Inventory > Costs > Average Cost Update - query the concerned item and update the cost as required.
    Thanks,
    DS

  • Average Costs of the Finished Products

    Hi,
    We are using the process order concept and we are calculating the costs of each FG seperately and our finished products are different kind of sizes and types.
    And the requirement here is that if we want to calculate the average cost of the two FG how we can do this ?
    For Example
    Product 1 is manufactured and the arrived cost is $ 30
    Porduct 2 is manufactured and the arrived cost is $ 10
    And if we use the ratios of 60 and 40 and we determine the avergae cost of (3060%+1040%)/100= $ 22
    And we adjust this $ 22 with Product cost of A and Product B.

    What is FG?

  • How to post Unplanned Delivery Cost before the actual invoice

    Hello Experts
    Our client has a scenario when they received the unplanned Delivery cost before the actual invoice of that purchase.
    We are handling the Unplanned Delivery cost by entering on the header level on the regular scenario.
    Can somebody give there exert thought how we handle the Unplanned Delivery Cost Invoice before the actual Invoice.

    Hello,
    It is a process issue and I have seen similar situtations. Here is a possible solution.
    1. You should estimate the standard freight and put this as part of Material Cost.
        This can be a condition type or it can come from PIR, depending upon how you
        calculate the material cost.
    2. At the purchasing level, add a condition type for Freight. So, at the time of GR,
        system will automatically accrue the freight amount. This will be based on the
        esitmated amount and not the actual amount. You can capture the offset  
        account in a separate accrued freight payable account.
    3. At the time of processing the actual freight invoice, debit the accrued freight
       payable account.
    4. On regular basis watch your estimated and actual freight expenses and make  
        necessary adjustments.
    Hope this helps.

  • Calculate NWC (Average for last 4 Month)

    Do someone know how to calculate NWC (Average for last 4 Month) using @AVG?FIX ("2002", Jan)     "Net Working Capital (Average for last 4 Month)" = @AVG(SKIPMISSING, "Net Working Capital", "2001"->Oct,"2001"->Nov, "2001"->Dec, "2002"->Jan);ENDFIXIn my case, we split the Year and Month Dim in our outline.Can this script work in under our situation???Thanks

    Try usingFIX ("2002", Jan) "Net Working Capital (Average for last 4 Month)" = @AVG(SKIPMISSING, "Net Working Capital"->"2001"->Oct,"Net Working Capital"->"2001"->Nov,"Net Working Capital"->"2001"->Dec,"Net Working Capital"->"2002"->Jan); ENDFIX

  • How to calculate cost(COGS) in Sales from project

    Dear all,
    I have a situation like this:
    My customer do not emplement PS(project system) and when managing sales from project, i don't know how to calculate cost of good sold for the service line.
    Each project (contruction site; building, ....), I use 1 material master as service item to manage. while project excecution, we issue material; collect labor cost; cost of external service, so on.... total up to COGS. but i don't know how to get this one in Sales order. Could you give me some suggestions?
    thanks!

    Dear,
    You need to create all these activities ( LABOUR CCOST/ COST OSFG EXTERNAL SERVICES)as Service Material and then put it into S.O. as Service Item and Bill the same.
    Regards
    AJIT K SINGH
    HAPPY TO HELP U

Maybe you are looking for