Here's an example of the data now:
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
And these are the rows that I want (Ignoring the contents of Attendance Name Given1 and 2. Right now my group by is including unique rows that I do not consider unique bc the data entered into them was done by the user and is free text. I want my results to show these free text fields but do not want them to be considered a unique row strickly because of these 2 Name given fields. (Think of these two name given fields as possible lies).
So my results need to yield...
For 445633 I don't care if I keep 445633 Matt or Joe since Student ID is the same, Type is the same, Attendance Date is the same and Test is the same. The only fields that should make up a unique row should be Student ID, Attendance Date, and Test Type. Student Type will always be the same (Student).
445633 we loose a BIO entry... 535485 we keep both since the attendance dates are different 114687 we loose a HIST entry
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
Thank you!
You can do this using group by
, if you really don't care whether the two given names are from the same record:
select StudentID, Type, AttendanceDate,
min(AttendanceNameGiven1), min(AttendanceNameGiven2),
TestType
from table t
group by StudentID, Type, AttendanceDate, TestType;
Or, you can do this using row_number()
if you want the values from the same record:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.