Answer the question
In order to leave comments, you need to log in
How can I make a selection from a MS SQL database?
Hello!
There is a schema for the database of debtors by subject at the university.
I'm interested in the following sample:
For each group, select the total number of debtors and the number of debtors who have more than two debts
select Students.GroupNumber as groupNum, Count(StudentsSubjects.StId) as Kol_dolgov
from StudentsSubjects, Students
where Students.RecordBookNumber = StudentsSubjects.StId
group by Students.GroupNumber
order by groupNum
select t1.Grp, count(t1.Rbn) as Kol_dol_bol2
from
(select Students.GroupNumber as Grp, Students.RecordBookNumber as Rbn, count(StudentsSubjects.SubjId) as Kol_dolg
from Students,StudentsSubjects, Subjects</li>
where Students.RecordBookNumber = StudentsSubjects.StId and Subjects.SubjectId = StudentsSubjects.SubjId
group by Students.RecordBookNumber, Students.GroupNumber
having count(StudentsSubjects.SubjId) > 2) as t1
group by t1.Grp
select Students.GroupNumber as Grp, count(Students.RecordBookNumber) as Kol_dolgnikov
from Students
group by Students.GroupNumber
order by 1
Answer the question
In order to leave comments, you need to log in
Too lazy to write from the phone. Not the best solution, but put two queries in a subrequest and join on the common field.
select *
from (query 1) as query1
left outer join (query 2) as query2 on query1.group = query2.group. There is a more elegant option.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question