Pick recent modified date in the table

  • Hi,

    This is my table:

    ID AppName DepCode DepName Group ModifiedDate YearlyAmount

    1 Nestle NS Foods Products 01/12/14 451

    1 Nestle NS Foods Products 01/17/14 495

    2 Oracle OR Software Info 01/24/14 279

    2 Oracle OR Soft & IT Info 01/26/14 310

    2 Oracle ORL Software Info 01/25/14 219

    2 Oracle ORL Soft IT 01/28/14 600

    MonthlyAmount Funded AppCategory Research

    37.5623 Yes NE NA

    41.2365 No N NA

    23.2568 Yes OR InProgress

    25.8333 Yes ORL NA

    18.2189 Yes SOF Approved

    50.0000 No IT Rejected

    Expected Output:

    ID AppName DepCode DepName Group ModifiedDate YearlyAmount

    1 Nestle NS Foods Products 01/17/14 946

    2 Oracle OR Soft & IT Info 01/26/14 589

    2 Oracle ORL Soft IT 01/28/14 819

    MonthlyAmount Funded AppCategory Research

    78.7988 No N NA

    49.0901 Yes ORL NA

    68.2189 No IT Rejected

    I want to pick the recent modified date for DepCode and sum Yearly and Monthly Amount. I have tried this query and not able to get the output. This is the single table. I am not able to get the DepCode called OR in my expected output

    select B1.[ID], B1.[AppName], B1.[DepCode], B1.[DepName], B1.[Group],

    B2.ModifiedDate, B2.YearlyAmount, B2.MonthlyAmount,

    B1.[FuBded], B1.[AppCategory], B1.[Research]

    FROM Business B1

    INNER JOIN

    (select [ID], MAX(ModifiedDate) as ModifiedDate, SUM(YearlyAmount) as YearlyAmount,

    SUM(MonthlyAmount) as MonthlyAmount

    from Business

    Group by ID) B2

    ON B1.ID = B2.ID AND B1.ModifiedDate = B2.ModifiedDate

    Anyone please correct me or advice how to solve this as I'm a starter in SQL.

  • In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply