希望简单的MS SQL问题!我有四个表:跨多个表的SQL Server连接
Portfolio:
int portfolioId, string portfolioName
1,'PortFolio 1'
Trip:
int tripId, int portfolioId, int tripType, int maxPeople, string tripName
1, 1, 1, 20, 'Trip A'
2, 1, 1, 21, ’Trip B'
3, 1, 2, 22, ’Trip C'
Person:
int personId, int personStatus, string personName
1, 14, ‘Person 1'
2, 15, ‘Person 2'
3, 16, ‘Person 3'
TripPerson:
int personId, int tripId
1, 1
1, 3
2, 1
2, 2
对于给定的portfolioId,我想写一个干净的查询,这将使我下面列:
tripId, countA, countB, tripType, tripName
其中: COUNTA:是总数旅行人员。 CountB:是该行程中至少有另外一次旅行的类型为'2'的总人数。 返回的行数必须与tripI相关的行程数量,其中portfolioId = 1,由tripName排序。
想法?我正在使用MS SQL,对SQL有一个基本的了解,而这正在推动着我的香蕉。
我结束了使用CTE的 - 他们已经永远地改变了我的生活MSSQL。 – Richard 2015-05-05 15:20:49