【发布时间】:2015-07-06 17:51:44
【问题描述】:
select distinct
max(A.awardyear) AS 'Award Year',
max(S.SSN) AS 'Student Social Security Number',
max(S.firstname) AS 'Student First Name',
max(S.lastname) AS 'Student Last Name',
max(D.birthdate) as 'Student Date of Birth',
max(M.OPEID) as 'Instituion Code',
max(M.Schoolname) as 'Institution Name',
max(Q.programtitle) as 'Program Name',
max(Q.CIPCode) as 'CIPCode',
max(Q.[Award Document]) as 'Credential Level',
case
WHEN q.[Award Document] = 'diploma'
then '01'
else '02'
end,
max(I.[1stSite]) as 'Medical or Dental internship',
min(A.AYStartDate) as 'Program attendance begin date',
max(y.EnStatus) as 'Program attendance status',
max(y.NewGradDate) as 'Program attendance status date',
case
when y.NewGradDate > '2009-06-30'
then '2009-06-30'
else y.NewGradDate
end,
min(A.AYStartDate) as 'program attendance begin date for this award year',
max(R.programtotal) as 'Tuition & fees',
max(R.AYSupplies + R.AYBooks + R.AYUniform) as 'allowance for books, supplies, and equipment',
max(P.programlength) as 'Length of GE program'
from
studentinfo s
inner join
awardletter a ON A.studentid = S.studentid
inner join
budget r on A.budgetid = r.budgetid
inner join
studentdata d on D.studentid = s.studentid
inner join
TranscriptInformation t on t.studentid = s.studentid
inner join
attendance e on e.studentid = s.studentid
inner join
ProgLengthCalc p on p.studentid = s.studentid
inner join
programs q on q.program = p.program
inner join
Schools m on m.school = s.school
inner join
enrollmentinformation y on y.studentid = s.studentid
left join
internship i on i.studentid = s.studentid
where
A.awardyear = '08/09'
group by
a.awardyear, a.awardyear, s.ssn, s.firstname, s.lastname, d.birthdate,
m.opeid, m.schoolname, q.programtitle, q.cipcode, q.[Award Document],
i.[1stSite], a.aystartdate, y.EnStatus, y.NewGradDate, e.date,
r.programtotal, r.AYSupplies + r.aybooks + r.ayuniform, p.programlength
发生的情况是我的代码仅返回 18 行,而实际上仅在 08/09 颁奖年就有 2,000 多行。我需要这个密集的代码来工作到 13/14 的所有奖励年份,但事实并非如此。
我不确定我在代码中设置的限制是否为 18 行。我对 SQL Server Management Studio 还很陌生,我知道有大量的内部连接,但我需要它们来处理如此密集的数据量。
有什么建议吗?提前谢谢!
【问题讨论】:
-
在不知道您想要的结果或样本数据的情况下,问题似乎在于您正在聚合分组依据的相同列。例如,为什么您只需要 MAX 社会保险号?
-
如果没有数据就无法回答这个问题,表之间有很多依赖关系,任何 join/where/groupby 都可以缩小结果范围
-
我会说您的 group by 子句有 99.99999% 的可能性是错误的或更可能不需要
-
@Amit 所以如果错了,我应该怎么做?
-
现在删除所有聚合函数...老实说,这感觉就像您在学会走路之前尝试进行障碍赛。在构建这样的查询之前你应该学习 SQL
标签: sql sql-server sql-server-2008 ssms