[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Grouping within groups: SQL

Posted on 2016-10-11
5
Medium Priority
?
69 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

656 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