SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Data Analysis


Data Analysis

Author
Message
mcmu4256
mcmu4256
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 15
Please help! I have the query below. The output is as follows:
ProcessDate--------------AccountNumber--------------CloseDate-----------Mem_years--------------credit_loan----chk_acct
20180208-----------------------323314------------------------NULL--------------------23------------------------500------------------X
20180208-----------------------323314------------------------NULL--------------------23-----------------------NULL----------------X

But I just want it to return:
ProcessDate--------------AccountNumber--------------CloseDate-----------Mem_years--------------credit_loan-------chk_acct
20180208-----------------------323314------------------------NULL--------------------23------------------------500------------------X

So some accounts have a 'Null' credit_loan, and that is fine, but if an account does have an amount in there, I only want to see that amount, I don't need to see both the 'Null' and '500' records.

Here is my query:
SELECT DISTINCT
TOP (100) PERCENT dbo.ACCOUNT.ProcessDate
, dbo.ACCOUNT.ACCOUNTNUMBER
, dbo.ACCOUNT.CLOSEDATE
, DATEDIFF(year, dbo.ACCOUNT.OPENDATE
, GETDATE() - 1080) AS Mem_Years
, CASE WHEN loan.TYPE = 77 AND LOAN.CLOSEDATE IS NULL
AND LOAN.CHARGEOFFDATE IS NULL
THEN loan.originalbalance END AS credit_loan
, CASE WHEN SAVINGS.SHARECODE = 1 AND SAVINGS.CLOSEDATE IS NULL THEN 'x' ELSE NULL
END AS chk_acct
FROM dbo.LOAN RIGHT OUTER JOIN
dbo.SAVINGS ON dbo.LOAN.PARENTACCOUNT = dbo.SAVINGS.PARENTACCOUNT
AND dbo.LOAN.ProcessDate = dbo.SAVINGS.ProcessDate
LEFT OUTER JOIN
dbo.ACCOUNT ON dbo.SAVINGS.ProcessDate = dbo.ACCOUNT.ProcessDate
AND dbo.SAVINGS.PARENTACCOUNT = dbo.ACCOUNT.ACCOUNTNUMBER
WHERE (dbo.ACCOUNT.ProcessDate = CAST(CONVERT(VARCHAR(8), GETDATE() - 1, 112) AS INT))
AND (dbo.ACCOUNT.CLOSEDATE IS NULL) AND
(dbo.ACCOUNT.OPENDATE < GETDATE() - 1080)
AND (dbo.LOAN.ORIGINALBALANCE IS NOT NULL)
AND (dbo.SAVINGS.SHARECODE = 1)


ORDER BY
dbo.ACCOUNT.ACCOUNTNUMBER


Any help would be greatly appreciated!

Thanks.

drew.allen
drew.allen
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40015 Visits: 14483
Use a CTE with a ROW_NUMBER() to return only those records with a ROW_NUMBER() = 1.

Since you haven't provided sample data to work with, I can't provide you tested code.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
J Livingston SQL
J Livingston SQL
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29096 Visits: 41715
maybe read this https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum

and repost....?

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

mcmu4256
mcmu4256
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 15
Does this help? Just a small sample of the tables.

Loan Table:
ProcessDate-------------ParentAccount-----------CloseDate-------------ChargeOffDate----------------OriginalBalance---------------Type
20180208-------------------0000012430-------------NULL--------------------NULL------------------------------NULL---------------------------77
20180208-------------------0000017770-------------NULL--------------------NULL------------------------------NULL---------------------------77
20180208-------------------0000017770-------------NULL--------------------NULL------------------------------500-----------------------------77

Savings Table:
ProcessDate------------ParentAccount-------------ShareCode-------------CloseDate
20180208-----------------0000012430---------------------1------------------------NULL
20180208-----------------0000017770---------------------1------------------------NULL
20180208-----------------0000017770---------------------1------------------------NULL

Account Table:
ProcessDate--------------AccountNumber------------CloseDate
20180208--------------------0000012430------------------NULL
20180208--------------------0000017770------------------NULL
drew.allen
drew.allen
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40015 Visits: 14483
mcmu4256 - Friday, February 9, 2018 2:38 PM
Does this help? Just a small sample of the tables.

Loan Table:
ProcessDate-------------ParentAccount-----------CloseDate-------------ChargeOffDate----------------OriginalBalance---------------Type
20180208-------------------0000012430-------------NULL--------------------NULL------------------------------NULL---------------------------77
20180208-------------------0000017770-------------NULL--------------------NULL------------------------------NULL---------------------------77
20180208-------------------0000017770-------------NULL--------------------NULL------------------------------500-----------------------------77

Savings Table:
ProcessDate------------ParentAccount-------------ShareCode-------------CloseDate
20180208-----------------0000012430---------------------1------------------------NULL
20180208-----------------0000017770---------------------1------------------------NULL
20180208-----------------0000017770---------------------1------------------------NULL

Account Table:
ProcessDate--------------AccountNumber------------CloseDate
20180208--------------------0000012430------------------NULL
20180208--------------------0000017770------------------NULL

No, that's a document, not data. J. Livingston posted a link on how to post data and I also have a link in my signature.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
george-178499
george-178499
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 318
mcmu4256 - Friday, February 9, 2018 1:15 PM
Please help! I have the query below. The output is as follows:
ProcessDate--------------AccountNumber--------------CloseDate-----------Mem_years--------------credit_loan----chk_acct
20180208-----------------------323314------------------------NULL--------------------23------------------------500------------------X
20180208-----------------------323314------------------------NULL--------------------23-----------------------NULL----------------X

But I just want it to return:
ProcessDate--------------AccountNumber--------------CloseDate-----------Mem_years--------------credit_loan-------chk_acct
20180208-----------------------323314------------------------NULL--------------------23------------------------500------------------X

So some accounts have a 'Null' credit_loan, and that is fine, but if an account does have an amount in there, I only want to see that amount, I don't need to see both the 'Null' and '500' records.

Here is my query:
SELECT DISTINCT
TOP (100) PERCENT dbo.ACCOUNT.ProcessDate
, dbo.ACCOUNT.ACCOUNTNUMBER
, dbo.ACCOUNT.CLOSEDATE
, DATEDIFF(year, dbo.ACCOUNT.OPENDATE
, GETDATE() - 1080) AS Mem_Years
, CASE WHEN loan.TYPE = 77 AND LOAN.CLOSEDATE IS NULL
AND LOAN.CHARGEOFFDATE IS NULL
THEN loan.originalbalance END AS credit_loan
, CASE WHEN SAVINGS.SHARECODE = 1 AND SAVINGS.CLOSEDATE IS NULL THEN 'x' ELSE NULL
END AS chk_acct
FROM dbo.LOAN RIGHT OUTER JOIN
dbo.SAVINGS ON dbo.LOAN.PARENTACCOUNT = dbo.SAVINGS.PARENTACCOUNT
AND dbo.LOAN.ProcessDate = dbo.SAVINGS.ProcessDate
LEFT OUTER JOIN
dbo.ACCOUNT ON dbo.SAVINGS.ProcessDate = dbo.ACCOUNT.ProcessDate
AND dbo.SAVINGS.PARENTACCOUNT = dbo.ACCOUNT.ACCOUNTNUMBER
WHERE (dbo.ACCOUNT.ProcessDate = CAST(CONVERT(VARCHAR(8), GETDATE() - 1, 112) AS INT))
AND (dbo.ACCOUNT.CLOSEDATE IS NULL) AND
(dbo.ACCOUNT.OPENDATE < GETDATE() - 1080)
AND (dbo.LOAN.ORIGINALBALANCE IS NOT NULL)
AND (dbo.SAVINGS.SHARECODE = 1)


ORDER BY
dbo.ACCOUNT.ACCOUNTNUMBER


Any help would be greatly appreciated!

Thanks.
The query you are using seems to be doing an outer join which results in duplicating rows and eventually getting rid of the duplicates by using a DISTINCT top 100 percent?. Follow the principle of "Filter Early, Join Late".

Since you are only interested in getting the results of loan.type=77 and loan.closedate is null and loan.chargeoffdate is null
why not filter out those records from the dbo.LOAN table first followed by the the rest of the join conditions

Another point is that you have a condition which does an outer join between savings and loan table.
Therefore any non matching records from that result set would contain NULLS for dbo.LOAN


However the below condition negates the RIGHT OUTER JOIN whereby you are asserting to get you records where the fields in dbo.loan is NOT NULL

Use SQL Formatting for readability.
Haven't tested this since you havent supplied the create scripts and DML but guessing this should work


SELECT DISTINCT TOP (100) PERCENT
dbo.ACCOUNT.ProcessDate,
dbo.ACCOUNT.ACCOUNTNUMBER,
dbo.ACCOUNT.CLOSEDATE,
DATEDIFF(YEAR, dbo.ACCOUNT.OPENDATE, GETDATE() - 1080) AS Mem_Years,
/*CASE
WHEN loan.TYPE = 77
AND LOAN.CLOSEDATE IS NULL
AND LOAN.CHARGEOFFDATE IS NULL THEN loan.originalbalance
END AS credit_loan,*/
loan_1.originalbalance as credit_loan
CASE
WHEN SAVINGS.SHARECODE = 1
AND SAVINGS.CLOSEDATE IS NULL THEN 'x'
ELSE NULL
END AS chk_acct
FROM (SELECT *
FROM dbo.LOAN
WHERE loan.TYPE = 77 /* Added this condition as you are only interested in those records from LOAN with a NOT NULL credit_loan*/
AND LOAN.CLOSEDATE IS NULL
AND LOAN.CHARGEOFFDATE IS NULL
) as loan_1
RIGHT OUTER JOIN dbo.SAVINGS
ON loan_1.PARENTACCOUNT = dbo.SAVINGS.PARENTACCOUNT
AND loan_1.ProcessDate = dbo.SAVINGS.ProcessDate
LEFT OUTER JOIN dbo.ACCOUNT
ON dbo.SAVINGS.ProcessDate = dbo.ACCOUNT.ProcessDate
AND dbo.SAVINGS.PARENTACCOUNT = dbo.ACCOUNT.ACCOUNTNUMBER
WHERE (dbo.ACCOUNT.ProcessDate = CAST(CONVERT(VARCHAR(8), GETDATE() - 1, 112) AS INT))
AND (dbo.ACCOUNT.CLOSEDATE IS NULL)
AND (dbo.ACCOUNT.OPENDATE < GETDATE() - 1080)
AND (loan_1.ORIGINALBALANCE IS NOT NULL) /* This condition negates the RIGHT OUTER JOIN as outer joined records would be NULL and the condition says get only not NULL values*/
AND (dbo.SAVINGS.SHARECODE = 1)
ORDER BY dbo.ACCOUNT.ACCOUNTNUMBER

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search