0

I have the following issue, I need to remove duplicate values from a specific column I query. No deleting!(ClassID)

SchoolNo Schoolyear Schoolgrade Classname ClassId
65432 2001 5 ab 441
65432 2001 5 cd 442
65432 2001 6 a 443
65432 2001 6 b 444
56838 2001 5 ab 445
56838 2001 5 cd 446
56838 2001 6 ab 445
56838 2001 6 ef 447
12726 2001 5 ms 448
12726 2001 6 ms 448

If you look at the values of classId I have repeated class numbers because some special schools sometimes put 2 classes together for both grades. The problem is my query needs to show only 1 classid value. No repeats. Therefore we can remove any extra class that is repeated in value and only show for grade 5.

In other words my table should at end up looking like this.

SchoolNo Schoolyear Schoolgrade Classname ClassId
65432 2001 5 ab 441
65432 2001 5 cd 442
65432 2001 6 a 443
65432 2001 6 b 444
56838 2001 5 ab 445
56838 2001 5 cd 446
56838 2001 6 ef 447
12726 2001 5 ms 448

The code generally looks like this.

select schoolno,schoolyear,schoolgrade,classname,classId
from classgroup cg

How should I approach this?

2 Answers 2

2

maybe you can do it like this:

select
  first_value(schoolno) over w,
  first_value(schoolyear) over w,
  first_value(schoolgrade) over w,
  first_value(classname) over w,
  first_value(classId) over w
FROM
  classgroup
WINDOW w AS (PARTITION BY schoolno, schoolyear, classId ORDER BY schoolgrade);

You partition the data by schoolno, schoolyear and classId and order by schoolgrade then take only the first row of each partition.

Note: the syntax may be a bit off since I couldn't test it

Sign up to request clarification or add additional context in comments.

2 Comments

I did a test on your query and it is OK. Nice approach.
I have given it a try but i got the same result... it is not removing the 2nd row of similar classID the repeated classid is showing twice.
1

Try this

select cg1.* from classgroup cg1
left join classgroup cg2 on (cg1."ClassId"=cg2."ClassId" and cg1."Schoolgrade"<cg2."Schoolgrade")
where cg2."Schoolgrade" is null

The output: enter image description here

3 Comments

Hey so your query worked in the case that repeated classid's dont repeat themselves. However I am getting instead of a 5 a 6 even through the logic sounds fine. is there a way to apply a case statement to force it?
your method was perfect. Quick question what is the "" for? is there a difference between cg2.schoolgrade and scg."schoolgrade"?
Use "" when column names contain capital letters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.