Solved

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

Posted on 2013-11-27
7
187 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

24 Experts available now in Live!

Get 1:1 Help Now