Query help - max value

Hello together,
I have the following example table:
PERSONID_____BEGINDATE_____ENDDATE_____ROOMID
1033_________1.10.2009______5.10.2009_____567
1033_________1.10.2010______5.10.2010_____891
1041_________2.11.2009______1.12.2009_____123
1041_________3.12.2010______3.12.2011_____456
What I need is the line with the highest end date by PERSONID.
In the example above I would need to see the 2nd and the 4th line.
This is the query I am using:
select example.personid, max(example.begindate), max(example.enddate), example.roomid
from example
where (example.enddate >= trunc(sysdate))
group by example.personid, example.roomid
The problem is it always gives me 4 result lines (in this example). I think because it gets grouped by the roomid, which is distinct in each line, in the example.
What I actually want is ONE result line, per person, which shows me the highest end date, independently of the room id.
Can someone please point me in the right direction? Using max(date) is correct?
Thank you very much in advance.

Hi,
Welcome to the forum!
The most versatile way is:
WITH     got_r_num     AS
     SELECT     e.*
     ,     ROW_NUMBER () OVER ( PARTITION BY  personid
                               ORDER BY          enddate     DESC
                       ) AS r_num
     FROM    example      e
     WHERE     enddate      >= TRUNC (SYSDATE)
SELECT  *     -- Or list all columns except r_num
FROM     got_r_num
WHERE     r_num     = 1
;What do you want to do in case of a tie? For example, what if both rows for personid=1033 had the same enddate?
The query above would pick one of them arbitarily.
If you want both of them, then change ROW_NUMBER to RANK.
If you want to add a tie-breaker (e.g., pick the one with the latest begindate), then add more columns to the analytic ORDER BY clause.
Edited by: Frank Kulash on Sep 15, 2009 2:12 PM
Added <tt>WHERE     enddate     >= TRUNC (SYSDATE)</tt> condition

Similar Messages

  • SQL Query for max values!!

    Hi to all,
    I have four tables
    Tbl_one
    Tbl_two
    Tbl_three
    Tbl_four
    the relation between these tables is
    Tbl_one.SEQ = Tbl_two.SEQ)
    and tbl_two.case_SEQ = Tbl_four.SEQ
    AND Tbl_two.ORDER_SEQ = tbl_three.SEQ))
    I want a query like this
    Select tbl_one.com_name, tbl_three.test_date,tbl_four.order_date
    from Tbl_one,Tbl_two,Tbl_three,Tbl_four
    where Tbl_one.SEQ = Tbl_two.SEQ)
    and tbl_two.case_SEQ = Tbl_four.SEQ
    AND Tbl_two.ORDER_SEQ = tbl_three.SEQ))
    and tbl_three.test_date in (select max(test_date) from tbl_three)
    and tbl_four.order_date in select(max(order_date from tbl_for)
    and max(test_date)> Max(order_date)
    any way it is possible?
    the real problem is there are multiple test_dates and
    multiple order_date for same seq in tbl_one.seq.
    eg: -
    name (indian) which has three or more test_date and each test_date have more than one order_date
    indian (name) 01/01/2009(test_date) has ---- 01/10/2009, 01/20/2009 and 01/21/2009) order_dates
    india(name) 02/02/2009 (test_date) has ----- 02/10/2009, 02/20/2009 and 02/30/2009 (order_dates)
    india(name) 03/03/2009 test_date has ----- 03/10/2009, 03/20/2009 , 03/25/2009 (order_dates).
    japan has the same situation and so on
    what i wanted from the query is
    max(test_date)= 03/03/2009 > max(order_date)=03/25/2009
    ans: -
    name
    india(name) 03/03/2009 (test_date) 03/25/2009(order_date)
    etc. etc . etc.
    thanks!!
    Edited by: pl/sql baby on Mar 24, 2009 10:45 AM
    Edited by: pl/sql baby on Mar 24, 2009 10:47 AM
    Edited by: pl/sql baby on Mar 24, 2009 10:51 AM
    Edited by: pl/sql baby on Mar 24, 2009 10:57 AM

    Please use tags either side of code / data (to preserve the formatting and spacing).
    I don't understand your requirement... 03/03/2009 is not greater than 03/25/2009 ?
    Could you please be clearer in your input/output samples and explain more about how to generate the output?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • BI Query : Min & Max Values

    Hi all,
    I have a query regarding finding the max and min values based on Keyfigure.
    Below are the values which i get in the report.From these values i need the Max and Min value separately based on the keyfigure.
    I tried option in Aggregation tab and Calculation tab.However i am getting the Maximum value correctly whereas minimum value shows as Zero.
    0.58
    0.65
    0.88
    Please guide me and help me to resolve this issue.
    Thanks & Regards,
    Kavitha

    Hi,
    The following is the sample data.
    Calday
    Inspection Point
    P1
    Min      Max
    01.04.2014
    01.04.14-06.HRS |
    0.00
    0.58
    01.04.14-10.00HRS |
    0.00
    0.65
    01.04.14-14.00HRS |
    0.00
    0.88
    We are getting maximum value as 0.88 but min value should be 0.58 but we are getting 0.00.
    Please help me to resolve the issue

  • Query Help---DISTINCT values

    Hi..
    I have the following query..
    I'm getting the following o/p...
    Offshore     In-house     1858     616
    Offshore     In-house     1858     615
    Onshore      In-house     1858     611
    Offshore     In-house     1858     923
    Offshore     In-house     1858     613
    Onshore             In-house     1858     941
    Onshore             In-house     1858     940
    Onshore             In-house     1858     890Can I get distinct onshores and offshores values..like in the above case only one in-house for offshore and one in-house for onshore and if there are multiple repeating values for offshore and on shrore..I need only dinstinct values in both offshore and onshore..
    Any help..?
    Edited by: user10280715 on Apr 14, 2009 10:21 AM

    If I have a result set like this..
    Offshore     In-house     1858     616
    Offshore     In-house     1858     615
    Onshore      In-house     1858     611
    Offshore     In-house     1858     923
    Offshore     In-house     1858     613
    Onshore             In-house     1858     941
    Onshore             In-house     1858     940
    Onshore             In-house     1858     890the o/p I'm looking for..
    Offshore     In-house     1858     616
    Onshore             In-house     1858     890If I have different result set like..
    Offshore     In-house     1858     616
    Offshore     In-house     1858     615
    Offshore      aaaaaaaa     1859     611
    Offshore     aaaaaaaa     1859     923
    Offshore     In-house     1858     613
    Onshore             In-house     1858     941
    Onshore             In-house     1858     940
    Onshore             eeeeeee     1860     890
    Onshore             eeeeeee     1860     941I'm looking for..
    Offshore     In-house     1858     616
    Offshore      aaaaaaaa     1859     611
    Onshore             In-house     1858     941
    Onshore             eeeeeee     1860     890Is it possible..

  • Query help Multiple values in condition

    Hi,
    I am trying to do something like this
    SELECT clave_grupo FROM SS_TTABLAS
    WHERE CLAVE_GRUPO IN
    CASE WHEN (condition) THEN 'v1'
    ELSE ('v1','v2',v3')
    end
    This is the error msg:
    ORA-00907: missing right parenthesis
    just before ,'v2',v3')
    Is it possible?

    Hi,
    Welcome to the forum!
    Here's one way:
    SELECT  clave_grupo
    FROM      ss_ttablas
    WHERE      clave_grupo     = 'v1'
    OR     (     clave_grupo     IN ('v2', 'v3')
         AND     NOT condition
    ;A CASE expression returns a single value. The error in what you posted was that the ELSE branch was trying to return 3 values.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • Query help needed in finding max value between two columns

    I have a table as follows:
    -- INSERTING into TESTTABLE
    Insert into TESTTABLE (COLA,COL2,COLC,COLD,COLE,COLF) values ('A','4.5','AA',0.3,'AB',5.5);
    Insert into TESTTABLE (COLA,COL2,COLC,COLD,COLE,COLF) values ('B','1','BB',2.5,'BC',6.9);
    Insert into TESTTABLE (COLA,COL2,COLC,COLD,COLE,COLF) values ('C','2.6','CC',3.3,'CD',1.4);
    Insert into TESTTABLE (COLA,COL2,COLC,COLD,COLE,COLF) values ('D','1.8','DD',2.9,'DE',1.2);
    Insert into TESTTABLE (COLA,COL2,COLC,COLD,COLE,COLF) values ('E','6.8','EE',4.8,'EF',9.6);
    Insert into TESTTABLE (COLA,COL2,COLC,COLD,COLE,COLF) values ('F','2.0','FF',6.34,'FG',3.9);
    Insert into TESTTABLE (COLA,COL2,COLC,COLD,COLE,COLF) values ('G','1.7','GG',3.6,'GH',5.8);
    I want to get results as follows:
    COLA COL2 MaxCol MaxColVal
    A 4.5 AB 5.5
    B 1 BC 6.9
    C 2.6 CC 3.3
    D 1.8 DD 2.9
    E 6.8 EF 9.6
    F 2.0 FF 6.34
    G 1.7 GH  5.8I want to get the max value of either of the columns COLD or COLF.. Whichever column has got max value, then the corresponding value of COLC and COLE should be returned..
    For eg., in first row, COLF has higher value than COLD.. ie., COLF = 5.5 > COLD = 0.3, so for row1, i want the result as MaxCol is AB and MaxColvalue is 5.5..
    Similarly for third row, COLD =3.3 > COLF=1.4, so for 3rd row, i want the result as MaxCol is CC and MaxColvalue is 3.3
    How is it possible to do this in a qery? Any help.. please..

    SQL> select cola
      2       , col2
      3       , case greatest(cold,colf)
      4         when cold then colc
      5         else cole
      6         end maxcol
      7       , greatest(cold,colf) maxcolval
      8    from testtable
      9  /
    C COL MA  MAXCOLVAL
    A 4.5 AB        5,5
    B 1   BC        6,9
    C 2.6 CC        3,3
    D 1.8 DD        2,9
    E 6.8 EF        9,6
    F 2.0 FF       6,34
    G 1.7 GH        5,8
    7 rijen zijn geselecteerd.Regards,
    Rob.

  • Query to print the max value of time of the latest record from table

    hi
    i wrote this query
    which should return max fx_time of the latest or current value of fx_date
    plz help
    this wuery is giving current date fx_date but with all values not the max value of fx_time.
    select FX_DATE, FX_TIME,FROM_CURRENCY, TO_CURRENCY, ASK_RATE, BID_RATE,
    time_stamp, source,
    DESCRIPTION, FX_TYPE, OVERRIDDEN_RATE, OVERRIDDEN_BY, OVERRIDDEN_DATE ,
    ASK_RATE_INVERSE, BID_RATE_INVERSE, PX_LAST, PX_MID, PX_OPEN, PX_HIGH,
    PX_LOW,NY_TIME_OF_LAST_PRICE_UPDATE,
    DATE_OF_LAST_UPDATE, PX_BID_AM, PX_ASK_AM, PX_BID_PM, PX_ASK_PM, PX_CLOSE_1D,
    CHG_NET_1D,
    CHG_PCT_1D, PRICING_SOURCE, PX_BID_1M, PX_ASK_1M, PX_BID_1YR, PX_ASK_1YR,
    PX_CLOSE_MTD,
    PX_CLOSE_YTD, FXOPT_COMMODITY_CCY, FXOPT_SPOT_FXRATE, SPOT_RT_USD_FLAG,
    PRIOR_CLOSE_BID,
    PRIOR_CLOSE_MID, PRIOR_CLOSE_ASK from CURRENCY_EXCHANGE_TXN
    WHERE
    source='BLOOMBERG'
    AND FX_DATE=
    (select max(fx_date) JIM from CURRENCY_EXCHANGE_TXN
    ,(select max(fx_time) TIM from CURRENCY_EXCHANGE_TXN GROUP BY FX_TIME )
    what change should i do

    Hi,
    Try this:
    select FX_DATE, FX_TIME,FROM_CURRENCY, TO_CURRENCY, ASK_RATE, BID_RATE,
    time_stamp, source,
    DESCRIPTION, FX_TYPE, OVERRIDDEN_RATE, OVERRIDDEN_BY, OVERRIDDEN_DATE ,
    ASK_RATE_INVERSE, BID_RATE_INVERSE, PX_LAST, PX_MID, PX_OPEN, PX_HIGH,
    PX_LOW,NY_TIME_OF_LAST_PRICE_UPDATE,
    DATE_OF_LAST_UPDATE, PX_BID_AM, PX_ASK_AM, PX_BID_PM, PX_ASK_PM, PX_CLOSE_1D,
    CHG_NET_1D,
    CHG_PCT_1D, PRICING_SOURCE, PX_BID_1M, PX_ASK_1M, PX_BID_1YR, PX_ASK_1YR,
    PX_CLOSE_MTD,
    PX_CLOSE_YTD, FXOPT_COMMODITY_CCY, FXOPT_SPOT_FXRATE, SPOT_RT_USD_FLAG,
    PRIOR_CLOSE_BID,
    PRIOR_CLOSE_MID, PRIOR_CLOSE_ASK from CURRENCY_EXCHANGE_TXN
    WHERE
    source='BLOOMBERG'
    AND FX_DATE=
    (select max(fx_date) JIM from CURRENCY_EXCHANGE_TXN
    FX_TIME )
    [PRE]
    Please always use [ PRE ]  and [ / PRE ] tags when ever posting any code.
    Regards                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Aggregation level query "value help" = Only Values in InfoProv doesn't work

    Hi,
    I have a problem with Selection Variable for a BEx Query :
    - I'm working with SAP BI 7.0 support package 15
    - My query is an input ready query built for an "aggregation level" based on "real time infocube" (Planning);
    - Characteristic, I want to filter with variable, has query property Advanced->Query Execution for Filter Value Selection = Only Values in InfoProvider
    - Characteristic (info-object) has set the property: Business Explorer tab--> Query Def. Filter Value Selection as Only Values in the info-provider...
    My problem is:
    when I execute a query,
    the Select Values for Variables screen appears
    I push on the Select from List button
    and Select Values For MyCharacteristic appears...
    but it shows me all values in master data table of my characteristic instead of only values in the infoprovider!!!
    If I build a query for the Real Time Cube it works correctly, but if the query is built for the aggregation level it doesn't work!
    Could someone explain me why it doesn't work correctly?
    Thanks in advanced
    Fede

    Thanks to  Mariana Lazar
    30.03.2009 - 12:30:36 CET - Risposta by SAP     
    Dear Fede,
    Regarding the query built on aggregation level please note the following
    the aggregation level is always a Virtual Provider built other
    InfoProvider and hence it does not have the dimension table and hence
    the F4 mode D is not supported.
    Therefore when aggregation level is used in a query, F4 does not supportD-mode: 'Only Values in InfoProvider' and all master data values are
    displayed in the value list.
    Reference from the note
    984229 F4 modes for input help as of SAP NetWeaver 2004s BI
    4. Since other InfoProviders do not have a dimension table, the system
    displays only posted values if you select "Only Posted Values for
    Navigation". Otherwise, it displays the values from the master data
    table.
    Hope this should clarify your doubts. Please let me know if you have
    questions else please confirm the message at your earliest convenience.
    Thank you for your cooperation and kind regards,
    @KR@ Mariana Lazar
    Senior Support Consultant
    SAP Active Global Support
    Netweaver Business Warehouse

  • Need help regarding complex calculation using Max value and limiting data after Max date in MDX

    I am working on a bit complex calculated measure in SSAS cube script mode.
    Scenario /Data Set
    Date
    A
    B
    C
    A+B
    5/29/2014
    Null
    34
    Null
    34
    6/30/2014
    Null
    23
    45
    68
    7/15/2014
    25
    -25
    Null
    0
    8/20/2014
    -34
    Null
    Null
    -34
    9/30/2014
    25
    Null
    60
    25
    10/15/2014
    45
    -45
    Null
    0
    11/20/2014
    7
    8
    Null
    15
    a) Need to capture latest non-null value of Column C based on date
    with above example it should be 60 as of 9/30/2014
    b) Need to capture column A+B for all dates.
    c) Add values from column (A+B) only after latest date which is after 9/30/2014. 
    with above example it's last 2 rows and sum is 15
    d) Finally add value from step a and step c. which means the calc measure value should be = 75
    I need to perform all this logic in MDX. I was able to successfully get step a and b in separate calc measure, however i am not sure how to limit the scope based on certain date criteria. In this case it's, date> Max date(9/30/2014) . Also how should
    i add calculated members and regular members?
    I was able to get max value of C based on date and max date to limit the scope.
    CREATE MEMBER CURRENTCUBE.[Measures].[LatestC] AS
    TAIL( 
      NONEMPTY(
        [Date].[Date].CHILDREN*[Measures].[C]),1).ITEM(0) ,visible=1;
    CREATE MEMBER CURRENTCUBE.[Measures].[MaxDateofC] AS
    TAIL( 
      NONEMPTY(
        [Date].[Date].CHILDREN,[Measures].[C]),1).ITEM(0).MemberValue ,visible=1;
    Please help with Scope statement to limit the aggregation of A+B for dates > MaxDateofC? Also further how to add this aggregation value to LatestC calc measure?
    Thank You

    Hi Peddi,
    I gave TRUNC to both of the dates. But still the same issue. I think the problem is in returning the BolbDomain.
    return blobDomain;
    } catch (XDOException xdoe) {
    System.out.println("Exception in XDO :");
    throw new OAException("Exception in XDO : "+xdoe.getMessage());
    catch (SQLException sqle) {
    System.out.println("Exception in SQL :");
    throw new OAException("SQL Exception : "+sqle.getMessage());
    catch (OAException e) {
    System.out.println("Exception in OA :");
    throw new OAException("Unexpected Error :: " +e.getMessage());
    Thanks and Regards,
    Myvizhi

  • Combo box with multiple variables pre populating multiple fields and max value validation script help

    I am creating a pdf form that will be used as a calculator, and my javascript knowledge is pretty rudimentary. I can do the simple things, some of what I need to do is over my head.
    1. I believe I need to use a combo box to pre populate certain fields. I have 13 variables in the dropdown to choose from, and whatever the client chooses will pre populate three other fields (and each of the 13 variables will populate those three fields differently). I'm not exactly sure how to go about doing this. Is it custom keystroke script or does it require a document script? Also, I'm guessing that whatever script will be a series of if/then statements, correct?
    2. Once all the fields have been populated (be it pre population or filled in by the customer), I need to calculate the answer which is not my main problem—I already have the proper calculations in place and they work. The problem is that the answer has a maximum value even if the actual value goes over that max number. So, the actual answer is 14 but the max value can only be 12. How do I get the calculation to replace that actual number (14) with the max number (12)? Is that in the validation tab or should that go elsewhere?
    I feel like I know just enough to be dangerous but not terribly effective. Any help is appreciated.  
    Thanks!

    1. There is a tutorial on this here:
    http://acrobatusers.com/tutorials/change_another_field
    2. To set the maximum value to 12, try this as the custom calculation script for the field:
    //Custom calculation script
    //Get value of text field
    var a = this.getField("Text1").value;
    //If it is > 12, then make it 12
    if (a > 12){
    a=12
    event.value = a
    You need to replace "Text1" with the name of the field in your form.

  • Daily max(value) query for time period

    I am looking to get the maximum value for every 24 hour period for a month. So for example my date range can be defined by...
    select to_date('&date','mm yyyy')-1 + level as DateRange
    from dual
    connect by level <= '&days'
    ...where I can provide the first date of the month and number of days in the month or a lesser value if less time is required. So, the results of the above query plus 24 for the range. I thought a some googling would provide me what I needed, but my search came up empty.
    I was hoping to do something like this...
    select utctime, max(value) from table where utctime between....
    Any guidance would be much appreciated!
    Thanks!

    Zewbie wrote:
    Sorry about that...Oracle 10.2.0.5
    create table x (utctime date,
    pointnumber int,
    value float)
    insert into x values (to_date('02022012 232221','mmddyyyy hh24miss'), 10, 3.245);
    insert into x values (to_date('02022012 202202','mmddyyyy hh24miss'), 13, 14.5);
    insert into x values (to_date('02024012 103421','mmddyyyy hh24miss'), 10, 23.245);
    insert into x values (to_date('02024012 042528','mmddyyyy hh24miss'), 10, 33.245);
    insert into x values (to_date('02028012 022321','mmddyyyy hh24miss'), 14, 32.245);
    insert into x values (to_date('02028012 205221','mmddyyyy hh24miss'), 10, 2.245);
    insert into x values (to_date('02029012 211421','mmddyyyy hh24miss'), 14, 1.4345);
    insert into x values (to_date('02029012 082221','mmddyyyy hh24miss'), 10, 3.245);Do you have dates 7000 years in the future, or ido you have typos?
    So a query of the above would provide max daily value based on a pointnumber such as 10 for a given period.What are the results you want from the given data?
    What role does pointnumber play in this problem?
    If you want something like this:
    A_DATE      POINTNUMBER  MAX_VALUE
    02-Feb-2012          10      3.245
    02-Feb-2012          13       14.5
    02-Feb-2012          14
    03-Feb-2012          10
    03-Feb-2012          13
    03-Feb-2012          14for the 2 days starting with February 2, 2012, then you can do this:
    WITH   days_wanted     AS
         SELECT  DATE '2012-02-02'     -- starting date
                  + LEVEL - 1     AS a_date
         FROM     dual
         CONNECT BY     LEVEL  <= 2     -- number of days wanted
    SELECT    d.a_date
    ,       x.pointnumber
    ,       MAX (x.value)   AS max_value
    FROM       days_wanted  d
    LEFT OUTER JOIN        x  PARTITION BY (x.pointnumber)
                                   ON  TRUNC (x.utctime)  = d.a_date
    -- WHERE  x.pointnumber       = 10         -- If wanted
    GROUP BY  d.a_date, x.pointnumber
    ORDER BY  d.a_date, x.pointnumber
    ;

  • How to get max value of a column in VO?

    Hi ,
    i need to find the max value of a column named insured value in VO.
    Please help!!

    Hi,
    If this value is cuming from the Query then u can easily go for MAX function. If its a user enterable value then u need to iterate through all the rows for that column to find the max of them.
    Let me know.
    Regards,
    Gyan

  • SSAS Tabular DAX- Need to get MAX value of the MIN (top)hierarchy level row

    EDIT:
    I got closer to resolving the issue using MAX. 
    However, If I remove the department hierarchy and just place on the MAX measure I get the single largest value out of all departments. 
    It would be ideal if the measure could still SUM the "top level" values across everything in the system if the hierarchy is not placed on the rows grouping.
    So it returns the largest value for a given department, but if the department hierarchy isn't present I need it to return a SUM of all the level 1 values for all departments...
    Basically return MAX value from the MIN L1ID's DeptLevel value, but I can't seem to construct that DAX query.  So if DepartmentID hierarchy is on display it gets MAX per row, but if that is removed it dips into MAX GoalValue for each L1ID grouping with
    the MIN DeptLevel.
    /EDIT
    I have a rather odd data table I'm bringing into a SSAS Tabular model.
    Instead of having all data at each child level and then it adding up to a grand total in the parent, it has a grand total predefined at each child level.
    I just need this tool to display the raw data if at all possible instead of trying to aggregate everything. Filter on active level, ignore child levels.
    Is there a way to do that?
    Example:
    SalesGoalsByDepartmentLevel:
    Level1 (top level) = 5,000
    Level2( lower level) = 0
    Level3(lower still) = 500
    Level 4(lowest) = 4,250
    So note that adding up all the child levels is still $250 shy of the top 5,000.
    IT is just an odd business rule, basically each level is expected to meet that goal or exceed it, the top level goal is 5,000 but management doesn't care where that last 250 comes from, they do are that each defined level is met.
    These levels are in a hierarchy so if I view the top level of the hierarchy it adds up to 4250+500+5000=9750 when I just want to see 5,000 at the top level and the details when they drill down.
    I added a filter to just filter to the top level, but then when I drill down of course those lower levels are blank.
    Is there a way to filter on the current displayed level without aggregating all child levels?
    Thanks!

    You might want to take a look at the Parent-Child Hierarchies pattern here:
    http://www.daxpatterns.com/parent-child-hierarchies/
    You might write DAX code to check what is the "current" level (see BrowseDepth measure in the sample file you can download) and depending on its level, se the filter to blank to all the levels below, so you don't aggregate "children".
    Just an idea, I'm not sure if it corresponds to your requirement and I don't have time to make more tests.
    I hope it will be helpful.
    Marco Russo (Blog,
    Twitter,
    LinkedIn) - sqlbi.com:
    Articles, Videos,
    Tools, Consultancy,
    Training
    Format with DAX Formatter and design with
    DAX Patterns. Learn
    Power Pivot and SSAS Tabular.

  • How could I replace hard coded value in my sql query with constant value?

    Hi all,
    Could anyone help me how to replace hardcoded value in my sql query with constant value that might be pre defined .
    PROCEDURE class_by_day_get_bin_data
         in_report_parameter_id   IN   NUMBER,
         in_site_id               IN   NUMBER,
         in_start_date_time       IN   TIMESTAMP,
         in_end_date_time         IN   TIMESTAMP,
         in_report_level_min      IN   NUMBER,
         in_report_level_max      IN   NUMBER
    IS
      bin_period_length   NUMBER(6,0); 
    BEGIN
      SELECT MAX(period_length)
         INTO bin_period_length
        FROM bin_data
         JOIN site_to_data_source_lane_v
           ON bin_data.data_source_id = site_to_data_source_lane_v.data_source_id
         JOIN bin_types
           ON bin_types.bin_type = bin_data.bin_type 
       WHERE site_to_data_source_lane_v.site_id = in_site_id
         AND bin_data.start_date_time     >= in_start_date_time - numtodsinterval(1, 'DAY')
         AND bin_data.start_date_time     <  in_end_date_time   + numtodsinterval(1, 'DAY')
         AND bin_data.bin_type            =  2
         AND bin_data.period_length       <= 60;
      --Clear the edr_class_by_day_bin_data temporary table and populate it with the data for the requested
      --report.
      DELETE FROM edr_class_by_day_bin_data;
       SELECT site_to_data_source_lane_v.site_id,
             site_to_data_source_lane_v.site_lane_id,
             site_to_data_source_lane_v.site_direction_id,
             site_to_data_source_lane_v.site_direction_name,
             bin_data_set.start_date_time,
             bin_data_set.end_date_time,
             bin_data_value.bin_id,
             bin_data_value.bin_value
        FROM bin_data
        JOIN bin_data_set
          ON bin_data.bin_serial = bin_data_set.bin_serial
        JOIN bin_data_value
          ON bin_data_set.bin_data_set_serial = bin_data_value.bin_data_set_serial
        JOIN site_to_data_source_lane_v
             ON bin_data.data_source_id = site_to_data_source_lane_v.data_source_id
            AND bin_data_set.lane = site_to_data_source_lane_v.data_source_lane_id
        JOIN (
               SELECT CAST(report_parameter_value AS NUMBER) lane_id
                 FROM report_parameters
                WHERE report_parameters.report_parameter_id    = in_report_parameter_id
                  AND report_parameters.report_parameter_group = 'LANE'
                  AND report_parameters.report_parameter_name  = 'LANE'
             ) report_lanes
          ON site_to_data_source_lane_v.site_lane_id = report_lanes.lane_id
        JOIN (
               SELECT CAST(report_parameter_value AS NUMBER) class_id
                 FROM report_parameters
                WHERE report_parameters.report_parameter_id    = in_report_parameter_id
                  AND report_parameters.report_parameter_group = 'CLASS'
                  AND report_parameters.report_parameter_name  = 'CLASS'
             ) report_classes
          ON bin_data_value.bin_id = report_classes.class_id
        JOIN edr_rpt_tmp_inclusion_table
          ON TRUNC(bin_data_set.start_date_time) = TRUNC(edr_rpt_tmp_inclusion_table.date_time)
       WHERE site_to_data_source_lane_v.site_id = in_site_id
         AND bin_data.start_date_time     >= in_start_date_time - numtodsinterval(1, 'DAY')
         AND bin_data.start_date_time     <  in_end_date_time   + numtodsinterval(1, 'DAY')
         AND bin_data_set.start_date_time >= in_start_date_time
         AND bin_data_set.start_date_time <  in_end_date_time
         AND bin_data.bin_type            =  2
         AND bin_data.period_length       =  bin_period_length;
    END class_by_day_get_bin_data;In the above code I'm using the hard coded value 2 for bin type
    bin_data.bin_type            =  2But I dont want any hard coded number or string in the query.
    How could I replace it?
    I defined conatant value like below inside my package body where the actual procedure comes.But I'm not sure whether I have to declare it inside package body or inside the procedure.
    bin_type     CONSTANT NUMBER := 2;But it does't look for this value. So I'm not able to get desired value for the report .
    Thanks.
    Edited by: user10641405 on May 29, 2009 1:38 PM

    Declare the constant inside the procedure.
    PROCEDURE class_by_day_get_bin_data(in_report_parameter_id IN NUMBER,
                                        in_site_id             IN NUMBER,
                                        in_start_date_time     IN TIMESTAMP,
                                        in_end_date_time       IN TIMESTAMP,
                                        in_report_level_min    IN NUMBER,
                                        in_report_level_max    IN NUMBER) IS
      bin_period_length NUMBER(6, 0);
      v_bin_type     CONSTANT NUMBER := 2;
    BEGIN
      SELECT MAX(period_length)
        INTO bin_period_length
        FROM bin_data
        JOIN site_to_data_source_lane_v ON bin_data.data_source_id =
                                           site_to_data_source_lane_v.data_source_id
        JOIN bin_types ON bin_types.bin_type = bin_data.bin_type
       WHERE site_to_data_source_lane_v.site_id = in_site_id
         AND bin_data.start_date_time >=
             in_start_date_time - numtodsinterval(1, 'DAY')
         AND bin_data.start_date_time <
             in_end_date_time + numtodsinterval(1, 'DAY')
         AND bin_data.bin_type = v_bin_type
         AND bin_data.period_length <= 60;
      --Clear the edr_class_by_day_bin_data temporary table and populate it with the data for the requested
      --report.
      DELETE FROM edr_class_by_day_bin_data;
      INSERT INTO edr_class_by_day_bin_data
        (site_id,
         site_lane_id,
         site_direction_id,
         site_direction_name,
         bin_start_date_time,
         bin_end_date_time,
         bin_id,
         bin_value)
        SELECT site_to_data_source_lane_v.site_id,
               site_to_data_source_lane_v.site_lane_id,
               site_to_data_source_lane_v.site_direction_id,
               site_to_data_source_lane_v.site_direction_name,
               bin_data_set.start_date_time,
               bin_data_set.end_date_time,
               bin_data_value.bin_id,
               bin_data_value.bin_value
          FROM bin_data
          JOIN bin_data_set ON bin_data.bin_serial = bin_data_set.bin_serial
          JOIN bin_data_value ON bin_data_set.bin_data_set_serial =
                                 bin_data_value.bin_data_set_serial
          JOIN site_to_data_source_lane_v ON bin_data.data_source_id =
                                             site_to_data_source_lane_v.data_source_id
                                         AND bin_data_set.lane =
                                             site_to_data_source_lane_v.data_source_lane_id
          JOIN (SELECT CAST(report_parameter_value AS NUMBER) lane_id
                  FROM report_parameters
                 WHERE report_parameters.report_parameter_id =
                       in_report_parameter_id
                   AND report_parameters.report_parameter_group = 'LANE'
                   AND report_parameters.report_parameter_name = 'LANE') report_lanes ON site_to_data_source_lane_v.site_lane_id =
                                                                                         report_lanes.lane_id
          JOIN (SELECT CAST(report_parameter_value AS NUMBER) class_id
                  FROM report_parameters
                 WHERE report_parameters.report_parameter_id =
                       in_report_parameter_id
                   AND report_parameters.report_parameter_group = 'CLASS'
                   AND report_parameters.report_parameter_name = 'CLASS') report_classes ON bin_data_value.bin_id =
                                                                                            report_classes.class_id
          JOIN edr_rpt_tmp_inclusion_table ON TRUNC(bin_data_set.start_date_time) =
                                              TRUNC(edr_rpt_tmp_inclusion_table.date_time)
         WHERE site_to_data_source_lane_v.site_id = in_site_id
           AND bin_data.start_date_time >=
               in_start_date_time - numtodsinterval(1, 'DAY')
           AND bin_data.start_date_time <
               in_end_date_time + numtodsinterval(1, 'DAY')
           AND bin_data_set.start_date_time >= in_start_date_time
           AND bin_data_set.start_date_time < in_end_date_time
           AND bin_data.bin_type = v_bin_type
           AND bin_data.period_length = bin_period_length;
    END class_by_day_get_bin_data;

  • How to take max value for this senario

    Hi,
    i have table xyz in which i have column display_code
    for eg.
    display_code in table like
    1. '1100000-999'
    2. '1100000-1000'
    3. '1100000-1001'
    now i have to take max value among 3 display code. my requirement when i take max it will give me '1100000-1001' or the next value
    select max(display_code) from xyz table;
    max(display_code)
    '1100000-999'
    Please help me to fetch max(display_code) = '1100000-1001'
    thanks & regard
    shyam~

    SQL> with xyz as (
      2  select '1100000-999' display_code from dual union
      3  select '1100000-1000' from dual union
      4  select '1100000-1001' from dual
      5  )
      6  --
      7  -- actual query:
      8  --
      9  select display_code
    10  from   xyz
    11  where  to_number(substr(display_code, instr(display_code, '-')+1)) =  ( select max(to_number(substr(display_code, instr(display_code, '-')+1)))
    12                                                                          from   xyz
    13                                                                        );
    DISPLAY_CODE
    1100000-1001or
    SQL> with xyz as (
      2  select '1100000-999' display_code from dual union
      3  select '1100000-1000' from dual union
      4  select '1100000-1001' from dual
      5  )
      6  --
      7  -- actual query
      8  --
      9  select display_code
    10  from   xyz
    11  where  to_number(replace(display_code, '-')) = ( select max(to_number(replace(display_code, '-')))
    12                                                                          from   xyz
    13                                                                        );
    DISPLAY_CODE
    1100000-1001Edited by: hoek on Sep 7, 2010 5:20 PM

Maybe you are looking for

  • After upgrade to crm 2007 31 issues in genstatus

    Following an upgrade from CRM 4.0 sp 6 to CRM 2007 sp4 we are getting the following errors in genstatus.  Anu idea as to how these can be resolved.  i have not been able to find any specific information on oss or in the forums on these specific issue

  • How to encode music to Protected AAC file?

    How to encode music to Protected AAC file?

  • Yellow Tinted Documents

    Hello, We are having trouble with our hp Scanjet 7000.  When we scan colored documents, they turn out with a yellow tint and some thin black lines.  However, when we scan in pure black and white, our documents come out in crisp black and white color.

  • Color Profile Problem

    Hey, I have a question about color profiles. If you go to system preferences -> display -> color, and use "Color LCD." Now click on "calibrate," use expert mode, and click continue all the time. does the screen change at all? Mine seems to become gre

  • Is there a tablet that is compatible with aperture 3?

    I have been looking for a tablet that would be compatible with aperture 3.  I talked with someone that was familiar with macs and they didn't think there was one at all.  I really don't want to use photoshop... I'm happy with aperture.  Thanks guys!!