分组依据语法错误

时间:2014-02-21 15:53:10

标签: sql sum subquery

这显示为NULL。

SELECT Gender, SUM( DISTINCT Freq ) AS TotalApp, 
(
    SELECT SUM( Freq ) 
    FROM ucbadmissions
    WHERE Admit = 'Rejected'
    IN (
        SELECT Admit
        FROM ucbadmissions
       )
) AS AdmRate
FROM ucbadmissions
GROUP BY Gender

这显示为错误。

SELECT Gender, SUM( DISTINCT Freq ) AS TotalApp, 
(
    SELECT  SUM( Freq ) 
    FROM ucbadmissions
    WHERE Admit != 'Rejected'
    Group By Gender
) AS AdmRate
FROM ucbadmissions
Group By Gender

表的示例。

  

承认性别部门Freq
  录取男性A 512   被拒绝的男性A 313
  录取女性A 89   被拒绝的女性A 19   被拒绝的女性B 8
  录取女性B 17
  拒绝男性B 207
  录取男性B 353

2 个答案:

答案 0 :(得分:0)

如果我理解你的要求,这应该可以解决问题:

SELECT 
  Gender, 
  SUM(Freq) AS TotalApp,
  SUM(CASE WHEN Admit = 'Rejected' THEN 0 else Freq END) AS AdmRate
FROM ucbadmissions
GROUP BY Gender

这将返回性别加上申请总数加上每个性别的成功申请数量。

答案 1 :(得分:0)

SELECT      Gender, SUM(Freq) AS TotalApp,
            SUM(CASE WHEN Admit = 'Admitted' THEN Freq ELSE 0 END) / SUM(Freq) AS AdmitRate
FROM        ucbAdmissions
GROUP BY    Gender