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

Database query with subselect

Hi there,

I've got a simple table with an ID and a parentID field which build the relation
between the entries. Now I want to select some entries and count their childs.

This is what I tried:

SELECT NAME, ID, parentID,
(SELECT COUNT(ID) FROM myTable WHERE parentID=???ID???)
AS CHILDS FROM myTable WHERE parentID=0 ORDER BY parentID ASC

My problem is how to tell oracle to use the ID of the current entry in the
main query, not the ID field in the subselect.

Can anybody help me?

Thanks,
Theodor
Jul 19 '05 #1
2 11906
th*************@hotmail.com (Theodor Ramisch) wrote in message news:<12*************************@posting.google.c om>...
Hi there,

I've got a simple table with an ID and a parentID field which build the relation
between the entries. Now I want to select some entries and count their childs.

This is what I tried:

SELECT NAME, ID, parentID,
(SELECT COUNT(ID) FROM myTable WHERE parentID=???ID???)
AS CHILDS FROM myTable WHERE parentID=0 ORDER BY parentID ASC

My problem is how to tell oracle to use the ID of the current entry in the
main query, not the ID field in the subselect.

Can anybody help me?

Thanks, ================================================== ===
Use the aliases thr
like FOM myTable A etc
and then use A.ID in select

it will work Theodor

Jul 19 '05 #2
am***********@zensar.com (Amit K) wrote in message news:<2b**************************@posting.google. com>...
th*************@hotmail.com (Theodor Ramisch) wrote in message news:<12*************************@posting.google.c om>...
Hi there,

I've got a simple table with an ID and a parentID field which build the relation
between the entries. Now I want to select some entries and count their childs.

This is what I tried:

SELECT NAME, ID, parentID,
(SELECT COUNT(ID) FROM myTable WHERE parentID=???ID???)
AS CHILDS FROM myTable WHERE parentID=0 ORDER BY parentID ASC

My problem is how to tell oracle to use the ID of the current entry in the
main query, not the ID field in the subselect.

Can anybody help me?

Thanks,

================================================== ===
Use the aliases thr
like FOM myTable A etc
and then use A.ID in select

it will work
Theodor

Why not just do this...

Select n.name, count(p.id) as childs
from myTable p,
myTable n
where p.parent_id in ( ?, ?, ? ) <-- if you want to filter
and n.id = p.parent_id
group by n.name
order by n.name asc
Jul 19 '05 #3

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

Similar topics

6
by: Dave | last post by:
To build a grid, all the distinct rows from T1 are required, and only those from T2 which fall btn 2003-09-11 and 2003-09-18. In the following example: A is included because it is within the date...
3
by: krystoffff | last post by:
Hi I would like to paginate the results of a query on several pages. So I use a query with a limit X offset Y to display X results on a page, ok. But for the first page, I need to run the...
5
by: James Foreman | last post by:
I've got lots of different tables, each pertaining to a different marketing campaign. I have another table that stores the name & schema of each of these tables, together with other information...
2
by: Todd | last post by:
Apologies up front if this is completely confusing. Currently, I'm working on a query that resembles this: Select A, B, C, MIND, E2.E, F2.F From (Select A, B, C, MIN(D) As MIND From TableABC...
5
by: Todd | last post by:
Data related to the query I'm working on is structured such that TableA and TableB are 1-many(optional). If an item on TableA has children on TableB, I need to use the Max(tstamp) from Table B in...
7
by: Patrick Fisher | last post by:
Hi I have a table which Contains entries with RefCode field containing INVP or INVPD Common fields in each entry would be InvoiceNo, Total and PurTyp for example. You could have ...
6
by: Sebastien | last post by:
I have the following statement which I run successfully in... 1 hour 10 minutes. SELECT a.tsgicd as ACCT_ID, a.tsa5cd as SEC_ID, CASE WHEN (SUBSTRING(a.tsgicd, 6, 1) = 'R' or...
4
by: ungahz | last post by:
hi all. im having a problem and wondering if anyone an help. i have the following tables along with column names (too many columns so im just including the relevent ones): folders ---------...
4
by: Richard | last post by:
In a manufacturing document control situation, a procedure revision table named has principal columns and and satellite data columns , , . A unique index and to ensure each procedure name has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.