How do I remove “duplicate” rows from a view?

后端 未结 5 1806
孤城傲影
孤城傲影 2021-01-06 10:58

I have a view which was working fine when I was joining my main table:

LEFT OUTER JOIN OFFICE ON CLIENT.CASE_OFFICE = OFFICE.TABLE_CODE.

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-06 11:22

    DISTINCT won't help you if the rows have any columns that are different. Obviously, one of the tables you are joining to has multiple rows for a single row in another table. To get one row back, you have to eliminate the other multiple rows in the table you are joining to.

    The easiest way to do this is to enhance your where clause or JOIN restriction to only join to the single record you would like. Usually this requires determining a rule which will always select the 'correct' entry from the other table.

    Let us assume you have a simple problem such as this:

    Person:  Jane
    Pets: Cat, Dog
    

    If you create a simple join here, you would receive two records for Jane:

    Jane|Cat
    Jane|Dog
    

    This is completely correct if the point of your view is to list all of the combinations of people and pets. However, if your view was instead supposed to list people with pets, or list people and display one of their pets, you hit the problem you have now. For this, you need a rule.

    SELECT Person.Name, Pets.Name
    FROM Person
      LEFT JOIN Pets pets1 ON pets1.PersonID = Person.ID
    WHERE 0 = (SELECT COUNT(pets2.ID) 
                 FROM Pets pets2
                 WHERE pets2.PersonID = pets1.PersonID
                    AND pets2.ID < pets1.ID);
    

    What this does is apply a rule to restrict the Pets record in the join to to the Pet with the lowest ID (first in the Pets table). The WHERE clause essentially says "where there are no pets belonging to the same person with a lower ID value).

    This would yield a one record result:

    Jane|Cat
    

    The rule you'll need to apply to your view will depend on the data in the columns you have, and which of the 'multiple' records should be displayed in the column. However, that will wind up hiding some data, which may not be what you want. For example, the above rule hides the fact that Jane has a Dog. It makes it appear as if Jane only has a Cat, when this is not correct.

    You may need to rethink the contents of your view, and what you are trying to accomplish with your view, if you are starting to filter out valid data.

提交回复
热议问题