473,385 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SQL for Query Referring to Itself

PhilOfWalton
1,430 Expert 1GB
I need to write some SQL to be used with VBA where a query refers to itself.

Not very clear I know, so here is the finished result for Query21.
I am trying to get the value of CountTables.



This is the SQL for Query21
Expand|Select|Wrap|Line Numbers
  1. SELECT TblPSysAllFields.TableName, TblPSysAllFields.FieldName, 
  2. TblPSysAllFields.InputTable, TblPSysAllFields.InputField, 
  3. DCount("TableName","Query21","TableName = '" & [TableName] & "'") AS CountTables
  4. FROM TblPSysAllFields
  5. WHERE (((TblPSysAllFields.FieldName) Is Not Null) 
  6. AND ((TblPSysAllFields.InputTable) Is Not Null) AND ((TblPSysAllFields.Level)=1) 
  7. AND ((TblPSysAllFields.IsPrimary) Not Like "*P*"))
  8. ORDER BY TblPSysAllFields.FieldName, TblPSysAllFields.TableName;
So the problem is that the SQL refers to Query21, but I don't want Query 21 to be a saved Query because in the code, I will be changing the value of "Level"

Is there a way round this please?

Phil
Attached Images
File Type: png Query21.png (21.3 KB, 276 views)
Oct 24 '18 #1

✓ answered by twinnyfo

Phil,

Try using a subquery. You would just have to make sure to change "Level" in each section.

Expand|Select|Wrap|Line Numbers
  1. SELECT TblPSysAllFields.TableName, 
  2.        TblPSysAllFields.FieldName, 
  3.        TblPSysAllFields.InputTable, 
  4.        TblPSysAllFields.InputField, 
  5.        tblSubTables.CountTables
  6. FROM TblPSysAllFields 
  7. INNER JOIN (SELECT TblPSysAllFields.TableName, 
  8.                    Count(TblPSysAllFields.TableName) AS CountTables
  9.             FROM TblPSysAllFields
  10.             WHERE (((TblPSysAllFields.FieldName) Is Not Null) 
  11.               AND ((TblPSysAllFields.InputTable) Is Not Null) 
  12.               AND ((TblPSysAllFields.Level)=1) 
  13.               AND ((TblPSysAllFields.IsPrimary) Not Like "*P*"))
  14.             GROUP BY TblPSysAllFields.TableName) AS tblSubTables 
  15. ON TblPSysAllFields.TableName = tblSubTables.TableName
  16. WHERE (((TblPSysAllFields.FieldName) Is Not Null) 
  17.   AND ((TblPSysAllFields.InputTable) Is Not Null) 
  18.   AND ((TblPSysAllFields.Level)=1) 
  19.   AND ((TblPSysAllFields.IsPrimary) Not Like "*P*"))
  20. ORDER BY TblPSysAllFields.FieldName, 
  21.          TblPSysAllFields.TableName;
Hope this hepps!

4 1179
twinnyfo
3,653 Expert Mod 2GB
Phil,

Try using a subquery. You would just have to make sure to change "Level" in each section.

Expand|Select|Wrap|Line Numbers
  1. SELECT TblPSysAllFields.TableName, 
  2.        TblPSysAllFields.FieldName, 
  3.        TblPSysAllFields.InputTable, 
  4.        TblPSysAllFields.InputField, 
  5.        tblSubTables.CountTables
  6. FROM TblPSysAllFields 
  7. INNER JOIN (SELECT TblPSysAllFields.TableName, 
  8.                    Count(TblPSysAllFields.TableName) AS CountTables
  9.             FROM TblPSysAllFields
  10.             WHERE (((TblPSysAllFields.FieldName) Is Not Null) 
  11.               AND ((TblPSysAllFields.InputTable) Is Not Null) 
  12.               AND ((TblPSysAllFields.Level)=1) 
  13.               AND ((TblPSysAllFields.IsPrimary) Not Like "*P*"))
  14.             GROUP BY TblPSysAllFields.TableName) AS tblSubTables 
  15. ON TblPSysAllFields.TableName = tblSubTables.TableName
  16. WHERE (((TblPSysAllFields.FieldName) Is Not Null) 
  17.   AND ((TblPSysAllFields.InputTable) Is Not Null) 
  18.   AND ((TblPSysAllFields.Level)=1) 
  19.   AND ((TblPSysAllFields.IsPrimary) Not Like "*P*"))
  20. ORDER BY TblPSysAllFields.FieldName, 
  21.          TblPSysAllFields.TableName;
Hope this hepps!
Oct 24 '18 #2
PhilOfWalton
1,430 Expert 1GB
Thanks Twinnyfo

Other than a small typo error, it worked perfectly. I must admit that among my many Access weeknesses, SubQueries is high on the list.

Grateful thanks

Phil
Oct 24 '18 #3
twinnyfo
3,653 Expert Mod 2GB
I think I learned everything I know about Sub-Queries here on Bytes!
Oct 24 '18 #4
NeoPa
32,556 Expert Mod 16PB
Subqueries in SQL may help.
Oct 24 '18 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Minitman | last post by:
I have an address database and I want to have the query prompt for me to enter an expression that will bring up ALL names begining with same first ltr for individual page on my report. I am...
6
by: Nicolae Fieraru | last post by:
Hi All, I have a query, Select Count(BoolField) from tblMyTable, Where BoolField = true. If I run the query by itself, it returns the number of true records I want to use the result of that...
3
by: Nicolae Fieraru | last post by:
Hi All, I have a table, tblCustomers, with fields SalutationID and Firstname. I made a query, qrySelect = "Select FirstName from tblCustomers Where SalutationID = " If I run this query by...
2
by: dio | last post by:
Hi there, I'm trying to create an OleDBDataAdapter with a command that will receive a parameter to act as a filter on the query. I'm using the Query Builder wizard and it works fine, unless I...
1
by: Dave | last post by:
I have a page with a button that updates a table in the database. This should affect the data displayed on the page. I've used break points to ensure that the code calling the query to collect...
4
by: pmacdiddie | last post by:
I have an append query that needs to run every time a line item is added to a subform. The append writes to a table that is the source for a pull down box. Problem is that it takes 5 seconds to...
3
by: Kevin M | last post by:
I have one table and have created a form from that table. Also, I have created a delete query. I want to but a button on the form which will delete all records from the table; however, I cannot get...
3
by: Richard Hollenbeck | last post by:
I am very sorry about the (almost) re-post, but you will see that my first question wasn't very clear; I have another question I posted this morning called, "in DAO: Run time error 3061 Too few...
0
by: Del | last post by:
Hello and thanks for any and all assistance! I have a database that is used by several users on several different machines. The backend database is housed on a file server. Each user has a...
19
by: phill86 | last post by:
Hi I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread. here is the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.