Solved

Complicated Select Statement

Posted on 2011-03-03
6
254 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query 5 44
Merge join vs exist 3 28
PowerShell: Adding ToGB to a script 4 30
SQL Query logic question 14 31
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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 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