SQL Server - how to select record based on distinct one column ?
Asked By mani on 29-Mar-12 05:43 AM
hi...i have sareenumber with multiple duplicate value in my table...and i need select record based on distinct sareenumber..
any help??
kalpana aparnathi replied to mani on 29-Mar-12 05:51 AM
hi,
Using this for getting records,
Select * from tbl_name group by sareeno=&sareeno;
Or using distinct keyword
SELECT DISTINCT columnName(s)
FROM tableName
Regards,
Sandeep Mittal replied to mani on 29-Mar-12 05:51 AM
Please elaborate your requirement bit more clearly.
mani replied to Sandeep Mittal on 29-Mar-12 05:57 AM
i have saree number with duplicate values and i need to select record with distinct row based on sareenumber only..so i need to avoid duplicate record with sareenumber
Somesh Yadav replied to mani on 29-Mar-12 06:06 AM
chesk this,
a similar example is explained here,
http://social.msdn.microsoft.com/Forums/en/transactsql/thread/f1445312-cde7-4b62-8c05-e21fd8b2eb35
Hope it helps you.
Sandeep Mittal replied to mani on 29-Mar-12 06:07 AM
If you have different values in other columns for sareenumber, then you would not be able to get the distinct data of sareenumber using select *.
Distinct operator works on all columns used in select. if the whole row data is same then only it would give a single row for duplicate rows.
Post some sample data and desired output.
Pat Hartman replied to mani on 29-Mar-12 12:18 PM
If you have no unique identifier, how would you propose that the query decide which non-unique row to return? One possibility is the Top values predicate.
Select Top 1 yourtable.*
From yourtable
Where somefield = somevalue;