I have a table with columns A, B and C. Column A might have duplicates.
I need a query that will get me a resultset with unique values in column A, and I don't care which possible duplicate it takes.
I don't know anything beforehand about the rest of the data.
An example might be:
A B C
1 8 8
1 7 7
2 10 10
In this case I'd want to select:
A B C
1 x x
2 10 10
x = It doesn't matter which value it would pick.
Kind regards,
Matthias Vance
Edit
I thought I found my solution with:
SELECT * FROM (
SELECT * FROM test GROUP BY a
) table_test;
But that wasn't working after all.
This will result in:
[Microsoft][ODBC Excel Driver] Cannot group on fields selected with '*'
Wouldn't this simple query work:
It groups by A and just selects the minimum values of B and C in the rows of A. The values of B and C might come from different rows, e.g.
would return
This works in SQL Server 2008, which illustrates the concept. You need a unique column.
Seeing that you're using Excel, I'd use the same principle. Add another column to the spreadsheet and make sure it is unique. Use that column as your ID-column.
I know this is a dirty way, but will work this case.
Pseudo code:
create table #tmpStaging with primary key as col( A )
for each row in the flatFile/excel/whatever begin begin try insert into #tmpstaging end try
begin catch --do nothing end catch end
select * from #tmpstaging will give you rows without dups
Try this,
I didn't tried it yet... who knows :)
Try this:
But since it contains correlated subquery it might be slow. (OTOH it is at least theoretically possible for database engine to optimize it into something I show below.)
What about something outside SQL? What are you going to do with result?
If you are going to process it with some program, why not just get:
and then do something like:
in your application?
I don't know PHP but I guess it would be something like this: