Results 1 to 2 of 2

Thread: Last year

  1. #1
    Join Date
    May 2008
    Posts
    1

    Unanswered: Last year

    Hello
    I have a SQL server model with Bill and BillLine, where the second references the first

    Bill(CodBill, Date, ...)
    BillLine(CodBillLine,CodBill,Description, ..., Total)

    What I want is a list of sum(total) grouped by month and description and also a list of last year sum(total).

    Example:

    Month | Description | Total this year |Total last year
    1 | Water | 5.00 | 3.00
    1 | Blood | 0.00 | 4.50
    1 |Milk | 2.00 | 0.00
    2 |Blood | 1.00 | 3.50
    2 |Milk | 7.80 | 2.30
    3 |... |... |...
    ....
    12 |... |... |...

    This is easy to do without the 4 th column using GROUP BY, but this column complicates all the thing. I was trying to do things with the PIVOT operator, but it was a useless effort.

    Please help.

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Modify your query for the last 2 years like this
    select Month, Description
    , Total this year=sum(case when year... then total else 0 end)
    , Total last year=sum(case when year... then total else 0 end)
    ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •