I’m working on an SSIS package where I’m importing data from a CSV file into a SQL table.
The only field that I’m concern with is the Username. This Username must be unique. I don’t care whether first name or last name are the same or not.
In the package, I imported the data from file into a temp SQL table. And then I used SELECT DISTINCT to pick unique Username. And then insert into the destination table.
The problem is: When I do a SELECT DISTINCT Username, Firstname and Lastname FROM tempUsers.
It returns:
- JSmith, John, Smith
- JSmith, Joe, Smart
- MBopp, Mary, Boppins
But I want it to return:
- JSmith, John, Smith
- MBopp, Mary, Boppins
SELECT DISTINCTwill select all distinct rows for each column you specify, so that’s not quite what you’re looking for.If your flavor of SQL supports it, try
GROUP BYwithFIRST(), like the following. It will return the first record for each individual Username.If not, you have to do it the hard way using a row ID and sub-select:
** You shouldn’t use MIN with Firstname and Lastname, because you can’t guarantee they will come from the same row: