Solved

MS SQL Server Stored Procedure - NULL linked fields

Posted on 2011-09-07
7
17 Views
Last Modified: 2016-06-16
I have a stored procedure that is linked (see below) between the ciKey field and the tClientInvoice field.  The problem is that there are cases where the tClientInvoice field is NULL but the ciKey field has a value.  I'm not sure how SQL Server is joining the tables where ciKey and tClient Invoice are, in fact, NOT equal.  Can anyone help clarify this or suggest a way to rewrite this part of the stored procedure so I don't get this type of return?  Any help would be greatly appreciated.

UPDATE   dbo.ConsultantPayWhenPaidReport
SET       ciConsultantReceipts            = ISNULL(tcdConsultantReceipts,0)
         ,ciConsultantReceivableBalance   = ISNULL(tcdConsultantReceivableBalance,0)
         ,prjConsultantWriteoff           = ISNULL(ConsultantWriteoff,0)
         ,rReceiptDate                     = ISNULL(rDate,NULL)
		 ,TotalPaidByClient               =ISNULL(CASE WHEN rDate IS NOT NULL THEN viInvoiceAmount END,0)
FROM dbo.ConsultantPayWhenPaidReport
   LEFT JOIN (
				SELECT     dbo.AxTransaction.tClientInvoice, dbo.AxReceipt.rDate, SUM(CASE WHEN rType = 0 AND gldType IN (8, 13) THEN - gldAmount END) 
									  AS tcdConsultantReceipts, SUM(dbo.AxGLDetail.gldAmount) AS tcdConsultantReceivableBalance, - SUM(CASE WHEN rType = 2 THEN gldAmount END) 
									  AS ConsultantWriteoff
				FROM         dbo.AxTransaction LEFT OUTER JOIN
									  dbo.AxGLDetail ON dbo.AxGLDetail.gldTransaction = dbo.AxTransaction.tKey LEFT OUTER JOIN
									  dbo.AxReceipt ON dbo.AxGLDetail.gldReceipt = dbo.AxReceipt.rKey
				WHERE     (dbo.AxTransaction.tActivityType = 3) AND (dbo.AxGLDetail.gldControlAccountType = 2)
				GROUP BY dbo.AxTransaction.tClientInvoice, dbo.AxReceipt.rDate
         )SUB ON SUB.tClientInvoice = ciKey

Open in new window


As an example of the join values where ciKey has a value but tClientInvoice doesn't, see below:
ciKey      tClientInvoice
3996      NULL
4909      NULL
4755      NULL
4755      NULL
2425      NULL
4542      NULL
5007      NULL
4974      NULL
4328      NULL
4498      NULL
4964      NULL
4224      NULL
4224      NULL
4215      NULL
4215      NULL
4967      NULL
4551      NULL
4951      NULL
5043      NULL
145      NULL
0
Comment
Question by:lunow160
[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
7 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 250 total points
ID: 36495855
Because you are doing a LEFT JOIN. Change it to a JOIN and all those nulls will disappear.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 36495864
Change the join from a left join to a join.
0
 
LVL 4

Assisted Solution

by:nfaria
nfaria earned 250 total points
ID: 36496590
LEFT JOIN is telling to return all records that are in dbo.ConsultantPayWhenPaidReport even if whithout match in SUB by SUB.tClientInvoice = ciKey

RIGHT JOIN would do the opposite returning all records from SUB

Remove LEFT As told and it will return only those whith matches.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:strickdd
ID: 36497013
nfaria, thanks for reiterating what has already been stated! It really solidifies the previous answers and their correctness!
0
 
LVL 4

Expert Comment

by:nfaria
ID: 36497114
the other answers are correct but showed only the solution and not why it was happening. Just trying to clarify the why. No points need!
0
 
LVL 9

Expert Comment

by:Moussa Mokhtari
ID: 41656170
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

749 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