Solved

Complicated Select Statement

Posted on 2011-03-03
6
225 Views
Last Modified: 2012-05-11
I have a table that has 3 columns
id  parentid  tText
1     NULL     Lottery
2       1             SubText
3        1           MoreText
4       Null        Structured
5         4           SMenu
I need to have a select statement tat gives me the number of records that a parent (id where parentid=null) has

So my select based on the data above would be

id     tText       numRecords
1      Lottery         2
4      Structured    1
 

0
Comment
Question by:lrbrister
6 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
ID: 35028841
select t1.id, t1.ttext, count(*) numRecords from tbl t1 inner join tbl t2 on t1.id = t2.parentid
group by t1.id, t1.ttext
0
 
LVL 9

Expert Comment

by:joshbula
ID: 35028862
SELECT tText, COUNT(*) AS numRecords FROM table
WHERE parentid is NULL
GROUP BY tText
0
 
LVL 4

Expert Comment

by:MarioAlcaide
ID: 35028890
SELECT a.ID, a.TTEXT, b.COUNT(*) FROM YOUR_TABLE_NAME a, YOUR_TABLE_NAME b WHERE PARENTID IS NULL AND a.ID = b.PARENTID group by a.ID,a.TTEXT;
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Closing Comment

by:lrbrister
ID: 35028899
Close enough...added a line and works great

select t1.id, t1.ttext, count(*) numRecords
from dbo.valueReportsCrystal t1 left join dbo.valueReportsCrystal t2 on t1.id = t2.parentid
where t1.parentID is null
group by t1.id, t1.ttext
0
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 35028902
select A1.id, A1.ttext, count(*) numRecords
from tbl A1 inner join tbl B2
on A1.id = A2.parentid
where A1.parentid isnull
group by A1.id, A1.ttext
0
 

Author Comment

by:lrbrister
ID: 35028914
joshbula: and MarioAlcaide:

Sorry...I didn;t see your responses and hadn't refresed the page before I submitted my award
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now