That doesn't get me anywhere... The first query in the union will ALWAYS return a column with Priority=1, and the second query in the union will ALWAYS return a column with Priority=2, so they aren't duplicate rows. What I was trying to accomplish in the second query example was to perform the DISTINCT on the only the fields that I needed (without the Priority column, so they COULD be identified as duplicates), but still get the sort order right, and that is a no-go.
Main Topics
Browse All Topics





by: dqmqPosted on 2009-11-02 at 19:48:40ID: 25725905
Use UNION instead of UNION ALL and the duplicates will be dropped.
SELECT 1 as Priority, FirstName, LastName, SSN, TID FROM People
WHERE FirstName='Rob' OR LastName='Rob' OR SSN='Rob' OR TID='Rob'
UNION
SELECT 2 as Priority, FirstName, LastName, SSN, TID FROM People
WHERE FirstName LIKE 'Rob%' OR LastName LIKE 'Rob%' OR SSN LIKE 'Rob%' OR TID LIKE 'Rob%'
ORDER BY Priority, LastName, FirstName