Solved

Nested Query, Link two separate queries together on more than one field

Posted on 2013-11-27
7
188 Views
Last Modified: 2013-12-03
Hi,

I have two queries, the first pulls through all the Water charges from a view:-

select PlaceRef, Accountcode, StartDate, EndDate, NetAmt
FROM vwPlaceChargesCurrent
WHERE AccountCode = 'WATER'

the second pulls through all the transactions from a two views:

select TransactionWeek, TransactionDate, TransactionAmount, AccountCode, PlaceRef, TenancyRef
FROM vwTenancyTransactionsCurrent
  INNER JOIN vwTenancyCurrent
    ON vwTenancyTransactionsCurrent.TenancySystemRef = vwTenancyCurrent.TenancySystemRef
WHERE AccountCode='DEBIT' and TransactionWeek='1'


I would like to join both of these queries together on PlaceRef and StartDate from the first query, MATCHING the PlaceRef and TransactionDate from the second query.

Thanks in advance for your help!

Deborah
0
Comment
Question by:deborahhowson00
  • 4
  • 3
7 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39680629
Well, the standard syntax applies:

SELECT qry1.F
select qry1.PlaceRef, qry1.Accountcode, qry1.StartDate, qry1.EndDate, qry1.NetAmt,
           qry2.TransactionWeek, qry2.TransactionDate, qry2.TransactionAmount,qry2.TenancyRef
FROM qry1
INNER JOIN qry2
ON qry1.PlaceRef = qry2.PlaceRef
AND qry1.Accountcode = qry2.Accountcode

If you want all records from qry1 and only the matching records from qry2, change "INNER JOIN" to "LEFT JOIN"
0
 

Author Comment

by:deborahhowson00
ID: 39680713
Thanks, but where do I put my 'WHERE' statements for each query?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39680755
Actually, now that I look closer, you mentioned join on PlaceRef and TransactionDate.

What date in query 1 would you want to join to Transaction date, since query1 contains two date values?

The other question is that in Query1, your where clause says:

WHERE AccountCode = 'WATER'

But the WHERE clause in query2 is:

WHERE AccountCode='DEBIT' and TransactionWeek='1'

Do you want to make these queries as parameter querys, where you pass the values at run-time, or are they static?  If static, you would save each of the queries and replace "qry1" with the name you use to save your first query and do the same for qry2 and your second query.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Assisted Solution

by:deborahhowson00
deborahhowson00 earned 0 total points
ID: 39680784
i've done it as follows:-

SELECT * FROM
(select A.TransactionWeek, A.TransactionDate, A.TransactionAmount, A.AccountCode, PlaceRef, TenancyRef
FROM vwTenancyTransactionsCurrent A INNER JOIN vwTenancyCurrent
    ON A.TenancySystemRef = vwTenancyCurrent.TenancySystemRef
WHERE AccountCode='DEBIT' and TransactionWeek='1') AA
INNER JOIN
(select B.PlaceRef, B.Accountcode, B.StartDate, B.EndDate, B.NetAmt
FROM vwPlaceChargesCurrent B
WHERE AccountCode = 'WATER') BB
ON AA.PlaceRef=BB.PlaceRef AND AA.TransactionDate=BB.StartDate

Works... does that look OK to you?
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 39680970
Yes, but I would probably not use the * because you will have redundant fields (PlaceRef and StartDate/TransactionDate).  No sense in returning more fields than you need.

I'm concerned about your joining on the TransDate and StartDate.  In my experience, transactions occur every day, not just on the StartDate of some date range.

You might want to try:

SELECT BB.*, AA.* FROM
(select A.TransactionWeek, A.TransactionDate, A.TransactionAmount, A.AccountCode, PlaceRef, TenancyRef
FROM vwTenancyTransactionsCurrent A INNER JOIN vwTenancyCurrent
    ON A.TenancySystemRef = vwTenancyCurrent.TenancySystemRef
WHERE AccountCode='DEBIT' and TransactionWeek='1') AA
INNER JOIN
(select B.PlaceRef, B.Accountcode, B.StartDate, B.EndDate, B.NetAmt
FROM vwPlaceChargesCurrent B
WHERE AccountCode = 'WATER') BB
ON AA.PlaceRef=BB.PlaceRef
AND AA.TransactionDate BETWEEN BB.StartDate AND BB.EndDate
0
 

Author Comment

by:deborahhowson00
ID: 39683486
The startdate to transdate is correct for the data we are wanting to return on this report.  Thanks for all your help.
0
 

Author Closing Comment

by:deborahhowson00
ID: 39692023
Figured it out myself however returns two acc codes and place refs so best using the experts methods.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
t-sql Joining Issue 10 40
[SQL server / powershell] bulk delete table from CSV 8 33
SQL server vNext 18 29
sql server query 6 9
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

810 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