• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

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

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
deborahhowson00
Asked:
deborahhowson00
  • 4
  • 3
2 Solutions
 
Dale FyeCommented:
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
 
deborahhowson00Author Commented:
Thanks, but where do I put my 'WHERE' statements for each query?
0
 
Dale FyeCommented:
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
deborahhowson00Author Commented:
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
 
Dale FyeCommented:
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
 
deborahhowson00Author Commented:
The startdate to transdate is correct for the data we are wanting to return on this report.  Thanks for all your help.
0
 
deborahhowson00Author Commented:
Figured it out myself however returns two acc codes and place refs so best using the experts methods.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now