Solved

Grouping within groups: SQL

Posted on 2016-10-11
5
36 Views
Last Modified: 2016-10-11
I have the data in Exhibit 1 of the attachment which I get by running the query shown below. However, what I am looking to do is to get my data to look like Exhibit 2 of the attached so that I only retain the records with the later Mod_Date for every Loan_ID by setting row1=1. Can anyone please offer their input? Any help would be extremely appreciated. Many thanks in advance.
 
select  t.Loan_ID, t.Mod_Option, t.Mod_Date, t.Loan_UPB, t.row1,
            row_number() over(partition by Mod_Option) as row2
from(select  Loan_ID, Mod_Option, Mod_Date, Loan_UPB,
            row_number() over(partition by Loan_ID order by a.Mod_Date) as row1
           from Exhibit1) t
Example.docx
0
Comment
Question by:maroulator
  • 3
5 Comments
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 500 total points
Comment Utility
using copy/paste only, into a "code block" you get this:
Exhibit 1:
Loan_ID  Mod_Option    Mod_Date     Loan_UPB   row1  row2
1341               3                  10/2/2011       123,876       1          1
1341               3                  10/2/2011       123,876       2          2
1341               3                  10/2/2011       123,531       3          3
1341               3                  10/2/2011       123,531       4          4
1341               4                     9/2/2011      123,355       5          1
1341               4                     9/2/2011      123,355       6          2
3211               2                     3/4/2014      115,780       1          1
3211               2                     3/4/2014      115,780       2          2
3211               2                     3/4/2014      115,780       3          3
3211               1                     2/3/2014      115,520       4          1
3211               1                     2/3/2014      115,520       5          2 

Exhibit 2:
Loan_ID  Mod_Option    Mod_Date     Loan_UPB   row1  
1341               3                  10/2/2011       123,876       1          
1341               3                  10/2/2011       123,876       1          
1341               3                  10/2/2011       123,531       1          
1341               3                  10/2/2011       123,531       1          
1341               4                     9/2/2011      123,355       2          
1341               4                     9/2/2011      123,355       2          
3211               2                     3/4/2014      115,780       1          
3211               2                     3/4/2014      115,780       1          
3211               2                     3/4/2014      115,780       1          
3211               1                     2/3/2014      115,520       2          
3211               1                     2/3/2014      115,520       2          

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
You need RANK() or DENSE_RANK() instead of row_number()

The RANK/DENSE_RANK functions will allow repeating a number (e.g. 4 rows of each with 1) but row_number simply will not do that.

SELECT
        t.Loan_ID
      , t.Mod_Option
      , t.Mod_Date
      , t.Loan_UPB
      , t.row1
FROM (SELECT
                Loan_ID
              , Mod_Option
              , Mod_Date
              , Loan_UPB
              , DENSE_RANK() OVER (PARTITION BY Loan_ID ORDER BY CAST(a.Mod_Date as date) DESC) AS row1
        FROM Exhibit1
     ) t

Open in new window

Note I have used CAST(... as date)  to ensure you are dealing with dates and not dates with time, if that column's data is already date then the cast is not necessary.
0
 

Author Closing Comment

by:maroulator
Comment Utility
Thanks for the tips on my using the code block; I will apply them going forward.
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
Thanks, using embedded images or code blocks mean we can read the whole question without jumping all over the place and downloading files from lots of questions.
0

Featured Post

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!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

744 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

21 Experts available now in Live!

Get 1:1 Help Now