如何为同一主题获得相同的中期两次或更多次?

时间:2015-06-25 04:24:32

标签: sql sql-server sql-server-2008 tsql

我有3张表,如学生,主题和中期表。 学生表包含

studid Firstname lastname Class

1      A       R        12A
2      B       S        12A
3      C       T        12A
4      D       U        12A
5      E       V        12B

SUBJECT TABLE CONTAINS

subid subname
  1     maths
  2     science
  3     english

MIDTERM TABLE CONTAINS

studid subid marks examdate
1        1    100  2014-09-24
1        2     92  2014-09-25
1        2     92  2014-09-26
2        1     74  2014-09-24
2        2     78  2014-09-26
2        3     73  2014-09-26
3        1     90  2014-09-24
3        2     84  2014-09-25
3        2     92  2014-09-25
5        1     87  2014-09-24
4        2     79  2014-09-24
4        3     90  2014-09-26

在此,我想列出所有同一个中期2次或更多次中期同学的学生。需要o / p是名字,姓氏,尝试次数

这是我的疑问:

select studid,SubID
FROM [StudentDetails].[dbo].[MidTermTable]
group by StudID,SubID having COUNT(subid)>1

2 个答案:

答案 0 :(得分:0)

创建示例数据

DECLARE @Student TABLE (studid INT, Firstname VARCHAR(10), lastname VARCHAR(10), Class VARCHAR(10))

INSERT @Student
SELECT 1,      'A',       'R',        '12A' UNION ALL
SELECT 2,      'B',       'S',        '12A' UNION ALL
SELECT 3,      'C',       'T',        '12A' UNION ALL
SELECT 4,      'D',       'U',        '12A' UNION ALL
SELECT 5,      'E',       'V',        '12B'

DECLARE @subject TABLE(subid INT, subname VARCHAR(10))

INSERT @subject
SELECT  1,     'maths' UNION ALL
SELECT  2,     'science' UNION ALL
SELECT  3,     'english'

DECLARE @Midterm TABLE (studid INT, subid INT, marks INT, examdate DATE)

INSERT @Midterm
SELECT 1,        1,    100,  '2014-09-24' UNION ALL
SELECT 1,        2,     92,  '2014-09-25' UNION ALL
SELECT 1,        2,     92,  '2014-09-26' UNION ALL
SELECT 2,        1,     74,  '2014-09-24' UNION ALL
SELECT 2,        2,     78,  '2014-09-26' UNION ALL
SELECT 2,        3,     73,  '2014-09-26' UNION ALL
SELECT 3,        1,     90,  '2014-09-24' UNION ALL
SELECT 3,        2,     84,  '2014-09-25' UNION ALL
SELECT 3,        2,     92,  '2014-09-25' UNION ALL
SELECT 5,        1,     87,  '2014-09-24' UNION ALL
SELECT 4,        2,     79,  '2014-09-24' UNION ALL
SELECT 4,        3,     90,  '2014-09-26'

查询:一种方法是使用窗口函数

;WITH C AS(
    SELECT  Firstname
            ,lastname
            ,m.subid
            ,examdate
            ,ROW_NUMBER() OVER (PARTITION BY s.studid, m.subid ORDER BY s.studid, m.subid) AS Rn
    FROM @Student AS s
    INNER JOIN @Midterm AS m ON m.studid = s.studid
    INNER JOIN @subject AS sj ON sj.subid = m.subid
)
SELECT  Firstname
        ,lastname
        ,MAX(Rn) AS [Number of attempts]
FROM C
WHERE Rn > 1
GROUP BY Firstname, lastname

<强>输出

Firstname   lastname    Number of attempts
A           R           2
C           T           2

我建议您学习Common Table ExpressionsROW_NUMBER()

答案 1 :(得分:0)

select [Student].Firstname, [Student].Firstname, count(*)
  FROM [MidTermTable] 
  JOIN [Student]
    on [Student].[studid] = [MidTermTable],[Student].
 group by [Student].Firstname, [Student].Firstname, SubID 
having COUNT(*) > 1