views:

363

answers:

2

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
+2  A: 
lc
I suggested using MIN() at first, but then realized that would end up scrambling the first and last names. I'm not used to FIRST(), but I'm guessing it would keep the first and last names of the chosen user together.
yukondude
Thanks you guys. Just didn't expect I'd get an answer within 7 minutes of posting this question :)My flavor of SQL doesn't support FIRST() unfortunately ...But MIN() works like a charm :)
tnafoo
MIN() will work for your subset of data above because "Joe" < "John" AND "Smart" < "Smith", but you're not guaranteed they come from the same row in all cases. It will take the minimum value for each column *independently* from within the group.
lc
A: 

For Oracle, you can use the following query :

select * from tempusers where rowid in (
select min(rowid) from tempusers group by username);
Deepak Singh Rawat