Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Comparison

Author  Topic 

joeyc0323
Starting Member

2 Posts

Posted - 2012-06-20 : 09:41:45
I need to run a comparison on a database but it is not one I am used to writing. I have a list of school districts and schools with their grades (01, 02, 03, 04...ect). I need to verify that every district and school has all grades represented. I have three individual columns, one for district, one for school, and one for grade.

Can anyone offer a solution to this?

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-06-20 : 10:06:21
select district, school
from tbl
group by district, school
having count(*) <> 10

If there are 10 grades


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

joeyc0323
Starting Member

2 Posts

Posted - 2012-06-20 : 10:11:39
Thanks! Works great! One question though, is it possible to see in the output what grades the school may be missing?

quote:
Originally posted by nigelrivett

select district, school
from tbl
group by district, school
having count(*) <> 10

If there are 10 grades


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.

Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-06-20 : 10:33:39
select t2.district, t2.school, t1.grade
from (select distinct grade from tbl) t1
cross join (select distinct district, school frrom tbl) t2
left join tbl t3
on t2.district = t3.district
and t2.school = t3.school
and t1.grade = t3.grade
where t3.district is null
order by t2.district, t2.school, t1.grade

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -