如何使用SQL Server完成此任务

时间:2014-12-15 06:18:52

标签: sql sql-server

I have a table in this format

  • 如果学生有两个studentsubjectstatus Merit studentperformanceMerit
  • 如果学生有两个studentsubjectstatusPass studentperformancePass
  • 其他是Fail

所以我需要一个新的列studentperformance,输出就像这样

studentname       studentperformance
   A                   Fail
   B                   Merit
   C                   Pass
   D                   Fail

4 个答案:

答案 0 :(得分:1)

您可以将查询编写为:

;WITH CTE AS
( SELECT studentname, 
         ROW_NUMBER() OVER (partition BY studentname,studentsubjectstatus ORDER BY educationmarks DESC)
         AS rownum,
         studentsubjectstatus         
  FROM  @student)
 ,CTE2 AS
 (
 SELECT  studentname , MAX(rownum) AS maxrow
 FROM CTE
 GROUP BY studentname
 )
 SELECT DISTINCT T1.studentname
       ,CASE WHEN T1.rownum = 1 THEN 'Fail' ELSE
       T1.studentsubjectstatus END AS studentperformance 
 FROM CTE AS T1
 JOIN CTE2 AS T2 ON T1.studentname=T2.studentname AND T1.rownum=t2.maxrow

答案 1 :(得分:1)

试试这个:

SELECT
    studentName AS 'Student Name'
    , CASE 
          WHEN Merit >= 2 
              THEN 'Merit'
          WHEN Pass >= 2
              THEN 'Pass'
          ELSE 'Fail'
      END AS 'Student Performance'
(SELECT
    studentName
    , COUNT(CASE WHEN StudentSubjectStatus = 'Merit' THEN 1 ELSE 0 END) AS 'Merit'
    , COUNT(CASE WHEN StudentSubjectStatus = 'Pass' THEN 1 ELSE 0 END) AS 'Pass'

      -- counting how many subjects the student failed is not needed, 
      -- but for example purposes it has been added

    , COUNT(CASE WHEN StudentSubjectStatus = 'Fail' THEN 1 ELSE 0 END) AS 'Fail'
FROM Students
GROUP BY studentName)

答案 2 :(得分:1)

你可以使用以下聚合的情况来做到这一点:

SELECT studentname,
       CASE
         WHEN Sum(CASE
                    WHEN studentsubjectstatus = 'Merit' THEN 1
                    ELSE 0
                  END) > 1 THEN 'Merit'
         WHEN Sum(CASE
                    WHEN studentsubjectstatus = 'pass' THEN 1
                    ELSE 0
                  END) > 1 THEN 'pass'
         ELSE 'fail'
       END
FROM   yourtable
GROUP  BY studentname 

OR

SELECT studentname,
       CASE
         WHEN Sum(CASE
                    WHEN studentsubjectstatus = 'Merit' THEN 1
                    ELSE 0
                  END) > 1 THEN 'Merit'
         ELSE
           CASE
             WHEN Sum(CASE
                        WHEN studentsubjectstatus = 'pass' THEN 1
                        ELSE 0
                      END) > 1 THEN 'pass'
             ELSE 'fail'
           END
       END AS studentperformance
FROM   yourtable 
GROUP  BY studentname 

注意:> 1条件可以更改为= 2,如果您想使用某个studentsubjectstatus计算完全2行。

答案 3 :(得分:0)

假设每个学生总共有三个项目,这就是想法。

对于每个studentname

  1. 计算每个studentsubjectstatus

  2. 首先按照计数的降序对结果进行排序,然后按studentsubjectstatus的字母顺序排序(以便Fail在关系出现时成为第一个。)

    < / LI>
  3. 获得最佳结果。

  4. 所以,像这样:

    SELECT DISTINCT
      studentname,
      studentperformance = (
        SELECT TOP (1)
          s.studentsubjectstatus
        FROM
          dbo.yourtable AS s
        WHERE
          s.studentname = t.studentname
        GROUP BY
          s.studentsubjectstatus
        ORDER BY
          COUNT(*) DESC,
          s.studentsubjectstatus ASC
      )
    FROM
      dbo.yourtable AS t
    ;
    

    如果您不想依赖studentsubjectstatus中值的字母顺序,则可以通过用以下内容替换Fail来确保s.studentsubjectstatus ASC首先出现:

    CASE s.studentsubjectstatus WHEN 'Fail' THEN 1 ELSE 2 END ASC