ASP.NET - how to select distict value and order by unique value sql

Asked By mani on 03-Feb-12 10:15 AM
hi..i have table with column as

sno    modelname
01    nokia
02   Samsung
03   LG
04   Nokia
05    LG
06    Nokia

how to get distinct value of modelname order by sno..
???any help??
D Company replied to mani on 03-Feb-12 10:42 AM
Hello Friend.

use distinct clause , try this query

Select DISTINCT modelname from tablename OEDER BY sno

let me know if it is not working.
Regards
D
D Company replied to mani on 03-Feb-12 10:43 AM
there was a typo in last post please ignore this line.

Select DISTINCT modelname from tablename OEDER BY sno

its

Select DISTINCT modelname from tablename ORDER BY sno
mani replied to D Company on 03-Feb-12 10:44 AM
i used u r query like this but getting error as

Select DISTINCT modelnumber from postquries ORDER BY sno


ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
D Company replied to mani on 03-Feb-12 11:27 AM
use this query,

Select DISTINCT sno,modelnumber from postquries ORDER BY sno

Regards
D
dipa ahuja replied to mani on 03-Feb-12 12:25 PM
Try this way:
 
SELECT DISTINCT modalname
FROM   table1
ORDER BY sno
Sandeep Mittal replied to mani on 04-Feb-12 12:37 AM
When distinct clause is used in "SELECT" stament, it is mandatory to have all the columns used in  "ORDER BY" should exists in "SELECT"

DECLARE @TAB TABLE(
  sno varchar(10), modelname varchar(10)
)
INSERT INTO @TAB   
SELECT '01','nokia'
UNION ALL SELECT '02','Samsung'
UNION ALL SELECT '03','LG'
UNION ALL SELECT '04','Nokia'
UNION ALL SELECT '05','LG'
UNION ALL SELECT '06','Nokia'
 
-- This might give you duplicate values if same modelname is repeated for different sno as in your given data
SELECT DISTINCT sno, modelname
FROM   @TAB
ORDER BY sno
 
-- Use like this
SELECT DISTINCT modelname
FROM (
  SELECT  TOP 100 PERCENT modelname
  FROM    @TAB
  ORDER BY sno
) TAB