以下是数据的示例:
Student ID Type Attendance Date Attendance Name Given1 Attendance Name Given2 Test Type
445633 Student 2/04 Matt Smith BIO
445633 Student 2/04 Matt Smith HIST
445633 Student 2/04 Joe Smith BIO
445633 Student 8/09 Joe English BIO
535485 Student 8/09 Joe English HIST
535485 Student 11/19 Joe English BIO
114687 Student 3/14 Frank William GEN
114687 Student 11/10 Greg William MATH
114687 Student 11/10 Greg William CHEM
114687 Student 8/09 Dan Harris HIST
114687 Student 8/09 Dan Harris HIST
这些是我想要的行(忽略出勤名称给定1和2的内容。现在我的分组包括我认为不唯一的唯一行bc输入的数据是由用户完成的,并且是我希望我的结果显示这些自由文本字段,但不希望它们被严格地视为一个唯一的行,因为这两个名称给定字段。(想想这两个名称给定字段可能的谎言)。
所以我的结果需要屈服......
对于445633我不在乎我是否保留445633 Matt或Joe,因为学生ID相同,类型相同,出勤日期相同且测试相同。应该构成唯一行的唯一字段应该是学生ID,出勤日期和测试类型。学生类型将始终相同(学生)。
445633我们放弃了BIO条目...... 535485我们保持两者,因为出勤日期不同 114687我们松开了一个HIST条目
Student ID Type Attendance Date Attendance Name Given1 Attendance Name Given2 Test Type
445633 Student 2/04 Matt Smith BIO
445633 Student 2/04 Matt Smith HIST
445633 Student 8/09 Joe English BIO
535485 Student 8/09 Joe English BIO
535485 Student 11/19 Joe English BIO
114687 Student 3/14 Frank William GEN
114687 Student 11/10 Greg William MATH
114687 Student 11/10 Greg William CHEM
114687 Student 8/09 Dan Harris HIST
谢谢!
答案 0 :(得分:0)
如果您真的不在乎这两个名字来自同一记录,您可以使用group by
执行此操作:
select StudentID, Type, AttendanceDate,
min(AttendanceNameGiven1), min(AttendanceNameGiven2),
TestType
from table t
group by StudentID, Type, AttendanceDate, TestType;
或者,如果您想要来自同一记录的值,则可以使用row_number()
执行此操作:
select StudentID, Type, AttendanceDate,
AttendanceNameGiven1, AttendanceNameGiven2,
TestType
from (select t.*,
row_number() over (partition by StudentID, Type, AttendanceDate, TestType
order by newid()
) as seqnum
from table t
) t
where seqnum = 1;