Find Specific Value Within Groups

Posted on 2014-01-31
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.
Question by:Donovan Moore
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 59

Expert Comment

by:Kevin Cross
ID: 39825008
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


Author Comment

by:Donovan Moore
ID: 39825067
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'.
LVL 59

Expert Comment

by:Kevin Cross
ID: 39825087
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.
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!


Author Comment

by:Donovan Moore
ID: 39825107
LVL 59

Accepted Solution

Kevin Cross earned 500 total points
ID: 39825150
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
    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
    SELECT MonthNumberOfYear, BillingQuarter, CalendarSemester
         , RN = ROW_NUMBER() 
             OVER(PARTITION BY BillingQuarter
                  ORDER BY CalendarSemester, MonthNumberOfYear DESC)
    FROM your_table
) derived

Open in new window

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

Author Closing Comment

by:Donovan Moore
ID: 39825182
Both excellent examples.  I really appreciate the help!

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sql Server group by 10 51
How come this XML node is not read? 3 31
Database Mail Profiles 1 22
Can a Trigger trigger a Trigger? 4 22
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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