6 Sep 2010 - Digital Strategy // By Productive Edge Team

Quick SQL snippit

I occasionally need to get some representative values from a table for testing and thought my solution might be something worth sharing.  I tend to think in nested selects and go back and clean up code into joins where appropriate.  Below you will see that I am using a group by and a min function to identify single instances of different types, and then pulling the required data in an outer select:

SELECT
UserName
FROM
Users
WHERE
UserID in
(
SELECT
MIN(UserID)
FROM
USERS
WHERE
Active = 1
GROUP BY
TypeCode
)
Share
Quick SQL snippit