Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用select distinct的分区_Sql Server - Fatal编程技术网

Sql server 使用select distinct的分区

Sql server 使用select distinct的分区,sql-server,Sql Server,我有一个像这样的数据集 StudentName Course Studentmailid Score Student1 A student1@gmail.com. 80 Student1 A student1@gmail.com. 75 Student2 A student2@gmail.com. 70 Student1 B student2@gmai

我有一个像这样的数据集

  StudentName   Course Studentmailid          Score
    Student1       A   student1@gmail.com.     80
    Student1       A   student1@gmail.com.     75
    Student2       A   student2@gmail.com.     70
    Student1       B   student2@gmail.com.     70
现在我要记录1,3,4,基本上是学生在每门课程中的第一次出现

我的问题是

select distinct StudentName,Course, Studentmailid,Score fromStudentTable group by Course
它抛出了一个错误。我需要对查询进行什么调整才能得到所需的输出?希望它能有所帮助

;with cte as (
  select StudentName,Course, Studentmailid,Score, Row_Number() over (partition by StudentName, Course order by StudentName) as Row_Num from StudentTable
)
select * from cte where Row_Num = 1

您需要一个列来定义多行中的第一项。换句话说,不能保证每次都能得到相同的输出。下面是一个示例,其中我按NULL排序,它将按正常顺序返回记录。但这并不总是返回相同的结果

您需要一个包含值的列来对所有记录进行排序。然后,您可以简单地用列替换窗口函数中的排序部分


您的预期输出是什么?@mkRabbani记录1、3和4为什么返回第3行?在同一课程中,A.@Mureinik第3行是一个不同的学生。是否有任何列来排序结果?
SELECT * FROM
(
    SELECT *,
    ROW_NUMBER() OVER (PARTITION BY StudentName, Course ORDER BY (SELECT NULL)) RN
    -- ORDER BY (SELECT NULL) is basically to keep data as it is now in the table
    -- But there is no guaranty that it will order data in same order always
    FROM your_table
)A
WHERE RN=1