Solved

group within a group

Posted on 2011-02-14
5
480 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
ID: 34891499
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
ID: 34892034
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
ID: 34892098
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
ID: 34892196
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
ID: 34897066
very nice- thanks.
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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

803 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