Skip to content
Advertisement

Deleting rows in sql rows based on pair value

I have SQL test table as below:

Item Doc_No Code Line_Item
1 abc1234 101 01
2 abc1234 102 01
3 def5678 101 01
4 def5678 102 01
5 ghi1234 101 01
6 ghi1234 101 02
7 jkl5678 101 01

I am trying to eliminate rows when duplicate values of “Doc_No” has pair values of “101” and “102” in “Code” column e.g abc1234 and def5678.

At the same time I want to maintain duplicate values of “Doc_No” without the pair value of “101” and “102” in “Code” column e.g. ghi1234. Final output as below:

Item Doc_No Code Line_item
5 ghi1234 101 01
6 ghi1234 101 02
7 jkl5678 101 01

I tried to get the rows with duplicate values and exclude them but this will wrongly exclude “ghi1234” and not the final output table I want.

Item Doc_No Code Line_Item Count_Item
7 jkl5678 101 01 1

Can somebody help me with this?

Advertisement

Answer

Building on your construct:

Note: This assumes that codes are not duplicated for a doc.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement