我的SQL代码没有提供我需要的所有数据

时间:2015-07-06 17:51:44

标签: sql sql-server sql-server-2008 ssms

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奖励年仅有2000多个。我需要这个密集的代码才能适用于截至13/14的所有奖励年度,并且它不是。

我不确定我的代码中是否存在限制,即制作的限制是否为18行。我对SQL Server Management Studio相当新,我知道有大量的内部联接,但我需要它们来处理这些密集数据。

有什么建议吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

您正在选择每个的最大值。因此,在上面的代码中,您有18个select max语句。它从每列的2000行左右获取最大值,从而为您提供18列。尝试删除最大值。