×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Using Distinct and count in same query???

Using Distinct and count in same query???

Using Distinct and count in same query???

(OP)
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....

RE: Using Distinct and count in same query???

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?

RE: Using Distinct and count in same query???

(OP)
M | M | 20

Male | Medium | qty Shirts

F | L | 15

Female | Large | qty shirts


RE: Using Distinct and count in same query???

you said

Quote (You)

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?


RE: Using Distinct and count in same query???

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close