?
Solved

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

Posted on 2013-11-27
7
Medium Priority
?
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 48

Expert Comment

by:Dale Fye
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 48

Expert Comment

by:Dale Fye
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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 

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 48

Accepted Solution

by:
Dale Fye earned 2000 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

770 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