473,847 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is a LEFT JOIN with a subquery possible?

I have a list of towns in tblTownData, and a list of people's names in
tblNames. During a year, there are multiple events where more names are
added in tblNames, for a particular town.

ie:
TOWNID EVENTID NAME
1 1 John Doe
1 1 Jane Doe
1 2 Bell E. Dancer
1 2 Frank N. Furter

For each event, I am trying to give feedback to the user as to which towns
have not had a single name added yet. That is, I am trying to generate a
list of towns using a left join on a subquery.

In Access, I can create a query (tblCurrentName sInTown):
SELECT tblNames.TownID
FROM tblNames
WHERE (((tblNames.ELE CTIONID)=1))

then a new query (tblTownData Without Matching Current):
SELECT tblTownData.Mun icipality
FROM tblTownData LEFT JOIN tblCurrentNames InTown ON tblTownData.Tow nID =
tblCurrentNames InTown.TownID
WHERE (((tblCurrentNa mesInTown.TownI D) Is Null));

This will return my list fine.

However, I need to call this query result from the database using a SQL
statement inside an ASP page.

Can you create a LEFT JOIN to the subquery (tblCurrentName sInTown)?
If so, can anyone help with the syntax?
Dave
Nov 13 '05 #1
3 22443

Dave wrote:
I have a list of towns in tblTownData, and a list of people's names in
tblNames. During a year, there are multiple events where more names are
added in tblNames, for a particular town.
...


Did you consider doing something simpler like:

SELECT Municipality FROM tblTownData WHERE TownID NOT IN (SELECT
DISTINCT TownID FROM tblNames WHERE EVENTID = 1);

Of course, if the number of records in tblNames is fairly large the
'NOT IN' syntax becomes inefficient and could explain the route you've
chosen.

James A. Fortune

Nov 13 '05 #2
Jim's suggestion is excellent. However, if you must use the join, you
can do the following:

SELECT tblTownData.Mun icipality
FROM tblTownData LEFT JOIN
(SELECT tblNames.TownID
FROM tblNames
WHERE (((tblNames.ELE CTIONID)=1))) AS tblCurrentNames InTown
ON tblTownData.Tow nID =
tblCurrentNames InTown.TownID
WHERE (((tblCurrentNa mesInTown.TownI D) Is Null));

Bill E.
Hollywood, FL

Nov 13 '05 #3
>I have a list of towns in tblTownData, and a list of people's names in
tblNames. During a year, there are multiple events where more names are
added in tblNames, for a particular town.


James and Bill,

Thanks for the feedback. This seemed to work well. I'm next going to load
the database with roughly the same number of records as I would expect over
several events, and determine which way to go.

Thanks for saving my sanity!

Dave
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
9037
by: Thomas Beutin | last post by:
Hi, i've a speed problem withe the following statement: SELECT DISTINCT pz.l1_id, pz.l2_id, pz.l3_id, pz.l4_id FROM ot_adresse AS a, ot_produkt AS p LEFT OUTER JOIN ot_kat_prod AS pz ON ( p.p_id = pz.p_id ) WHERE p.a_id = a.id AND a.id = '105391105424941' AND a.m_id = '37'; This is terrible slow compared to the inner join: SELECT DISTINCT pz.l1_id, pz.l2_id, pz.l3_id, pz.l4_id
7
2917
by: stabbert | last post by:
I am attempting to join two tables together on two different unix servers. Here is some relevant info about the tables. TABLE 1 Setup ----------------------- DB2 UDB 7.2 EE
23
82062
by: Brian | last post by:
Hello All - I am wondering if anyone has any thoughts on which is better from a performance perspective: a nested Select statement or an Inner Join. For example, I could do either of the following: SELECT supplier_name FROM supplier
7
9354
by: deko | last post by:
SELECT tblTxAcct.TxAcctName, tblTxType.TxTypeName, Nz(qryTxAcctListCt.TxCount, 0) AS TxCt FROM (tblTxAcct INNER JOIN tblTxType ON tblTxAcct.TxType_ID=tblTxType.TxType_ID) LEFT JOIN qryTxAcctListCt ON tblTxAcct.TxAcct_ID=qryTxAcctListCt.TxAcct_ID; I use this query as a RecordSource for a Datasheet that displays Accounts, Types, and the number of Transactions in each Account. An Account cannot exist outside of a Type, but an Account...
14
6112
by: Ryan | last post by:
I want to do the following SQL statement in Access. However, it won't allow me to have the secondary part of my join statement and tells me that this is not supported. OK, so Access doesn't support it, but there must be an alternative. I can add the part 'D.Dealer_Code' to the where clause, but because of the way that the statement is executed, it excludes data that I want. My SQL statement is perfectly leagal and works fine, but Access...
14
5739
by: mike | last post by:
I'm using postgresl 7.3.2 and have a query that executes very slowly. There are 2 tables: Item and LogEvent. ItemID (an int4) is the primary key of Item, and is also a field in LogEvent. Some ItemIDs in LogEvent do not correspond to ItemIDs in Item, and periodically we need to purge the non-matching ItemIDs from LogEvent. The query is: delete from LogEvent where EventType != 'i' and ItemID in
2
8442
by: lelandhuang | last post by:
I am developing reporting service and using lots of 'LEFT OUTER JOIN', I am worried about the performance and want to use some subquery to improve the performance. Could I do that like below, SELECT * FROM TableA LEFT OUTER JOIN TableB
6
51619
by: Avaenuha | last post by:
I need to use a left outer join to get all of one table, and match it to specific instances of another table. Eg, report all of A, and where A has made a specific kind of B, report the name of that B. Now, to get the specific B, I need to join three other relations together, and then do a string match. That's easy. I can report the subset of A that has made specific kind of B. I can report all of A. I can report all of A that has made all...
4
2721
by: polycom | last post by:
Assistance needed to optimize this query SELECT SD.content_id AS Id, SD.title AS Title, CT.name AS Contenttype, PV.content_id AS SponsorId, PV.display_name AS Sponsor, CONCAT_WS("", NT.title, " - ", N.pubdate, " ") AS ListGroup,
0
9730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10984
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10650
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10708
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7056
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5912
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4531
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3164
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.