Solved

Grouping within groups: SQL

Posted on 2016-10-11
5
56 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
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 48

Accepted Solution

by:
PortletPaul earned 500 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 48

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell v3 - SQLCMD 3 26
how to just get time from a date 6 31
SQL Select in Access 2003 3 20
What is the best way to use power bi and ssrs 3 18
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

791 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