Solved

SQL Select Statement

Posted on 2010-11-30
5
277 Views
Last Modified: 2012-05-10
My attached select statement will produce (example) the following output
You'll notice that there can be multiple compounding methods for a deal.
The loan date will always be the same for a dealID

What I need is ONE record per deal.  

This is my current output
DealID     cp           ld
8001       Annual   01/01/2011
8002       Annual   01/01/2012
8002       Monthly  01/01/2012
8003       Monthly  01/01/2013

This is what I need.
Notice...that anything with more than one compounding method outputs "Split"

DealID     cp           ld
8001       Annual   01/01/2011
8002       Split        01/01/2012
8003       Monthly  01/01/2013
Select		DealID, compoundingMethod cp, loanDate  ld

	from	P_PaymentStreams

Group by	DealID, compoundingMethod, loanDate

Open in new window

0
Comment
Question by:lrbrister
5 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 500 total points
ID: 34239001
Try this:
Select		

	DealID

	,CASE WHEN COUNT(compoundingMethod) > 1 THEN 'Split' ELSE MIN(compoundingMethod) END cp

	,MAX(loanDate)  ld

from	P_PaymentStreams

Group by	

DealID

Open in new window

0
 
LVL 7

Expert Comment

by:luceysupport
ID: 34239024
start by creating a table variable to hold your data, add an extra column for count

first insert into this table variable the dealid and id and the count, grouping by both.

Then update the cp column to be split where count > 1

Update the cp column to be the actual value of cp where count =1
0
 

Author Closing Comment

by:lrbrister
ID: 34239076
Perfect...thanks.
0
 
LVL 18

Expert Comment

by:deighton
ID: 34239120
Erick37 is on to someting, but you might want to do

Select            
      DealID
      ,CASE WHEN COUNT(distinct compoundingMethod) > 1 THEN 'Split' ELSE MIN(compoundingMethod) END cp
      ,MAX(loanDate)  ld
from      P_PaymentStreams
Group by      
DealID




so that it does not split if they are all the same
0
 

Author Comment

by:lrbrister
ID: 34239271
deighton:
You're absolutely right. Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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 …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

757 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

22 Experts available now in Live!

Get 1:1 Help Now