?
Solved

Grouping within groups: SQL

Posted on 2016-10-11
5
Medium Priority
?
66 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
[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
5 Comments
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 2000 total points
ID: 41839430
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 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 41839435
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
ID: 41839443
Thanks for the tips on my using the code block; I will apply them going forward.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41839451
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

765 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