Aggregation of same data with different conditions - Help please!

sql server Iran
  • 11 years ago

    Hi all.

    I've got a set of data which has a userid, a numeric value, and a date associated.

    I need to aggregate the numeric values by user and by yearly quarter (current and next).  That is, I'm trying to produce a result set which is grouped by the userid, and has 6 other columns; the 3 months of the current quarter and the 3 months of next.

    I'm struggling to work out how to get this view.  For the current date it'd be something like:

    Column 1: User ID
    Column 2: Sum of Values, Where associated Date is in January (assuming January can be identified by various date functions - I've already got this done).
    Column 3: Sum of Values, Where associated Date is in February.
    Column 4: Sum of Values for March.
    Column 5: Sum of Values for April.
    Column 6: Sum of Values for May.
    Column 7: Sum of Values for June.

    Can anyone suggest how I could achieve this?  Assume I've got variables to identify months, quarters, etc.  So pseudo-code responses are fine :)  Thanks.

  • 11 years ago

    UPDATE:  I've found a solution now using switches.  Thanks to anyone who pondered this thread!

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The greatest performance improvement of all is when a system goes from not-working to working.” - John Ousterhout