Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Distinct and count in same query???

Status
Not open for further replies.

ItHurtsWhenIThink

Technical User
Sep 1, 2007
60
0
0
US
First, I thought that table I created was just going to track birthdays and then my small simple task grew. At first we just wanted to remind teachers of their student birthdays, then we added other data. I would have created a totally different schema had I known. But, here I am.

We decided to give our academy students polo shirts. So I just added shirt size and gender to the table.

Problem was that there are duplicate students with same student ID. A student may have many teachers within the academy. I know... at the beginning all we were going to is iterate through the table and send reminders of birthdays only the teachers that have the specific student.

Now I need to query and get a count of the sizes based on gender and size.

So I want:
F XS 3
F S 10
F M 18
F L 9
M S 1
M M 10
M L 20

Where 1st column= gender, 2nd = size and third = qty

Here is my query. Problem is I just can't figure out how to only get distinct count based on studentID, since there are many if the same ID.

here is what I am using (it gives my wrong count because it counts same student several times)

Select Count(ShirtSize) AS ctSizes, ShirtSize, Gender
from academybirthdays
Where ShirtSize IN('XS','S','M','L','XL','XXL','XXXL') /// there are some null vales
AND
StudentID in (select Distinct(StudentID) from StudentIDs)

Group By Gender,ShirtSize

The studentids table allow me to filter out those students that are not in the academy or qualify for a shirt.

Anyone have a solution for me? thanks....
 
So, you have M M 10 and F S 10 is that the same student id but different gender and size? in thins case which result you expect and why?
 
M | M | 20

Male | Medium | qty Shirts

F | L | 15

Female | Large | qty shirts


 
you said

You said:
Problem is I just can't figure out how to only get distinct count based on studentID, since there are many if the same ID.

here is what I am using (it gives my wrong count because it counts same student several times)

Select Count(ShirtSize) AS ctSizes, ShirtSize, Gender
from academybirthdays
Where ShirtSize IN('XS','S','M','L','XL','XXL','XXXL') /// there are some null vales
AND
StudentID in (select Distinct(StudentID) from StudentIDs)

Group By Gender,ShirtSize
My question if you have multiple entries for the same student in academybirthdays
for example
studentnId 1, ShirtSize 'L', Gender 'M'
and
studentnId 1, ShirtSize 'M', Gender 'M'
and
studentnId 1, ShirtSize 'L', Gender 'F'
which record you would like to pick for calculation count?


 
But any way I think you looking to something like

; with t as
(
select ROW_NUMBER() over(partition by studentid order by gender, shirtSize) as row,
gender,
ShirtSize,
StudentID

from academybirthdays
where ISNULL(ShirtSize,'')<>''
)
Select Count(ShirtSize) AS ctSizes, ShirtSize, Gender
from t
where row=1
and StudentID in (select Distinct(StudentID) from StudentIDs)
group by ShirtSize, Gender
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top