Solved

group within a group

Posted on 2011-02-14
5
474 Views
Last Modified: 2012-05-11
select count(*),PROG_KEY from TEACHER group by PROG_KEY order by 1
but the above should also make sure there are at least 2 TYPE_KEY for each PROG_KEY

how can you check that with t-sql?

thanks
0
Comment
Question by:anushahanna
5 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
try:

select count(1), PROG_KEY from TEACHER
group by PROG_KEY
having count(1) > 1
order by 1
0
 
LVL 41

Accepted Solution

by:
ralmada earned 250 total points
Comment Utility
I was thinking something like this instead
a) 

SELECT PROG_KEY, count(1) from TEACHER
group by PROG_KEY
having count(distinct TYPE_KEY) > 1
order by 2

b) 
select 	PROG_KEY, 
	sum(sKey), 
from (
	select PROG_KEY, TYPE_KEY, count(*) as sKey 
	from TEACHER 
	group by PROG_KEY, TYPE_KEY
) a	
group by PROG_KEY 
having count(PROG_KEY) > 1
order by 2

Open in new window

0
 
LVL 7

Assisted Solution

by:lundnak
lundnak earned 125 total points
Comment Utility
If you need two distinct type_key values per prog_key, then go with the first solution that ralmada recommended.

If they type_key's don't need to be distinct, than all you need to do is modify his solution by removing the distinct.

Ralmada provided a good solution.

SELECT PROG_KEY, count(1) from TEACHER
group by PROG_KEY
having count(TYPE_KEY) > 1
order by 2

Open in new window

0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 125 total points
Comment Utility
or you can use

select count(TYPE_KEY) cnt_type, PROG_KEY from TEACHER
group by PROG_KEY
having count(TYPE_KEY) > 1
order by 1

or

select count(distinct TYPE_KEY) cnt_type, PROG_KEY from TEACHER
group by PROG_KEY
having count(distinct TYPE_KEY) > 1
order by 1
0
 
LVL 6

Author Comment

by:anushahanna
Comment Utility
very nice- thanks.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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 video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

763 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

10 Experts available now in Live!

Get 1:1 Help Now