【问题标题】:My SQL code doesn't give me all the data I need我的 SQL 代码没有提供我需要的所有数据
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 这样做了,结果还是一样
  • 尝试删除 distinct 以及它只是一个选择。
  • 我的最后一个建议是,除了学生社会安全号码之外,您的代码中的所有内容都最多,只需选择它即可。现在您只需按学生社会安全号码进行分组。如果 2000 行中的每一行都代表具有唯一社会安全号码的唯一学生,则不应合并任何行,您应该看到所有 2000 行。如果您没有看到所有行仍然查看内部连接。如果不满足联接条件,则内部联接不会显示行。
  • 因此,如果您的任何内部连接条件在一行上没有得到满足,那么整个行都不会作为您的查询结果显示。如果这不是您的意图,请尝试左连接。左连接或外连接。即使不满足连接条件,这也将保留您的所有行。当不满足时,该行将不会从它正在连接的表中提取列信息。
  • 我做了左连接并得到了我想要的行数,但现在我得到了不应该有的 NULL 值
猜你喜欢
  • 1970-01-01
  • 2023-01-14
  • 2018-10-09
  • 2023-01-07
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多