Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Finding the records in specific scenario

Author  Topic 

jdogydog23
Starting Member

3 Posts

Posted - 2010-12-01 : 17:27:46
I am new to SQL and am trying to find a way to find where specific scenario occurred. There is a loan which has two transactions: 'LOAN' & 'PAYOFF'. A 'PAYOFF' should only occur once in a transaction table unless there is a Void of that transaction. As you can see below, there were two 'PAYOFF's in a row. I need to write a query that can find other MASTER_CODEs where this happened.

I tried to do SELECT MASTER_CODE, COUNT(MASTER_CODE) FROM TRANS_TABLE WHERE TRANS_ID = 'PAYOFF' AND IS_VOIDED = 'N' HAVING COUNT(MASTER_CODE)= 2 GROUP BY MASTER_CODE, TRAN_ID, TO_CHAR(TRANS_DATE, 'MM-DD-YY') but it doesn't bring it up as there are 3 'PAYOFF' with IS_VOIDED ='N'. I only need to count the first two (omit the one after the void)

TRANS_TABLE
MASTER_CODE TRANS_CODE TRANS_ID TRANS_DATE IS_VOIDED
123456 56988 LOAN 15-NOV-10 19:41:39 N
123456 56989 PAYOFF 30-NOV-10 19:44:45 N
123456 56990 PAYOFF 30-NOV-10 19:47:45 N
123456 56991 PAYOFF 30-NOV-10 19:58:47 Y
123456 56992 PAYOFF 30-NOV-10 20:01:18 N

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2010-12-01 : 17:47:37
So Count the transactions before there was a void?


Create table TRANS_TABLE (
MASTER_CODE int not null,
TRANS_CODE int not null,
TRANS_ID char(6) not null,
TRANS_DATE datetime not null,
IS_VOIDED char(1) not null)
GO

Insert into TRANS_TABLE

Select 123456, 56988, 'LOAN','15-NOV-10 19:41:39', 'N' UNION
Select 123456, 56989, 'PAYOFF', '30-NOV-10 19:44:45', 'N' UNION
Select 123456, 56990, 'PAYOFF', '30-NOV-10 19:47:45', 'N' UNION
Select 123456, 56991, 'PAYOFF', '30-NOV-10 19:58:47', 'Y'UNION
Select 123456, 56992, 'PAYOFF', '30-NOV-10 20:01:18', 'N'

--get list of ID's applicable..
SELECT DISTINCT MASTER_CODE
FROM TRANS_TABLE
WHERE TRANS_ID = 'PAYOFF'
AND IS_VOIDED = 'N'
AND EXISTS( Select MASTER_CODE FROM TRANS_TABLE WHERE IS_VOIDED = 'Y')

-- get every record for prior to voided date
SELECT DISTINCT TRANS_TABLE.*
FROM TRANS_TABLE INNER JOIN (Select MASTER_CODE,TRANS_DATE
FROM TRANS_TABLE
WHERE IS_VOIDED ='Y') VOIDS
on TRANS_TABLE.MASTER_CODE = VOIDS.MASTER_CODE
WHERE TRANS_ID = 'PAYOFF'
AND IS_VOIDED = 'N'
AND TRANS_TABLE.TRANS_DATE < VOIDS.TRANS_DATE
AND EXISTS( Select MASTER_CODE FROM TRANS_TABLE WHERE IS_VOIDED = 'Y')

--do it as a count instead
SELECT DISTINCT TRANS_TABLE.MASTER_CODE,COUNT(*)
FROM TRANS_TABLE INNER JOIN (Select MASTER_CODE,TRANS_DATE
FROM TRANS_TABLE
WHERE IS_VOIDED ='Y') VOIDS
on TRANS_TABLE.MASTER_CODE = VOIDS.MASTER_CODE
WHERE TRANS_ID = 'PAYOFF'
AND IS_VOIDED = 'N'
AND TRANS_TABLE.TRANS_DATE < VOIDS.TRANS_DATE
AND EXISTS( Select MASTER_CODE FROM TRANS_TABLE WHERE IS_VOIDED = 'Y')
GROUP BY TRANS_TABLE.MASTER_CODE





Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

jdogydog23
Starting Member

3 Posts

Posted - 2010-12-08 : 16:56:18
First of all dataguru1971, thanks for the help. This is closer to what I am trying to find. I modified the table and added another voided record. I need the count to be 2 so that it doesn't include trans_code 56992. It should only count 56989 & 56990 so basically need it to count the 'PAYOFF' records before the first voided record


TRANS_TABLE
MASTER_CODE TRANS_CODE TRANS_ID TRANS_DATE IS_VOIDED
123456-------56988------LOAN-----15-NOV-10 19:41:39--N
123456------56989------PAYOFF----30-NOV-10 19:44:45--N
123456------56990------PAYOFF----30-NOV-10 19:47:45--N
123456------56991------PAYOFF----30-NOV-10 19:58:47--Y
123456------56992------PAYOFF----30-NOV-10 20:01:18--N
123456------56993------PAYOFF----30-NOV-10 21:00:00--Y

Thanks
Go to Top of Page

jdogydog23
Starting Member

3 Posts

Posted - 2010-12-08 : 17:11:34
Ok, I figured it out. I changed trans_date in Inner join to min(trans_date), group by master_code and voids.trans_date to voids.mintransdate. This gave exactly what I need.

SELECT DISTINCT TRANS_TABLE.*
FROM TRANS_TABLE INNER JOIN (Select MASTER_CODE,MIN(TRANS_DATE) MINTRANSDATE
FROM TRANS_TABLE
WHERE IS_VOIDED ='Y' GROUP BY MASTER_CODE) VOIDS
on TRANS_TABLE.MASTER_CODE = VOIDS.MASTER_CODE
WHERE TRANS_ID = 'PAYOFF'
AND IS_VOIDED = 'N'
AND TRANS_TABLE.TRANS_DATE < VOIDS.MINTRANSDATE
AND EXISTS( Select MASTER_CODE FROM TRANS_TABLE WHERE IS_VOIDED = 'Y')
Go to Top of Page
   

- Advertisement -