Solved

MS SQL Server Stored Procedure - NULL linked fields

Posted on 2011-09-07
7
14 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

867 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

15 Experts available now in Live!

Get 1:1 Help Now