2016-10-25 2 views
0

먼저 영어를하지 못해서 영어가 완벽하지 않으면 미안 해요. 나는 학교에있는 모든 사람들과 그들이 속한 방을 나열한 테이블을 가지고있다. 두 번째 테이블에는 방 번호와 각 방에있는 사람의 수가있다. 방에있는 방의 빈 좌석 수와 방 번호가있는 테이블을 어떻게 표시 할 수 있습니까? Microsoft sqlserver managment studio 2008을 사용하고 있습니다. 도움을 주셔서 감사합니다! 당신의 테이블의 구조를 가정SQL에서 빼는 동안 두 테이블을 결합하는 방법

+1

** [편집] ** 귀하의 질문에 일부 샘플 데이터를 추가하고 예상 출력 해당 데이터를 기반으로합니다. [_Formatted_] (http://stackoverflow.com/editing-help#code) ** text ** please, [스크린 샷 없음] (http://meta.stackoverflow.com/questions/285551/why-may-i) -not-upload-images-of-code-on-so-asking-a-question/285557 # 285557) –

답변

0

다음과 같이 :

첫 번째 테이블 :

StudentId | RoomNumber 

두 번째 테이블 : 다음

RoomNumber | Count 

,

select t1.RoomNumber, t2.Count - count(t1.StudentId) 
from FirstTable t1 
inner join SecondTable t2 
on t1.RoomNumber = t2.RoomNumber 
group by t1.RoomNumber 
+0

t1.RoomNumber로 그룹화하고 같은 것을 계산하면 항상 1이됩니다. 'count (t1.StudentId) '로 바꾸십시오. –

+0

힌트를 보내 주셔서 감사합니다. –

0

테이블 :

학생 학생용 | RoomId

RoomId | NumberOfSeats

Select room.RoomId, room.NumberOfSeats - count(stu.StudentId) 
from Rooms room 
left join Students stu on room.RoomId = stu.RoomId 
group by room.RoomId 

너무

바이올린 어떤 학생없이 객실을 얻을 것이다이 방법 : here

+0

좋습니다! 도움을 주셔서 감사합니다 –

-1
select t1.StudentId, 
    t1.RoomNumber, 
    t1.StudentId-Count(t2.Count), 
    t2.RoomNumber, 
    t2.count 
from FirstTable as t1 
Right join SecondTable as t2 
on t1.RoomNumber = t2.RoomNumber 
group by t2.RoomNumber 
+0

테이블 후 별칭을 사용하여 예를 들어 선택 *에서 tablename으로 –

관련 문제