Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find Specific Value Within Groups

Posted on 2014-01-31
6
Medium Priority
?
195 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
[X]
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
6 Comments
 
LVL 60

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

0
 

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'.
0
 
LVL 60

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.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:Donovan Moore
ID: 39825107
Thanks!
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 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
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
ID: 39825182
Both excellent examples.  I really appreciate the help!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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