Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select Distinct

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2007-10-12 : 09:56:54

Something simple here, just not finding the right way.

I have the following:

SELECT DISTINCT fld1, fld2
FROM tbl1


Ok, this will bring back duplicates of fld1 because fld2 is different for the row.

What I need is no duplicates of fld1 but also bring back fld2 of that row.

Thanks,

Zath

Kristen
Test

22859 Posts

Posted - 2007-10-12 : 10:15:43
Need some sample data, and expected results, to better understand the question please

Otherwise we're just guessing!

Kristen
Go to Top of Page

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2007-10-12 : 10:18:48
Sorry, as the sql is above it will return:

fld1 fld2
x 2
x 3
y 5
z 0

What it needs to return is:

fld1 fld2
x 2
y 5
z 0

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-12 : 10:20:36
Isnt it as simple as this?

select fld1,min(fld2) as fld2 from table
group by fld1

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-12 : 10:22:31
Why

x 2

rather than

x 3

??

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-12 : 10:27:42
quote:
Originally posted by Kristen

Why

x 2

rather than

x 3

??

Kristen


Min, Max or any

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-12 : 10:32:14
Or, I expect, "First one in the database" ...
Go to Top of Page
   

- Advertisement -