YTD average in MDX

Hi,
I am trying to calculate AVG YTD need an MDX formula.Tried this formula
(SUM(YTD(),(MEASURES.PERIODIC,ACCOUNT))) / IIF((TIME.CURRENTMEMBER.PROPERTIES("HLEVEL"="1"),12,STRTOVALUE(TIME.CURRENTMEMBER.PROPERTIES("MONTHNUM_FY")))
any suggessions

Hi  Nijsink,
Maybe you need to use  StrToValue([Time].Currentmember.Properties("MONTHNUM")).
But be careful. This is one of worst performance fucntion in the MDX so if you put it in the Account dimension,
Its performance will not be good. especailly with SQL2005.
I hope it will help you.
Thank you.
James Lim

Similar Messages

  • Crosstab YTD averages problem

    Pulling a large set of customer contacts (100,000s of rows) with each row being a contact. I have a crosstab which groups by location and employee, when the detail rows are the types of contacts (in person, telephone, etc) and then I have months across the top. Here's the problem I'm having:
    I need to calculate the average # of contacts per business day. I could countdistinct(contact_date) if I were basing the average on the number of days on which contacts occured (for each dept), but the report has to calculate averages for all departments based on the same # of business days per month (21 in January, 19 in February, etc.). If I just put the number of days on each row in the dataset, I could do min or max or avg and it would work fine for the months' averages, but the totals at the end for YTD won't work. I can't sum, because I have the # of business days on each row, so 1000 contacts in a month with 21 days would put a denominator of 21000.
    This is how it would look working the way I want it to.

    Thank Graham,
    when i use the same technique of setting up a nested counter and highlighting the fields based on that value...
    The counter should be set on every column of the row?
    It is because i found there is the problem, when i set the counter on every column...all the row will be highlighted without condition (x<=5), i think the counter may overcounted or not work.
    for example..
    user   action count
    1. a     10
    2. b     9
    3. c     8
    4. d     7
    5. e     6
    when i set the counter (let's say x<=2), on both user and action
    i will see the user 1,2 had been highlight and whole action column as well.
    and i also struggling with the filtering..
    i saw the whitepaper, can it apply in the crosstab?

  • YTD average issue

    Hi There,
    I setup Dynamic Time Series YTD, it works correctly for sum aggregation, however, for example, I have employee count per month, in this case, it should be average of sum of employee,
    Jan Feb Mar YTD (Mar)
    2 3 4
    It should be (Jan + Feb + Mar)/3 = 3, however the YTD (Mar) number is not showing correctly, I tag "Total Employee" as TB Average.
    Anything I am doing wrong here?

    I check tech reference:
    "Oracle recommends that you avoid assigning time balance properties (First, Last, Average, Skip Missing) to members set for dynamic calculations if you plan to use these members in Dynamic Time Series calculations. Doing so may retrieve incorrect values for the parent members in your accounts dimension."
    Is this the reason? If this is the case, then how do we handle YTD for employee count?

  • Setting in BEx formula for calculation of YTD and MAT

    Hi,
    I have to implement 2 formulae in BEx.
    As I am trying to avoide ABAP coding, can anyone please suggest SETTINGS in BEx FORMULA to achieve these below mentioned 2 formulae -
    1 ) Calculate YTD - Average of the sum of the reported quarter values from the start of the year until and including the selected quarter. But, if we enter Quarter 3 and there is no data for quarter 2 then the average should be equal to sum of data for Q1 and Q2 divided by 2 instead of 3 even though user entered Q3.
    2) Calculate MAT - Average of the sum of the reported quarter values for the last 4 quarters. We need to check last 4 quarters. If data is present only for 3 quarters in the last four quarters then the MAT will be data for 3 quarters divided by 3.
    Please suggest BEx formulae settings, if any for the above calculations.
    Thanks a lot.
    Regards,
    Prerna

    Hi
    I am not sure requirement related YTD is possible without customer exits variable
    But regarding your 2nd requirement
    Calculate MAT
    Create four restricted key figures by using offset (0, -1, -2, -3) and restriction characteristic will be quarter.
    Create a calculated key figure "Count of Quaerter". This will consist of "Number of rows" key figure with exception aggr on the Quarter characteristic and aggr will be "Count for all not eq to 0".
    Create a calculated key figure which will be sum of four restricted key figures divided by Count of Quarter.
    Kindly let me know incase further details required
    Best Regards
    Rohit

  • General Average question

    I need to create 3 different calculations based on one single measure.
    I have a Fact table containing the measure and two dimensions (time and category).
    It would be something like:
    1 - The average for the current month (AVG);
    2 - YTD average;
    3 - and the running average, for all the entries for each category.
    I have a dashboard prompt with year and month filters. Once the end user makes his selection, the request must display:
    1- Current Month AVG, based on filters;
    2- YTD AVG, based on filters;
    3- Running AVG, NOT based on filters.
    My question is about the running AVG. I've tried the MAVG function, but it doesn't apply to the request needs. Also, since there are filters for the request, I'd not know about the integer I should use in the formula (MAVG, MEASURE, INTEGER).
    Any Ideas of what should I do to have this measure calculated?! I'm considering change the ETL interface and calculate it in a plain cell, but first I need to know if I can have it resolved in the OBIEE Admin Tool.
    Thanks in Advance!

    But what is the difference between average exchange rate, buying rate and selling exchange rate.
    Does system has a way to calculate those exchange rate if we specifiy the exchange rate type or we have to input the figures in SPRO tables in that exchange rate type ?
    Regards,
    Rajesh.

  • Sorting on a total column or calculated column in a pivot table

    We have a pivot table showing customer activity by month. We have added a calculated field to show the YTD average instead of a total column. Is there a way to sort on this calculated field? We have applied a sort on the measure in the criteria, and our resulting pivot table sorts by the values in the most recent month, not by the YTD average.

    I think we cant sort when we use a pivot view becoz all the rows are already fixed. Say your rows are sales and volume and columns are year 2007 and 2008, imagine if you r given the sorting ability then if number of units solds is more then it need to change the rows (but in pivot table rows are fixed). So, we cant sort in pivot tables!!

  • Formula's at Universe level

    Hi All,
    I want to create objects at universe level for the following :
    Ver:- BOXIR2, Database is Oracle.
         Monthly averages for the last 12 months u2013 Balances for last 12 months are shown in the report. These are previous 12 months, starting from the month of the report date.
         Variance between last month and previous month u2013 This column will display the variance between two consecutive months. The optional attributes of FX variance can be used by the user in that case.
               Variance between last month and same month from previous year u2013 This is the difference in volume of balances for the last month of the current year and the same month of the previous year.
         Year-to-Date (YTD) average u2013 This is an aggregated average of balances up to the current month in the current year.
         Previous year average - This is an aggregated average of balances up to the current month, but for the previous year.
         Variance between YTD average and Full Year- previous year u2013 This is the difference between the balances YTD and the year ending December 31st of the previous year.
    Please contribute your inputs .
    Thankyou in advance
    Regards,
    Chaitanya

    Little hints to get you started.
    Monthly averages for the last 12 months u2013 Balances for last 12 months are shown in the report. These are previous 12 months, starting from the month of the report date.
    Avg(<columnName>)
    Where
    Date_dim.reportdate between add_months(<reportdate>, -12) and reportdate
    Year-to-Date (YTD) average u2013 This is an aggregated average of balances up to the current month in the current year.
    Avg(balance)
    where
    Date_dim.reportdate between add_months(<reportdate>, - extract(month from sysdate)) and reportdate
    Previous year average - This is an aggregated average of balances up to the current month, but for the previous year.
    Avg(balance)
    Where
    Date_dim.reportdate between add_months(add_months(<reportdate>, -12),-extract(month from add_months(<reportdate>, -12))) and reportdate
    --Kuldeep

  • Memberrange error on Financial Reports

    Hi there,
    We have an ASO cube and the outline is as follows :
    Time Period
    ......Year Total
    .......Otr1
    .......Jan
    .......Feb
    Scenario
    ........Actual
    ........YTD Actual with MDX formula
    (CASE
    WHEN [Time Period].CurrentMember IS [Time Period] THEN ([Year Total], [Actual])
    WHEN [Time Period].CurrentMember IS [Year Total] THEN ([Year Total], [Actual])
    ELSE
         CASE WHEN IsUda ([Account].CurrentMember, "TBLast") THEN ([Time Period].CurrentMember,[Actual])
         ELSE Sum([Time Period].Oct:[Time Period].CurrentMember,[Actual])
         END
    END)
    In the report we need last months YTD actual on the POV that was selected, so this is how it is done:
    Col :
    criteria 1: Relative to Current Point of View for Time Period offset by -1
    criteria 2 : YTD Actual
    The issue is that as soon as the report run and the POV is selected to Year Total or Time Period we get a Memberrange error.
    Any help would be appreciated !
    Thanks
    Edited by: MVenter on Feb 19, 2009 12:09 PM

    9.3
    No I am not using the Range function anywhere!! Not in Essbase or in Reports.
    When I select Year Total Get a different error now :
    5200: Error executing query: Error(1042013) Network error [10054]: Cannot Receive Data;hasPovDims=1;povXML=<?xml version="1.0"?><datasources><datasource name="MI_GL" dsid="a01603f_11ef3561733_-7268" allowEdit="1"><dim name="Time Period" dimIndex="1" dsName="MI_GL" keyDimName="Time Period" memberName="Time Period" displayName="Time Period: Time Period"/><dim name="Years" dimIndex="7" dsName="MI_GL" keyDimName="Years" memberName="FY09" displayName="Years: FY09"/><dim name="Currency Type" dimIndex="9" dsName="MI_GL" keyDimName="Currency Type" memberName="EUR Book" displayName="Currency Type: EUR Book"/></datasource></datasources>
    But as soon as I change POV to Jan I get the correct data

  • 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');

  • MDX - How to group results to calculate the average

    Hi,
    I have the following MDX query which selects total unique visits per day between a range of my choosing:
    WITH MEMBER [Measures].[MyAvg] AS
    Round( AVG(
    EXCEPT([Dim Date].[Day Of Week].Members,
    {[Dim Date].[Day Of Week].[All].&[1],[Dim Date].[Day Of Week].[All].&[7],[Dim Date].[Day Of Week].[All].UNKNOWNMEMBER}
    ), [Measures].[UniqueVisitsDay]
    ), 2)
    SELECT { [Measures].[MyAvg] } ON COLUMNS,
    NON EMPTY { [Dim Date].[Year Month Date].[PK Date].&[2013-03-01T00:00:00]:[Dim Date].[Year Month Date].[PK Date].&[2013-03-31T00:00:00] } ON ROWS
    FROM
    [MyCube];
    Which gives me the following results:
    01/03/2013 634
    02/03/2013 16
    03/03/2013 19
    04/03/2013 698
    05/03/2013 704
    06/03/2013 692
    07/03/2013 774
    08/03/2013 755
    09/03/2013 9399
    10/03/2013 19990
    11/03/2013 775
    12/03/2013 835
    13/03/2013 868
    14/03/2013 900
    15/03/2013 844
    17/03/2013 19
    18/03/2013 248
    19/03/2013 920
    20/03/2013 958
    21/03/2013 1092
    22/03/2013 798
    23/03/2013 21
    24/03/2013 10
    25/03/2013 731
    26/03/2013 770
    27/03/2013 537
    28/03/2013 300
    29/03/2013 28
    30/03/2013 8
    31/03/2013 4
    What I need to do is get the average of this set of results over the month.  I am expecting this answer:
    1478.233333 (which is total unique hits - 44347 / total rows - 30)
    But when I change my MDX code to this:
    WITH MEMBER [Measures].[MyAvg] AS
    Round( AVG(
    EXCEPT([Dim Date].[Day Of Week].Members,
    {[Dim Date].[Day Of Week].[All].&[1],[Dim Date].[Day Of Week].[All].&[7],[Dim Date].[Day Of Week].[All].UNKNOWNMEMBER}
    ), [Measures].[UniqueVisitsDay]
    ), 2)
    SELECT { [Measures].[MyAvg] } ON COLUMNS,
    NON EMPTY { [Dim Date].[Year Month].[Mar 2013]:[Dim Date].[Year Month].[Mar 2013] } ON ROWS
    FROM
    [MyCube];
    I get the following result:
    MyAvg
    Mar 2013 9868
    Actual: 9868  
    Expected: 1478.233333
    What am I doing wrong?
    Thanks,
    Rob

    Hi Rob,
    In SQL Server Analysis Serviceswe do not have a measure Average aggregation type. Fortunately, we have Sum and Count, and since Average = Sum / Count, we can build our own Average aggregation when we need one. Here is a blog about how to create a average
    measure, please refer to the link below.
    Average Aggregation in Analysis Services
    Regards,
    Charlie Liao
    TechNet Community Support

  • Custom measure to calculate average in YTD model

    Hi,
    I am working in a YTD model which currently has the standard measures: YTD, QTD and periodic. I now need a new custome measure to calculate the average of the YTD values.
    So, if on an account I have the following YTD amounts:
    month 1: 10
    month 2: 20
    month 3: 30
    I would need the new measure to give me
    month 1: 10
    month 2: (10+20)/2 = 15
    month 3: (10+20+30)/3 = 20
    This will also have to work where a month in the middle has a zero value, for example:
    month 1: 10
    month 2: 0
    month 3: 30
    I would need the new measure to give me
    month 1: 10
    month 2: (10+0)/2 = 5
    month 3: (10+0+30)/3 = 13.3333333333
    Has anyone done this before and would be willing to share the measure formula use with me?
    Thanks,
    Arnold

    So I've tried to copy the YTD measure and adjust it
    IIF([%ACCOUNTDIM%].PROPERTIES("ACCTYPE")="EXP" OR [%ACCOUNTDIM%].PROPERTIES("ACCTYPE")="AST", 1 ,-1)*(([MEASURES].[SIGNEDDATA],CLOSINGPERIOD([%TIMEDIM%].[%TIMEBASELEVEL%]))/[%TIMEDIM%].CURRENTMEMBER.PROPERTIES("MONTHNUM"))
    but that doesn't work, whilst I can see the measure I get no values at all.

  • MDX YTD until previous month - problem for january

    Hello,
    I have a Time hierarchy YQMD
    I want to make an MDX calculation that  gives me the YTD until the previous month.
    I did this with formula:
    I first made a 'normal' ytd calculation resulting in 'MyMeasureYTD'
    sum
    (YTD([Time].[Years Quarters Months Weeks Days]),[Measures].[MyMeasure])
    and then I use the following formula
    ParallelPeriod([Time].[Years Quarters Months Weeks Days].[Months]),[Measures].[MyMeasureYTD]
    this works fine for all months except for january: when the selected month is 'january' (of any year) , the result should be NULL, instead of the YTD until december of the previous year (which is what the formula above gives me).
    I have tried with using an iif in the formula that checks on currentmember being the firstchild, but I don't seem to get the required result.
    I would be very gratefull for your help!
    kind regards
    Sylvie

    Hi Sylvie,
    Please refer to the MDX below with AdventureWorks sample:
    with member [x]
    as
    iif([Date].[Calendar].currentmember is
    Ancestor([Date].[Calendar].currentmember,3).firstchild.firstchild.firstchild,
    "NULL",[Measures].[Internet Sales Amount])
    select [x] on 0,
    [Date].[Calendar].[Month].members on 1
    from
    [Adventure Works]
    In my scenario, the hierarchy of Date Calendar is Year->Half Year->Quarter->Month->Day. So my Ancestor() function need to step back 3 levels. If you date hierarchy is Year->Quarter->Month->Day. It should be:
    Ancestor
    ([Date].[Calendar].currentmember,2).firstchild.firstchild
    If you have any question, please feel free to ask.
    Regards,
    Simon Hou
    TechNet Community Support

  • YTD and Previous YTD in MDX query

    Hi All,
    I have two parameters Start Date and End Date. when i select the values of start date  as 5th jan 2014 and End Date as 13 th Jan 2014, i want to see the Count of Tickets from 5th Jan 2014 till 13th Jan 2014 as YTD Count of Tickets and Count
    of Tickets from 5th Jan 2013 till 13th Jan 2013 as PYTD Count of Tickets. The date range is user specific and is dynamic. I have used cube to create SSRS reports.
    Could anyone please tell me how i can pass the selected dates to the query and get YTD and PYTD values?
    Thanks
    Syed Faiz
    SFH

    Hello!!
    if you have user hierarchy in  date dimension that looks like : year-month-date than you could use following mdx query:
    with member measures.[count of tickets last year] as aggregate({parallelperiod([date].[Y-M-D].[year],1,[Date].[Y-M-D].[Date].&[20140104]):
    parallelperiod([Date].[Y-M-D].[year],1,[Date Submit].[Y-M-D].[Date].&[20140107])},measures.[count of tickets])
    select {measures.[count of tickets],measures.[count of tickets last year]} on 0,[your dimensions] on 1
    from yourcube
    where
    {[Date].[Y-M-D].[Date].&[20140104]:[Date].[Y-M-D].[Date].&[20140107]}
    y-m-d is the name of the hierarchy. This query returns results for dates between 01/04/2014 and 01/07/2014.
    In order for the query to work you must map date parameters to ssas date dimension members. You should use strtomember function. 
    These are usefull links:
    http://jsimonbi.wordpress.com/2011/03/22/using-a-date-parameter-in-ssrs-with-mdx/
    http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/using-a-calendar-for-a-date-hierarchy-pa-2010/
    HTH
    Ivan Roguljić

  • Can we Implement YTD using MDX syntax in olap universe.

    Hi,
    I am trying to implement YTD prompt from universe level. Can any one suggest how can i implement it using MDX syntax in olap universe.
    Regards,
    Anil Kumar.

    Hi,
    In MDX, you need to generate the technical name to have you expression valid.
    Moreover the correct syntax is: SUM(MTD(member),measure)
    So in your case the prompt must be constrained on the technical name and cannot be free, the correct syntax is:
    <EXPRESSION> SUM(MTD(@Prompt('YYYYMM','A','Cal. year / month\L01 Cal. year / month',mono,primary_key)),
    @(Select(Key Figures\Total Variance)) </EXPRESSION>
    Last, the result will be not really significant because you are asking for a MTD with a Year-Month as time member: the result will be the measure value for the selected month.
    In your case you need to have a time series function for an upper level like YTD or QTD. If you want to use MTD, the the time member selected would be at a lowest level, such as week or day.
    Regards
    Didier

  • Average YTD in SSRS

    The image above is a replica of a report that already exist. Now users want to see average Year to Day(YTD) per for each product. The idea is to aggregate the sum of each product and divide by the count of months. This report is group by month by year.
    I understand using Window Function but I am running Sql Server 2008 so cannot frame. How do I go about this using SSRS?
    Zionlite

    Hi Zionlite,
    Could you provide some sample data in form of a CREATE temporary table or CTE declaration?
    Your example looks odd to me, als YTD Sum does not align with MTD Sums. Maybe you are calculating on a fiscal year and calculation does not start with january....but even then it's odd to me that YTD Sales Tables is 50 in January, 100 (not 50+10 = 60)
    in February and dops back to 50 in March ?!
    Regarding alternatives to framing via Window Functions Itzik Ben Gan suggests two alternativ methods in his book "Microsoft SQL Server 2012 HIgh-Performance T-SQL USing Window Functions".
    Here an excerpt of the code provided by him. Maybe that helps already.
    -- FIRST_VALUE, LAST_VALUE, NTH_VALUE
    -- FIRST_VALUE, LAST_VALUE as window functions
    SELECT custid, orderdate, orderid, val,
    FIRST_VALUE(val) OVER(PARTITION BY custid
    ORDER BY orderdate, orderid) AS val_firstorder,
    LAST_VALUE(val) OVER(PARTITION BY custid
    ORDER BY orderdate, orderid
    ROWS BETWEEN CURRENT ROW
    AND UNBOUNDED FOLLOWING) AS val_lastorder
    FROM Sales.OrderValues;
    custid orderdate orderid val val_firstorder val_lastorder
    1 2007-08-25 10643 814.50 814.50 933.50
    1 2007-10-03 10692 878.00 814.50 933.50
    1 2007-10-13 10702 330.00 814.50 933.50
    1 2008-01-15 10835 845.80 814.50 933.50
    1 2008-03-16 10952 471.20 814.50 933.50
    1 2008-04-09 11011 933.50 814.50 933.50
    2 2006-09-18 10308 88.80 88.80 514.40
    2 2007-08-08 10625 479.75 88.80 514.40
    2 2007-11-28 10759 320.00 88.80 514.40
    2 2008-03-04 10926 514.40 88.80 514.40
    3 2006-11-27 10365 403.20 403.20 660.00
    3 2007-04-15 10507 749.06 403.20 660.00
    3 2007-05-13 10535 1940.85 403.20 660.00
    3 2007-06-19 10573 2082.00 403.20 660.00
    3 2007-09-22 10677 813.37 403.20 660.00
    3 2007-09-25 10682 375.50 403.20 660.00
    3 2008-01-28 10856 660.00 403.20 660.00
    -- returning one row per customer
    WITH C AS
    SELECT custid,
    FIRST_VALUE(val) OVER(PARTITION BY custid
    ORDER BY orderdate, orderid) AS val_firstorder,
    LAST_VALUE(val) OVER(PARTITION BY custid
    ORDER BY orderdate, orderid
    ROWS BETWEEN CURRENT ROW
    AND UNBOUNDED FOLLOWING) AS val_lastorder,
    ROW_NUMBER() OVER(PARTITION BY custid ORDER BY (SELECT NULL)) AS rownum
    FROM Sales.OrderValues
    SELECT custid, val_firstorder, val_lastorder
    FROM C
    WHERE rownum = 1;
    custid val_firstorder val_lastorder
    1 814.50 933.50
    2 88.80 514.40
    3 403.20 660.00
    4 480.00 491.50
    5 1488.80 1835.70
    6 149.00 858.00
    7 1176.00 730.00
    8 982.00 224.00
    9 88.50 792.75
    10 1832.80 525.00
    -- pre SQL Server 2012 solutions
    -- Using row numbers
    WITH OrdersRN AS
    SELECT custid, val,
    ROW_NUMBER() OVER(PARTITION BY custid
    ORDER BY orderdate, orderid) AS rna,
    ROW_NUMBER() OVER(PARTITION BY custid
    ORDER BY orderdate DESC, orderid DESC) AS rnd
    FROM Sales.OrderValues
    SELECT custid,
    MAX(CASE WHEN rna = 1 THEN val END) AS firstorderval,
    MAX(CASE WHEN rnd = 1 THEN val END) AS lastorderval,
    MAX(CASE WHEN rna = 3 THEN val END) AS thirdorderval
    FROM OrdersRN
    GROUP BY custid;
    custid firstorderval lastorderval thirdorderval
    1 814.50 933.50 330.00
    2 88.80 514.40 320.00
    3 403.20 660.00 1940.85
    4 480.00 491.50 407.70
    5 1488.80 1835.70 2222.40
    6 149.00 858.00 330.00
    7 1176.00 730.00 7390.20
    8 982.00 224.00 224.00
    9 88.50 792.75 1549.60
    10 1832.80 525.00 966.80
    -- using carry-along-sort technique
    -- step 1: create concatenated strings
    SELECT custid,
    CONVERT(CHAR(8), orderdate, 112)
    + STR(orderid, 10)
    + STR(val, 14, 2)
    COLLATE Latin1_General_BIN2 AS s
    FROM Sales.OrderValues;
    custid s
    85 20060704 10248 440.00
    79 20060705 10249 1863.40
    34 20060708 10250 1552.60
    84 20060708 10251 654.06
    76 20060709 10252 3597.90
    34 20060710 10253 1444.80
    14 20060711 10254 556.62
    68 20060712 10255 2490.50
    88 20060715 10256 517.80
    35 20060716 10257 1119.90
    -- step 2: group, aggregate and extract
    WITH C AS
    SELECT custid,
    CONVERT(CHAR(8), orderdate, 112)
    + STR(orderid, 10)
    + STR(val, 14, 2)
    COLLATE Latin1_General_BIN2 AS s
    FROM Sales.OrderValues
    SELECT custid,
    CAST(SUBSTRING(MIN(s), 19, 14) AS NUMERIC(12, 2)) AS firstorderval,
    CAST(SUBSTRING(MAX(s), 19, 14) AS NUMERIC(12, 2)) AS lastorderval
    FROM C
    GROUP BY custid;
    custid firstorderval lastorderval
    1 814.50 933.50
    2 88.80 514.40
    3 403.20 660.00
    4 480.00 491.50
    5 1488.80 1835.70
    6 149.00 858.00
    7 1176.00 730.00
    8 982.00 224.00
    9 88.50 792.75
    10 1832.80 525.00
    -- in case ordering element supports negative values, e.g., orderid
    WITH C AS
    SELECT custid,
    CONVERT(CHAR(8), orderdate, 112)
    + CASE SIGN(orderid) WHEN -1 THEN '0' ELSE '1' END -- negative sorts before nonnegative
    + STR(CASE SIGN(orderid)
    WHEN -1 THEN 2147483648 -- if negative add abs(minnegative)
    ELSE 0
    END + orderid, 10)
    + STR(val, 14, 2)
    COLLATE Latin1_General_BIN2 AS s
    FROM Sales.OrderValues
    SELECT custid,
    CAST(SUBSTRING(MIN(s), 20, 14) AS NUMERIC(12, 2)) AS firstorderval,
    CAST(SUBSTRING(MAX(s), 20, 14) AS NUMERIC(12, 2)) AS lastorderval
    FROM C
    GROUP BY custid;
    Cheers
    Martin

Maybe you are looking for

  • Application to iBooks problem: iTunes account already used for apps

    Hi, I work at a publishing company, where we have an iTunes Connect account we use to sell apps to the appstore. I'm trying to use this same account to distribute iBooks as well. So I go to the application portal at https://itunesconnect.apple.com/We

  • [REQUEST UEFI GOP vBIOS] MSI N660 TF 2GD5/OC

    Dear Svet, Is there a ROM for my video card? Model: MSI N660 TF 2GD5/OC SN: 602-V287-050B1302029749 NVFLASH report below NVIDIA Firmware Update Utility (Version 5.118) Adapter: GK1xx                (10DE,11C0,1462,2871) H:--:NRM B:01,PCI,D:00,F:00 Th

  • How can I use instance methods created with HashMap

    class Template static HashMap customer; a few methods created various customer public static display() //to display a particular customer information among i've created above              String objectName,currentObjectName;           Customer custom

  • Where to keep java Servlet  files in  Oracle Apps R12 ??

    Hi all, We have custom java Servlet files .. can any body help me where exactly do we need to keep java servlet in Oracle applications R12. Regards Bala

  • Fastest conversion in compressor

    what settings in compressor will give the fastest conversion from a final cut reference movie into a compressed format that can be played on a windows computer? i am not concerned about video quality. i just need to share a rough edit with my clients