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 2012 Forums
 Transact-SQL (2012)
 Creating School Classes (Girl/Boy)

Author  Topic 

LacOniC
Starting Member

29 Posts

Posted - 2014-12-04 : 04:06:35
Hi. I need a query that share students to classes.

For example there are 15 Boys, 25 Girls and 4 classes. Query should share all sexes -if possible- equal to classes. In this example i need something like that:

Class 1: 4 Boys 6 Girls
Class 2: 4 Boys 6 Girls
Class 3: 4 Boys 6 Girls
Class 4: 3 Boys 7 Girls

or

Class 1: 4 Boys 7 Girls
Class 2: 4 Boys 6 Girls
Class 3: 4 Boys 6 Girls
Class 4: 3 Boys 6 Girls

Thnaks in advance.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-04 : 08:51:59
This is clearly homework. You need to take a crack at it yourself first. Remember your lectures on SELECT and see what you come up with.
Go to Top of Page

viggneshwar
Yak Posting Veteran

86 Posts

Posted - 2014-12-04 : 09:33:14
May be this would be helpful

SELECT classname + ': '
+ CONVERT(VARCHAR(3),MAX(CASE WHEN sex='Male' THEN 1 ELSE 0 END)) + ' Boys'
+ CONVERT(VARCHAR(3),MAX(CASE WHEN sex='Female' THEN 1 ELSE 0 END)) + ' Girls'
FROM tablename
GROUP BY classname

Regards
Viggneshwar A
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-04 : 09:46:46
quote:
Originally posted by viggneshwar

May be this would be helpful




I'm sure it is, but then the OP won't learn anything from doing their homework, will they?
Go to Top of Page

viggneshwar
Yak Posting Veteran

86 Posts

Posted - 2014-12-12 : 02:40:16
Yeah you are right. If it is urgency then the solution is better as of now. Surely have to learn by considering this as an example.

Regards
Viggneshwar A
Go to Top of Page
   

- Advertisement -