We help IT Professionals succeed at work.
Get Started

Need assistance expanding TSQL query

Last Modified: 2012-05-05
I am using the attached query to pull records from 2 tables in SQL Server.  I need to expand this query (or at least the filter) so that amount1=paidtous OR amount1=sum(paidtous WHERE date is in range and account# is the same).  I have no idea how to perform the appropriate join to build this section.  Any help is appreciated.
ALTER PROCEDURE dbo.getRecords
	@GetFound int = 1
declare @rec table( invoiceNumber nvarchar(40), debtor nvarchar(80),
					accountNumber nvarchar(40), paidToUs numeric(18,2), 
					GST numeric(10,2),ourDate nvarchar(10))
insert into @rec SELECT DISTINCT OurRecords.invoiceNumber, OurRecords.debtor, 
						OurRecords.AccountNumber, SUM(OurRecords.paidToUs), 
						SUM(OurRecords.GST), ourDate From OurRecords 
						invoiceNumber, debtor, AccountNumber, OurDate
if @getFound=1
                      OurRecords.fileName, OurRecords.invoiceNumber, OurRecords.debtor, OurRecords.AccountNumber, OurRecords.paidToUs, OurRecords.dueToUs, 
                      OurRecords.GST, OurRecords.ourDate, ExcelData.DateAgency, ExcelData.DateDirect
FROM         ExcelData INNER JOIN
                      OurRecords ON ExcelData.Account = OurRecords.AccountNumber AND ExcelData.Amount1 = OurRecords.paidToUs AND 
                      ExcelData.Amount3 = OurRecords.dueToUs  And ExcelData.DateAgency<=OurRecords.ourDate ANd ExcelData.DateDirect>=OurRecords.ourDate
	SELECT DISTINCT  Debtor, Account, Amount1 AS PaidToUs, Amount3 AS DueToUs, Amount4 AS GST, DateAgency, DateDirect
	FROM         ExcelData
	WHERE     (id NOT IN
	                          (SELECT DISTINCT XD.id
	                            FROM          ExcelData AS XD INNER JOIN
	                                                   OurRecords AS OD ON XD.Account = OD.AccountNumber 
	                                                   AND XD.Amount1 = OD.paidToUs
	                                                   AND XD.Amount3 = OD.dueToUs 
	                                                   AND (XD.DateAgency<=OD.OurDate AND XD.DateDirect>=OD.OurDate)))

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 25 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE