SQL Select Statement

Posted on 2010-11-30
Medium Priority
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

Question by:lrbrister
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
LVL 32

Accepted Solution

Erick37 earned 2000 total points
ID: 34239001
Try this:
	,CASE WHEN COUNT(compoundingMethod) > 1 THEN 'Split' ELSE MIN(compoundingMethod) END cp
	,MAX(loanDate)  ld
from	P_PaymentStreams
Group by	

Open in new window


Expert Comment

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

Author Closing Comment

ID: 34239076
LVL 18

Expert Comment

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

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

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

Author Comment

ID: 34239271
You're absolutely right. Thanks

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

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