Solved

Find Specific Value Within Groups

Posted on 2014-01-31
6
172 Views
Last Modified: 2014-01-31
I need to find a specific value based on two different values in a table.

The table is a date dimension within an Analysis cube; I am attempting to find the last month within each quarter.  Here is some sample data:

MonthNumberOfYear  BillingQuarter  CalendarSemester
1                  1               1
2                  1               1
12                 1               2
3                  2               1
4                  2               1
5                  2               1
6                  3               1
7                  3               2
8                  3               2
9                  4               2
10                 4               2
11                 4               2

Open in new window



The issue lies with the fact that December (MonthNumberOfYear) is equal to 12; trying to do a MAX() on the MonthNumberOfYear and including the BillingQuarter implies that December is the last month, when in reality, February (2) is the last month.

My thought was to find both the MIN(CalendarSemester) and then MAX(MonthNumberOfYear), but am having issues making this work correctly.

What I need in the end is:

MonthNumberOfYear  BillingQuarter
2                  1
5                  2
8                  3
11                 4

Open in new window



Any and all help would be greatly appreciated.  Please let me know if there are any questions.

I'm using SQL 2008 R2.
0
Comment
Question by:Donovan Moore
  • 3
  • 3
6 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
In T-SQL, one simple way to get the results you have is to GROUP BY BillingQuarter, then SELECT MAX(MonthNumberOfYear).

SELECT MonthNumberOfYear = MAX(MonthNumberOfYear)
     , BillingQuarter
FROM your_table
GROUP BY BillingQuarter
;

Open in new window

0
 

Author Comment

by:Donovan Moore
Comment Utility
Thanks - but the issue is that December becomes the last month in the quarter, but the way the billing is setup as a December through February quarter.  So, February is the last month in the quarter.  This is why I included using the CalendarSemester as an additional field; finding the minimum CalendarSemester value within the BillingQuarter group would theoretically find February as the maximum MonthNumberOfYear where CalendarYear is equal to '1'.
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Yes, sorry.  I read that and then thought I was over-complicating it.
Okay, you can do this in a couple steps, I will post an example shortly.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Donovan Moore
Comment Utility
Thanks!
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
One way is to use similar GROUP BY statement, but add a filter to make sure that you always get the row with the lowest CalendarSemester by BillingQuarter.
SELECT MonthNumberOfYear = MAX(MonthNumberOfYear)
     , BillingQuarter
FROM your_table a
WHERE NOT EXISTS (
    SELECT 1
    FROM your_table b
    WHERE b.BillingQuarter = a.BillingQuarter
    AND b.CalendarSemester < a.CalendarSemester
)
GROUP BY BillingQuarter
;

Open in new window


Another approach uses windowing function to rank records based on the CalendarSemester and MonthNumberOfYear.  With the correct ORDER BY, the latest month for each BillingQuarter becomes RN = 1.
SELECT MonthNumberOfYear, BillingQuarter, CalendarSemester
FROM (
    SELECT MonthNumberOfYear, BillingQuarter, CalendarSemester
         , RN = ROW_NUMBER() 
             OVER(PARTITION BY BillingQuarter
                  ORDER BY CalendarSemester, MonthNumberOfYear DESC)
    FROM your_table
) derived
WHERE RN = 1
;

Open in new window


There likely are many other ways, but I hope these help.
0
 

Author Closing Comment

by:Donovan Moore
Comment Utility
Both excellent examples.  I really appreciate the help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now