473,414 Members | 1,707 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,414 software developers and data experts.

two nested select statements

JS
Let's say I have two tables:

t1
myname
apple
banana
cherry

t2
myname | value
apple | 1
banana | 2
apple | 3
banana | 4
cherry | 5
I want to do one query and get back the data like this:

+--------+-----------+
| myname | all_value |
+--------+-----------+
| apple | 1,3 |
| banana | 2,5 |
| cherry | 4 |
+--------+-----------+
In my head, it seems like the query should look like:

select t1.name, (select t2.value from t2 where t2.name=t1.name) as
all_value from t1;

But that just doesn't work. Is this possible without resorting to
code?

Thanks
Nov 13 '05 #1
2 4222
jd***@hotmail.com (JS) wrote in
news:b0**************************@posting.google.c om:
Let's say I have two tables:

t1
myname
apple
banana
cherry

t2
myname | value
apple | 1
banana | 2
apple | 3
banana | 4
cherry | 5
I want to do one query and get back the data like this:

+--------+-----------+
| myname | all_value |
+--------+-----------+
| apple | 1,3 |
| banana | 2,5 |
| cherry | 4 |
+--------+-----------+
In my head, it seems like the query should look like:

select t1.name, (select t2.value from t2 where
t2.name=t1.name) as all_value from t1;

But that just doesn't work. Is this possible without
resorting to code?

Thanks


Moving each value to a comma delimited list will require code. If
you can live with

+--------+-----------+
| myname | all_value |
+--------+-----------+
| apple | 1 |
| apple | 3 |
| banana | 2 |
| banana | 5 |
| cherry | 4 |
| lemon | 0 |

try
SELECT t1.name,nz(t2.value,0) AS all_value
FROM t1 LEFT JOIN t2 ON t1.name=t2.name
ORDER BY t1.name,t2.value.

If the query is to feed a report, you can set up the detail section
to print multiple columns, giving the appearance you want.

If you really want it in the query as a comma delimited list,
you'll need a function that creates the list. Note to MicroSoft:
there really should be a domain aggregate function that does that.

Bob Quintal
Nov 13 '05 #2
This will do it:
http://www.mvps.org/access/modules/mdl0004.htm
"Return a concatenated list of sub-record values"
Nov 13 '05 #3

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

Similar topics

2
by: Jamie Townsend | last post by:
Hi. I'm having trouble filtering some nested selects. Can anyone tell me why these two statements (below) don't return the same results? The second example works as expected, but the first...
2
by: Forgone Conclusion | last post by:
Hi, I have a View that groups and sums up totals. This View is then nested within in another View and used (it needs to be done like this). What i need to do is to be able to vary the records...
5
by: Matt | last post by:
I am working in a project where the business model is complex enough that many common retrieval functions become difficult to develop and maintain in single query statements or functions. I have...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
2
by: Jim Irvine | last post by:
Does anybody know the limit of nested iif statements you can use in Access?
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
1
by: sql_er | last post by:
Hi all, I am trying to convert an SQL statement into an XPath (or a sequence of XPath) statements. More specifically, I have the following: SELECT a FROM b WHERE c IN (SELECT d FROM e) I...
6
by: duke442games | last post by:
Hi Gang I am working on fixing a corrupted database (A yearly proc did not handel the conversion from year to year and now I have to fix the resulting lines in the DB). Most of my fixes I have...
2
by: pradeep.thekkottil | last post by:
I'm setting up an auction website using PHP and MySQL. There in the section where logged in members can put up new auction in a form, I want to run a form validation where I used if else statements...
4
by: Patrick A | last post by:
All, I rely on nested IF statements with multiple conditions heavily, and someone suggested recently writing the statements (and especially reading them months later) would be much easier if I...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...

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.