Solved

MS SQL Server Stored Procedure - NULL linked fields

Posted on 2011-09-07
7
13 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
7 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 250 total points
Comment Utility
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
Comment Utility
Change the join from a left join to a join.
0
 
LVL 4

Assisted Solution

by:nfaria
nfaria earned 250 total points
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 28

Expert Comment

by:strickdd
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

763 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

12 Experts available now in Live!

Get 1:1 Help Now