Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Click here to copy the following block
create table #Topic (
    TopicID  char(2)   not null primary key ,
    TopicName varchar(32) not null
)
create table #Seminar (
    SeminarID int   not null primary key ,
    TopicID  char(2) not null foreign key references #Topic(#TopicID)
)
create table #Registrant (
    RegistrantID int not null primary key ,
    SeminarID  int not null foreign key references Seminar(SeminarID)
)
go
insert into #Topic values ( 'BR','How to be the Best Receptionist' )
insert into #Topic values ( 'AC','Advanced Coding'         )
insert into #Topic values ( 'IP','Insurance Processing'      )

insert into #Seminar values ( 1 , 'BR' )
insert into #Seminar values ( 2 , 'IP' )
insert into #Seminar values ( 3 , 'AC' )
insert into #Seminar values ( 4 , 'AC' )
insert into #Seminar values ( 5 , 'BR' )
insert into #Seminar values ( 6 , 'BR' )

insert into #Registrant values ( 1 , 1 )
insert into #Registrant values ( 2 , 2 )
insert into #Registrant values ( 3 , 1 )
insert into #Registrant values ( 4 , 3 )
insert into #Registrant values ( 5 , 1 )
insert into #Registrant values ( 6 , 2 )
insert into #Registrant values ( 7 , 4 )
insert into #Registrant values ( 8 , 2 )
insert into #Registrant values ( 9 , 3 )
insert into #Registrant values ( 10 , 1 )

--The sample data:
SELECT *
FROM #Topic AS t
JOIN #Seminar AS s
    ON s.TopicID = t.Topicid
JOIN #Registrant r
    ON r.seminarid = s.seminarid
ORDER BY t.Topicid, s.seminarid , r.registrantid
/*
TopicID TopicName            SeminarID  TopicID RegistrantID SeminarID 
------- -------------------------------- ----------- ------- ------------ -----------
AC   Advanced Coding              3 AC         4      3
AC   Advanced Coding              3 AC         9      3
AC   Advanced Coding              4 AC         7      4
BR   How to be the Best Receptionist      1 BR         1      1
BR   How to be the Best Receptionist      1 BR         3      1
BR   How to be the Best Receptionist      1 BR         5      1
BR   How to be the Best Receptionist      1 BR        10      1
IP   Insurance Processing            2 IP         2      2
IP   Insurance Processing            2 IP         6      2
IP   Insurance Processing            2 IP         8      2
*/


-- Problem: To show each topic , distinct seminars in each topic &
--     the distinct registrations in each topic.
SELECT t.TopicID  ,
    t.TopicName ,
    ( SELECT COUNT( DISTINCT s.SeminarID )
     FROM #Seminar AS s
     WHERE s.TopicID = t.TopicID ) AS Seminars ,
    ( SELECT COUNT( DISTINCT r.RegistrantID )
     FROM #Registrant AS r
     JOIN #Seminar AS s
     ON r.SeminarID = s.SeminarID
     WHERE s.TopicID = t.TopicID ) AS Registrants
FROM #Topic AS t
ORDER BY t.TopicID
/*
TopicID TopicName            Seminars  Registrants
------- -------------------------------- ----------- -----------
AC   Advanced Coding              2      3
BR   How to be the Best Receptionist      3      4
IP   Insurance Processing            1      3
*/


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.