How to select results from column that groups results into different years

  • I have a table that stores what is essentially the value of each sale a rep makes. All the sales data from various years is stored in the same table.

    AcFeesHistory (table)

    FeeEarnerCode (varchar)

    CostsAllocated (money)

    BillDate (datetime)

    I have a query that selects the sum of CostsAllocated (billed amount) for a specified time period. But what I really want to do is to return the CostsAllocated figure for the same month over several years in order to identify general trends in sales (e.g. Feb 2014, Feb 2013, Feb 2012).

    select isnull(sum(CostsAllocated),0) as CostsThisYear

    from AcFeeHistory

    where FeeEarnerCode like 'prw' and BillDate >'2014-01-01'

    How do I form a query that selects data from the same column, but with different filtering criteria so that the results from ThisYear, LastYear, TwoYearsAgo etc appear next to each other.

    I'm looking for results that look roughly like this

    TwoYearsAgo LastYear ThisYear

    PRW 1234 1050 775

    Should the filtering to return multiple date ranges be included as part of the 'select' statement?

    select isnull(sum(CostsAllocated),0) as CostsThisYear (FILTER ON DATES SOMEWHERE HERE?)

    from AcFeeHistory

    where FeeEarnerCode like 'prw' and BillDate >'2014-01-01'

    Thanks

  • Pretty hard to tell from what you posted how to do this. I can think of 2 or 3 ways this might be done but it all depends on your actual table structure.

    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/

  • Try it out below query, You can add more conditions as per your requirement

    ;WITH Sales AS

    (

    SELECT CostsAllocated ,

    CAST(MONTH(BillDate)AS VARCHAR) +'-' +CAST (YEAR(BillDate)AS VARCHAR) AS BillDate

    FROM AcFeesHistory WHERE FeeEarnerCode like 'prw' and BillDate >'2014-01-01'

    )

    SELECT SUM(CostsAllocated),BillDate FROM Sales Group By BillDate

  • hardik.panchal (4/21/2014)


    Try it out below query, You can add more conditions as per your requirement

    ;WITH Sales AS

    (

    SELECT CostsAllocated ,

    CAST(MONTH(BillDate)AS VARCHAR) +'-' +CAST (YEAR(BillDate)AS VARCHAR) AS BillDate

    FROM AcFeesHistory WHERE FeeEarnerCode like 'prw' and BillDate >'2014-01-01'

    )

    SELECT SUM(CostsAllocated),BillDate FROM Sales Group By BillDate

    This is nowhere close to what the OP wanted for output. Where is this year? Last year?

    _______________________________________________________________

    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/

  • One other question, are we talking calendar years here?

Viewing 5 posts - 1 through 4 (of 4 total)

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