Solved

Complicated Select Statement

Posted on 2011-03-03
6
247 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

861 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