如何在SQL Server 2008中汇总子查询数据?
在此查询中,我想在此Score
函数中对此CASE
列数据求和,其中数字范围之间的点,足够的时间我尝试对此列求和它显示错误。
现在,我想将此列Score
与Group by Applicant id
相加,我可以将Score
列数据加在一起的方式是什么?
但是我不能这样做。请帮助我,这里我是初学者。
SELECT
ApplicantId, point,
'Score' = CASE
WHEN point BETWEEN 4.00 AND 4.50 THEN ('3' )
WHEN point BETWEEN 3.5 AND 4.00 THEN ('2' )
END
FROM tblAcademicInfo
WHERE tblAcademicInfo.ApplicantId = tblAcademicInfo.ApplicantId
输出:
ApplicantId point Score
--------------------------------
xzc1 3.25 NULL
xzc1 4.36 3
xzc1 3.59 2
xzc1 4.00 3
答案 0 :(得分:1)
有几个问题
WHERE
条款毫无意义SUM()
和GROUP BY
条款CASE
中的数值而不是字符串文字point
列。但它不属于GROUP BY
,因为您希望按applicantid
进行分组。因此,您应该在此列上应用聚合函数(例如SUM()
)或从结果集中删除它。尝试
SELECT ApplicantId,
SUM(CASE WHEN point BETWEEN 4.0 AND 4.5 THEN 3
WHEN point BETWEEN 3.5 AND 4.0 THEN 2
END) Score
FROM tblAcademicInfo
GROUP BY ApplicantId
输出:
| APPLICANTID | SCORE | |-------------|-------| | xzc1 | 8 |
这是 SQLFiddle 演示
答案 1 :(得分:0)
您是否尝试过以下操作:
SELECT ApplicantId,point,
SUM(CASE
WHEN point BETWEEN 4.00 AND 4.50 THEN 3
WHEN point BETWEEN 3.5 AND 4.00 THEN 2
END
) [Score]
FROM tblAcademicInfo
WHERE tblAcademicInfo.ApplicantId
GROUP BY ApplicantId, point
答案 2 :(得分:0)
试试这个
SELECT ApplicationId,Point,sum(Case When score IS Null Then 0 else score END) as SumScore
FROM tblAcademicInfo
GroupBy Applicatio0nId,Point,score