Using a table alias to identify row with max timestamp for same ID, syntax?

Hello experts
I have created an alias of a table, so now I have table T1 and its alias T2. (T2 is not joined to anything in the universe currently)
I need to identify the row from T1 with the maximum timestamp for any given ID:
ID                       Timestamp
1                         2011-01-24 16:26:00.000
1                         2011-02-24 14:21:00.000
1                         2011-02-24 13:49:00.000
I couldn't find anything on the SAP forums, but elsewhere suggested my approach should be thus:
1) Create a table alias (leave it free standing i.e. not joined) - DONE
2) For T1, create a dimension object named Timestamp - DONE
3) Create a seperate predefined condition icon funnel / filter - in the where clause:
T1.timestamp = (SELECT max(T2.timestamp) from T2 WHERE
T1.Key = T2.Key)
I'm stuck with the BO XI 3.1 syntax on step 3. I can't get it to parse.
In the where clause, mine starts with @select(T1\Timestamp) = max(@select(T2\Timestamp)
@where T1.Claim_no = T2.Claim_no)
Please can someone help me with the syntax so this thing will parse.
Many thanks in anticipation.
Eddie

Hi ,
Can you try
SELECT   ID, MAX(datetime) FROM T1 GROUP BY by ID
Thanks
Ponnarasu

Similar Messages

  • Query to get row with max values for distinct

    I have a table test with ID, ADID, MTMST columns.
    ID     ----ADID----     MTMST
    1     ----100----     24-MAR-12 08.17.09.000000 PM
    1     ----101----     24-MAR-12 08.18.15.000000 PM
    1     ----102----     24-MAR-12 08.18.56.000000 PM
    2     ----103----     24-MAR-12 08.19.21.000000 PM
    2     ----104----     24-MAR-12 08.19.36.000000 PM
    2     ----105----     24-MAR-12 08.19.46.000000 PM
    3     ----99----      22-MAR-12 09.48.22.000000 PM
    I need the rows with max ADID for each ID.
    I used the following query but it provided max ADID of the table but not the distinct ID
    select * from test where ADID in (select max(ADID) from test where id in (select distinct(id) from test where mtmst > sysdate -1))
    Result:*
    ID     ----ADID----     MTMST
    2     ----105----     24-MAR-12 08.19.46.000000 PM
    Expected result:*
    ID     ----ADID----     MTMST
    1     ----102----     24-MAR-12 08.18.56.000000 PM
    2     ----105----     24-MAR-12 08.19.46.000000 PM
    Thanks,
    Dheepan
    Edited by: Dheepan on Mar 24, 2012 9:53 AM

    select id, adid, mtmst from test where (id, adid) in (select id, max(adid) from test group by id) and MTMST>sysdate-1
    is the answer.

  • How to get the last (max of string) row with multiple records for same ID

    Hi all,
    I have a query that returns for each client ID the telephone nr, mobile phone nr, email etc... 
    There is also a column FLG_DEFAULT that indicates if the phone is the default one or not (N= changed).
    Basically what I would like my query to return is the phone number where FLG_DEFAULT = N, if this number is NULL or if FLG_DEFAULT =N does not exist I want to return the phone nr from DEFAULT_FLG = Y.
    Anyone know how I can do this in SQL? I cannot get a maximum because the format is string (e.g. +32/xxx xx xx or xxx/xx xxx xx.
    Thanks!

    ;With cte As
    (Select ClientID, PhoneNumber,
    Row_Number() Over(Partition By ClientID,
    Order By Case When FLG_DEFAULT = 'N' And PhoneNumber Is Not Null Then 1
    When DEFAULT_FLG - 'Y' Then 2 End) As rn
    From YourTableName)
    Select ClientID, PhoneNumber
    From cte
    Where rn = 1;
    Replace column names and table name with your column names and table name.
    Tom

  • BODI and automatic use of table alias

    I have a problem with BODI's automatic use of table alias as the same as the table name, when the table name is a reserved word.
    I'm reading from an Informix database, where I've got a table named aggregate. This is a reserved word and gives me problems when BODI is generating the optimized SQL for my select statement.
    The generated SQl would look like this (simplified):
    SELECT  aggregate.aggr_id
    FROM informix.aggregate aggregate
    This will fail (I'm using an odbc connection), due to syntax error.
    Running this within Aqua Studio will also fail. Changing the table alias to aggr will work fine.
    My question is how I can prevent or owerride BODI's use of aggregate as the table alias in this case.

    Have you tried the Templates?
    1. Select Tools > General Options
    2. Expand Naming Standards and select Templates
    3. Set the template structure you want to use. I used abbreviation (table) or short name(entity) here. i.e use the "Add Variable button to see the options available and add as required. In this instance I'd suggest using the abbreviation e.g. for the primary key I have {table abbr}_PK. Note that you can use "substr" here too.
    4. Apply the change and go to the model.
    Now these template work in 3 different ways.
    1. When transforming or forward engineering the logical to relational models, the templates are applied to the key creation.
    2. In the Relational diagram when drawing new relationships between tables or adding new constraints.
    3. After the fact, if you apply the templates. Either 1 at a time or globally.
    a. To update a single table: To do this select any table and invoke the property palette. Select the Naming Rules button. Check the rules you want to update and click OK. The templates are now applied to your table, renaming the keys.
    b. To update multiple tables, select the relational model mode int he Browser, right-click for the context menu and select "Apply Naming Standards" This will update all the items in the model using the template structure set. Please note, regrettably this last (3b), does not work in the latest patch release, but if you are on the initial production release, then the tables are all updated. This has been bugged and fixed and so will be rectified in the next release.
    Regards
    Sue

  • This morning I changed my Apple ID to my new email address.  I thought it meant that I could then use all my alias icloud email addresses with my new Apple ID because it would be the same account.  Why am I now not getting email to those addresses.

    This morning I changed my Apple ID to my new email address.  I thought it meant that I could continue to use all my alias icloud email addresses with my new Apple ID because it would be the same account.  Since then I have not received and new emails to my iCloud email addresses.  Normally I get at least  7 new emails a day, so I'm pretty sure my iCloud addresses are not working.  It would be a lot of work for me to notify a lot of people, companies, and organizations about new email addresses, so I really would like to make them work.  Will it be possible to make them work with my new Apple ID?

    Roger, I didn't know that "Delete Account" was synonymous with "log out".  Yes, I had deleted my iCloud account in my iPad email settings more than once yesterday and again one more time after I read your reply.  I then followed the instructions below to log off on my Windows PC.  I found these when I searched for how to log off of iCould:
    1- Go to www.iCloud.com
    2- Sign in with your current login info
    3- Click on your user name on top right corner
    4- Select account setting
    5- Select "Advanced."
    6- Click 2nd option, "Sign Out Of All Browsers."
    I also scanned settings for all the other apps on my iPad and changed a couple that still had my old sbcglobal email address. Later, I noticed that I was able to see my old previous iCloud emails in iCloud.com, and the couple of messages that I received from this Support Community site, but still no new emails.  Suddenly the emails started rolling in again this morning.  I have 7 new ones at the moment.  I sort of expected to get a backlog of emails from yesterday, but something tells me those were all lost as "undeliverable".  I don't know who or exactly what did the trick, but the issue seems to be fixed.  I'm using my new Apple ID, and I'm glad I don't have to get all new iCloud alias email addresses after all.

  • Measure using UseRelationship not working well when sliced with attributes from the same table

    Hi,
    I have Measure created using the 'UseRelationship' Function, which uses a different datekey to link to the DateDim than the one the table is directly related by. The measure works as expected except in one scenario.
    If I browse the measure using an attribute from the same fact table then the attribute is filtered using active relationship whereas the measure is filtered using the inactive relationship as shown below:
    FACT(2 rows)(Active Relationship to Date using DateKey1)
    SNo     DateKey1     DateKey2     Geo        Amt
    1         20100101     20120101    India      100
    2         20100101     20120101    US         200
    AmtMeasure:=CALCULATE(SUM([Amt]),USERELATIONSHIP(FACT[DateKey2],'Date'[DateKey]))
    If I browse the above measure in excel, with Year selected as 2012, I get 100+200=300. Now if I drag the Geo attribute against the measure I get 2 rows with 100 and 200.
    If I do the same in a power view report I don't get any results after dragging the Geo attribute, whereas I get the correct value of 300 without the geo attribute. I checked the DAX query which the power view generates and figured this is being caused because
    there are no rows in the table with DateKey1 having year 2012. I understand why this is happening this way in a DAX query and not in MDX, but shouldn't both behave in the same way and what is a work around.
    Thanks,
    Sachin Thomas

    Sac, is this still an issue?
    Thank you!
    Ed Price, Azure & Power BI Customer Program Manager (Blog,
    Small Basic,
    Wiki Ninjas,
    Wiki)
    Answer an interesting question?
    Create a wiki article about it!

  • How to join each row with other in the same table without repeating, please help

    Hi,
    I have a table say Adjustment having following data
    Emp_Id                  
    Adjustment_id                      
    Date
    1000101               
    1000300                               
    2014-02-12 00:00:00.000
    1000101               
    1000301                               
    2014-02-12 00:00:00.000
    1000101               
    1000302                               
    2014-02-12 00:00:00.000
    1000101               
    1000303                               
    2014-02-12 00:00:00.000
    1000102               
    1000302                               
    2014-02-12 00:00:00.000
    1000102               
    1000303                               
    2014-02-12 00:00:00.000
    1000102               
    1000304                               
    2014-02-12 00:00:00.000
    And I want following records:
    Emp_Id                  
    Adjustment_id      Adjustment_id1              
          Date
    1000101               
    1000300                1000301                               
    2014-02-12 00:00:00.000
    1000101               
    1000300                1000302                               
    2014-02-12 00:00:00.000
    1000101               
    1000300               
    1000303                               
    2014-02-12 00:00:00.000
    1000101               
    1000301                1000302                               
    2014-02-12 00:00:00.000
    1000101               
    1000301                1000303                               
    2014-02-12 00:00:00.000
    1000101               
    1000302                1000303                               
    2014-02-12 00:00:00.000
    1000102               
    1000302                1000303                               
    2014-02-12 00:00:00.000
    1000102               
    1000302                1000304                               
    2014-02-12 00:00:00.000
    1000102                    
    1000303               
         1000304                                       
    2014-02-12 00:00:00.000

    Hi Uri,
    I am using SQL Server 2008, and I am trying join each adjustment_id with all other adjustment_id having same Date and Emp_id.
    so for following table records:
    Emp_Id                 
    Adjustment_id                
    Date
    1000102               
    1000302                         
    2014-02-12 00:00:00
    1000102               
    1000303                         
    2014-02-12 00:00:00
     1000102                   
    1000304                               
    2014-02-12 00:00:00
    Output should be:
    Emp_Id        
    Adjustment_id      Adjustment_id     
    Date
    1000102      
    1000302               1000303              
    2014-02-12 00:00:00 (first row with 2nd)
    1000102      
    1000302               1000304              
    2014-02-12 00:00:00 (first row with 3rd)
    1000102      
    1000303               1000304              
    2014-02-12 00:00:00 (2nd row with 3rd)

  • How to copy  existing  row  value into new row  with a trigger. Same table

    Oracle guru,
    I am looking for a before or after trigger statement that will copy existing values inserted in the previous row into columns A & B. Then insert those values in a new row into column A & B if null? Same table. Hopefully my question is clear enough.
    -Oracle 10g express
    -I have an existing " before insert trigger" that insert id and timestamps when a new row is created.
    -Table is composed of column like id,timestamps,A,B and more.
    Thanks in advance
    Pierre

    957911 wrote:
    Oracle guru,
    I am looking for a before or after trigger statement that will copy existing values inserted in the previous row into columns A & B. Then insert those values in a new row into column A & B if null? Same table. Hopefully my question is clear enough.
    -Oracle 10g express
    -I have an existing " before insert trigger" that insert id and timestamps when a new row is created.
    -Table is composed of column like id,timestamps,A,B and more.
    Thanks in advance
    PierreI will call it a very Wrong design.
    It is a wrong Table Design. You are duplicating the data in table and not complying with the Database Normalization rules.
    How about Verifying if Column A & B are NULL before inserting and inserting another row and avoiding it in Triggers?
    If you are bent to achieve this, below code might be helpful. However, I would never go with this approach. If you would care about explaining the reason for going ahead with such a data model, people could suggest better alternative that might conform with Normalization rules.
    create or replace trigger trg_test_table
    after insert on test_table
    for each row
    declare
      pragma autonomous_transaction;
    begin
      if :new.col_a is null and :new.col_b is null then
        insert into test_table
        select 2, systimestamp, col_a, col_b
          from test_table
         where pk_col = (select max(pk_col) from test_table b where b.pk_col < :new.pk_col);
      end if;
      commit;
    end trg_test_table;Read SQL and PL/SQL FAQ and post the mentioned details.
    Do not forget to mention output from
    select * from v$version;

  • Find unique row with max func

    I am really stupid of this right now. Have not tried the regular SQL for long time, so my question is that I have a table- Cost
    A     B     C     D     E     F
    0002     6002     5     55     68.35     6.12
    0003     6003     5     99.26     89.33     8.23
    0004     6004     5     78.85     4.4     7.42
    0004     6004     6     78.85     8.1     1.7
    0004     6004     7     78.85     70.9     6.73
    0005     6005     5     49.88     76.1     68.53
    0005     6005     6     49.88     93.7     18.57
    0005     6005     7     49.88     255.7     63
    How can I get the column E and F, when I want to get the row with highest order in C as result in
    A     B     C     D     E     F
    0002     6002     5     55     68.35     6.12
    0003     6003     5     99.26     89.33     8.23
    0004     6004     7     78.85     70.9     6.73
    0005     6005     7     49.88     255.7     63
    I used the max () and group by in query as
    select A, B, max(C), D
    from cost
    group by A,B,D
    If I added the max(E) and max(F) in the SELECT, I got correct row in E but not the F.
    Please help me out. Thanks.

    an alternative using analytic fiunctions would be:
    select "A","B",max("C"),"D","E","F" from
    (select "A","B","C",last_value("D") over (partition by "A","B" order by
    "A","B","C" rows between unbounded preceding and unbounded following) "D",
    last_value("E") over (partition by "A","B" order by
    "A","B","C" rows between unbounded preceding and unbounded following) "E",
    last_value("F") over (partition by "A","B" order by
    "A","B","C" rows between unbounded preceding and unbounded following) "F"
    from test1)
    group by "A","B","D","E","F"
    order by "A","B"
    this appears to give a better plan on the test data but you would want to test with a larger volume, we have reduced to numbet of buffer gets but added two sorts currently these fit in memory but you may find with a larger dataset these result in disk sorts.
    Chris
    Autotrace output:
    SQL> select * from test1 where "A"||B||"C" in (select "A"||B||Z from (select "A", B, max("C") Z from test1 group by "A",B));
    A    B             C          D          E          F
    0002 6002          5         55      68.35       6.12
    0003 6003          5      99.26      89.33       8.23
    0004 6004          7      78.85       70.9       6.73
    0005 6005          7      49.88      255.7         63
    Execution Plan
    Plan hash value: 2706139598
    | Id  | Operation             | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT      |          |     1 |    50 |     8  (25)| 00:00:01 |
    |*  1 |  HASH JOIN            |          |     1 |    50 |     8  (25)| 00:00:01 |
    |   2 |   TABLE ACCESS FULL   | TEST1    |     8 |   192 |     3   (0)| 00:00:01 |
    |   3 |   VIEW                | VW_NSO_1 |     8 |   208 |     4  (25)| 00:00:01 |
    |   4 |    HASH UNIQUE        |          |     8 |   104 |     4  (25)| 00:00:01 |
    |   5 |     HASH GROUP BY     |          |     8 |   104 |     4  (25)| 00:00:01 |
    |   6 |      TABLE ACCESS FULL| TEST1    |     8 |   104 |     3   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - access("$nso_col_1"="A"||"B"||TO_CHAR("C"))
    Statistics
              0  recursive calls
              0  db block gets
             64  consistent gets
              0  physical reads
              0  redo size
            795  bytes sent via SQL*Net to client
            381  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              4  rows processed
    SQL>
    SQL> select "A","B",max("C"),"D","E","F" from
      2  (select  "A","B","C",last_value("D") over (partition by "A","B" order by
      3  "A","B","C" rows between unbounded preceding and unbounded following) "D",
      4  last_value("E") over (partition by "A","B" order by
      5  "A","B","C" rows between unbounded preceding and unbounded following) "E",
      6  last_value("F") over (partition by "A","B" order by
      7  "A","B","C" rows between unbounded preceding and unbounded following) "F"
      8  from test1)
      9  group by "A","B","D","E","F"
    10  order by "A","B";
    A    B      MAX("C")          D          E          F
    0002 6002          5         55      68.35       6.12
    0003 6003          5      99.26      89.33       8.23
    0004 6004          7      78.85       70.9       6.73
    0005 6005          7      49.88      255.7         63
    Execution Plan
    Plan hash value: 3359524756
    | Id  | Operation            | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT     |       |     8 |   496 |     5  (40)| 00:00:01 |
    |   1 |  SORT GROUP BY       |       |     8 |   496 |     5  (40)| 00:00:01 |
    |   2 |   VIEW               |       |     8 |   496 |     4  (25)| 00:00:01 |
    |   3 |    WINDOW SORT       |       |     8 |   192 |     4  (25)| 00:00:01 |
    |   4 |     TABLE ACCESS FULL| TEST1 |     8 |   192 |     3   (0)| 00:00:01 |
    Statistics
              0  recursive calls
              0  db block gets
              7  consistent gets
              0  physical reads
              0  redo size
            802  bytes sent via SQL*Net to client
            381  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              2  sorts (memory)
              0  sorts (disk)
              4  rows processed
    SQL>

  • Row with max date

    Hi guys,
    Quick question which I would appreciate some help with. I am currently querying a table with multiple rows but I only want it to return 1 row (the one which was created most recently) ie. the one with the max date_Created field.
    I was thinking I could do this:
    Select *
    from table
    where date_created = max(date_created)
    but you will probably know I cant do this. I know I can partition etc but since I am calling this as a cursor from oracle forms I cant use partition, can anyone help me out please?
    Thanks.

    Hi,
    Here's one way:
    WITH     got_r_num     AS
         SELECT     t.*
         ,     ROW_NUMBER () OVER (ORDER BY  date_created  DESC)
                  AS r_num
         FROM    table_x  t
    SELECT     *   -- or list all columns except r_num
    FROM     got_r_num
    WHERE     r_num     = 1
    786733 wrote:Hi guys,
    Quick question which I would appreciate some help with. I am currently querying a table with multiple rows but I only want it to return 1 row (the one which was created most recently) ie. the one with the max date_Created field.
    I was thinking I could do this:
    Select *
    from table
    where date_created = max(date_created)
    but you will probably know I cant do this. You can do something pretty close:
    SELECT     *
    FROM     table_x
    WHERE     date_created = (
                         SELECT  MAX (date_created)
                      FROM        table_x
    ;However, if there is a tie (that is, 2 or more rows that have exactly the same date_created, and none later), then this will return all of them, and you said you only want 1 row.
    I know I can partition etc but since I am calling this as a cursor from oracle forms I cant use partition, can anyone help me out please?Sorry, I don;t undersand what you're saying about partitions.
    If there's something that you can do in, say, SQL*Plus, but not in Forms, then hre's probably a way to "hide" it (in a view, perhaps) so that the part Forms can't hande isn't being done in Forms.

  • "System error: Move error" on comparing DATE rows with literal timestamps

    say, I have a table consisting of a DATE and a TIMESTAMP column:
      create table test (
      d date,
      ts timestamp
    now the problem:
    when I try to retrieve values using a statement like this (using MaxDB's SQL Studio or via JDBC):
      select * from test
      where  d >= {ts '2007-06-18 12:19:45'}
    it produces the following error:
      General error;-9111 POS(36) System error: Move error
    So, I am not able to compare DATE columns with timestamp values?
    In contrast, the following combinations all work flawlessly:
      select * from test
      where  d >= {ts '2007-06-18 12:19:45'}
      select * from test
      where ts >= {d '2007-06-18'}
      select * from test
      where  ts >= {ts '2007-06-18 12:19:45'}
    Thus, the opposite direction (comparing a TIMESTAMP column to a literal date value) apparently poses no problem to the MaxDB database engine.
    Unfortunately, I cannot just resort to "truncating" the timestamp values and using the {d ...}-Syntax, because I am using Apache's Torque object-relational mapper which generates the statement like this and feeds the JDBC API with it. Anyway, I deem this a bug in MaxDB, especially with respect to the quite obscure error message.
    I can reproduce this issue on both MaxDB 7.5 and 7.6 server, using client software (SQL Studio), version 7.5 and 7.6 as well.
    Does anybody know this problem?
    TIA,
    Alex

    Hi,
    this is a new bug and we will fix it with one of the next versions of MaxDB.
    You can watch the proceeding in our internal bug tracking system:
    http://www.sapdb.org/webpts?wptsdetail=yes&ErrorType=0&ErrorID=1151609
    Thank you for reporting it.
    Kind regards
    Holger

  • Problem with gettigs Timestamp for a FTP files

    Hi All,
    I try to get a files from FTP location to my location Pc using enterprisedt.net.ftp.FTPCient. I need to get the fides from FTP location and copy that files in local with original timestamp.
    ex.
    client.get(localfilenamepath,remotefilename);
    Date ftpdate= client .modtime(remoteFilename);
    after coping file to my local pc,i need to set the original timestamp of the remote file.
    localfile.setLastModified(ftpdate.getTime);
    in that case,i cannot get the original time but the date will updated correct.
    appreciate if anyone give me a solution.
    Thanks
    Message was edited by:
    mayen

    After setting lastmodified time,the date only updated with original but the time is bit differnt (i.e) its updated with some time different
    for ex,
    FTP File shows>> 30/05/2006 1:39PM
    after coping in local file>> 30/05/2006 5:39 AM.
    The original ftp file's time is update with different one.
    How do i set the time also the same one.
    Message was edited by:
    mayen
    Message was edited by:
    mayen

  • Pb detection two cameras GigE with MAX on the same pc

    I installed on the same pc two GigE camera, each camera has its own interface card  (IP), but only one camera in dtected with max.
    sometimes cam0 and sometimes cam1.I have to carry out has each camera a fixed address IP but always only camera is detected.
    my question is how to make the configuration to appear both camera at the same time with max.
    I use the MAX version 5.3
    NI IMAQDX 4.0
    Think you very match for your help.
    Hamd2015
    Solved!
    Go to Solution.

    Hi Hamden,
    Could we get a little more information about your setup as it will help narrow down the issue?
    - What network interface cards (NIC) are you using?
    - What cameras are you using?
    - Are we running the cameras through a network or directly connecting them to the computer?
    - Can we see each camera when plugged in individually?
    - It sounds like you said that you are using a static IP address, but I wanted to confirm this.
    - Can we see both NICs in Windows Device Manger?
    Bill E. | Applications Engineer | National Instruments

  • Update multiple rows with datas from the same table

    i have a table like
    name version value1 value2
    2 A 4,31 3,5
    3 A 3,45 10
    2 B 6,97 12
    4 B 12 16
    so name + version is unique
    i have to update the datas value1 and value2 ( version A) with the datas from version B where the name ( VersionA) = name version B
    i.e the result should be
    name version value1 value2
    2 A 6,97 12
    3 A 3,45 10
    2 B 6,97 12
    4 B 12 16
    is it possible to do this in sql? ( sql. 8.1)

    ... if your table does not contain exactly what you expect you could get:
    'single row sub-query returns more than one row'
    To prevent that, put a unique index on (name, version). If this combination is not always unique, you will need to cater for the possibility in the update statement.

  • SQL subquery returning too many rows with Max function

    Hello, I hope someone can help me, I been working on this all day. I need to get max value, and the date and id where that max value is associated with between specific date ranges. Here is my code , and I have tried many different version but it still returning
    more than one ID and date
    Thanks in advance
    SELECT
      distinctbw_s.id, 
    avs.carProd,cd_s.RecordDate,
    cd_s.milkProductionasMilkProd,
    cd_s.WaterProductionasWaterProd
    FROMtblTestbw_s
    INNERJOINtblTestCpcd_sWITH(NOLOCK)
    ONbw_s.id=cd_s.id   
    ANDcd_s.recorddateBETWEEN'08/06/2014'AND'10/05/2014'
    InnerJoin
    (selectid,max(CarVol)ascarProd
    fromtblTestCp
    whererecorddateBETWEEN'08/06/2014'AND'10/05/2014'
     groupby 
    id)avs
    onavs.id=bw_s.id
    id RecordDate carProd       MilkProd WaterProd
    47790 2014-10-05   132155   0 225
    47790 2014-10-01   13444    0 0
    47790 2014-08-06   132111    10 100
    47790 2014-09-05   10000    500 145
    47790 2014-09-20   10000    800 500
    47791 2014-09-20   10000    300 500
    47791 2014-09-21   10001    400 500
    47791 2014-08-21   20001    600 500
    And the result should be ( max carprod)
    id RecordDate carProd       MilkProd WaterProd
    47790 2014-10-05   132155  0 225
    47791 2014-08-21   20001    600 500

    Help your readers help you.  Remember that we cannot see your screen, do not know your data, do not understand your schema, and cannot test a query without a complete script.  So - remove the derived table (to which you gave the alias "avs")
    and the associated columns from your query.  Does that generate the correct results?  I have my doubts since you say "too many" and the derived table will generate a single row per ID.  That suggests that your join between the first
    2 tables is the source of the problem.  In addition, the use of DISTINCT is generally a sign that the query logic is incorrect, that there is a schema issue, or that there is a misunderstanding of the schema. 

Maybe you are looking for