1

我有以下查询来生成学生的经济援助奖励表。我们想做一个 Pivot,因为我们想同时显示多个学期的帮助。

select pt.award as Award, pt.[1] as Fall, pt.[2] as Spring, pt.[3] as Summer            
--into #awardTemp
  from (select fa.Description as award, fa.OriginalAmount, fa.TextTerm,
          ROW_NUMBER() OVER(PARTITION BY fa.description 
            order by fa.textterm) AS M
      from CAMS_Enterprise.dbo.cams_FinancialAward_view as fa
        where fa.Code1ID = 0
      and fa.StudentAccepted is null
      and fa.studentuid = @studentuid) as AT
  pivot (max(at.originalamount) for at.m in ([1],[2],[3])) as pt

我们没有在同一行列出每学期的金额,而是得到如下所示的结果。即使描述标题相同,每个学期的奖项都有自己的行。有谁知道问题出在哪里?

Academic Scholarship                5000.00 NULL    NULL
Academic Scholarship                NULL    5000.00 NULL
Federal Subsidized Stafford Loan    1750.00 NULL    NULL
Federal Subsidized Stafford Loan    NULL    1750.00 NULL
Federal Unsubsidized Stafford Loan  1000.00 NULL    NULL
Federal Unsubsidized Stafford Loan  NULL    1000.00 NULL
4

1 回答 1

3

如果没有从 之前的表中看到您的数据pivot,我猜测这是由于fa.TextTerm在查询中包含的。

这给出了TextTerm导致多行的不同值。

这似乎是问题的明显原因,因为您没有在最终SELECT列表中使用该列。我建议将您的查询更改为:

select pt.award as Award, pt.[1] as Fall, pt.[2] as Spring, pt.[3] as Summer            
--into #awardTemp
from 
(
    select fa.Description as award, 
        fa.OriginalAmount, 
        ROW_NUMBER() OVER(PARTITION BY fa.description order by fa.textterm) AS M
    from CAMS_Enterprise.dbo.cams_FinancialAward_view as fa
    where fa.Code1ID = 0
      and fa.StudentAccepted is null
      and fa.studentuid = @studentuid
) as AT
pivot 
(
    max(at.originalamount) 
    for at.m in ([1],[2],[3])
) as pt
于 2013-03-12T19:02:39.193 回答