Solved

MS SQL Server Stored Procedure - NULL linked fields

Posted on 2011-09-07
7
16 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
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 to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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