Solved

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

Posted on 2013-11-27
7
186 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 121
Need Counts 11 43
SQL Query Syntax Error 9 34
Export data with an insert trigger to a linked server 20 21
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

760 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

23 Experts available now in Live!

Get 1:1 Help Now