Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select specific user only if they are a "duplicate" and meet condition

I have a result set like this:

UserName   Action
-------------------
Joe        Add
Joe        Remove
Fred       Add
Sam        Add
Carl       Remove

I only want to return the Add for Joe in this case. Add takes precedence. So if the user has an add record we only return that like.

Joe   Add
Fred  Add
Sam   Add
Carl  Remove

Not sure the best way to accomplish this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If your real example is really as simple as your post, maybe consider something like:

SELECT username, MIN(action) action
FROM mytable
GROUP BY username

That will return one row per username (because of the GROUP BY). If there is some mix of "Add"/"Remove" actions, it will pick add (because "Add" will be the MIN). If there is only a "Remove" action (no "Add"s) it will use that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading