Solved

Complicated Select Statement

Posted on 2011-03-03
6
236 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
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.

785 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