get unique records and un-sorted. RRS feed

  • Question

  • Hi Respected,
    I am working in .net, and according my client he want to see all record of a table uniquely, and does not want to  give order also.
    for doing this i did use DISTINCT, but my problem is when i used DISTINCT the query return sorted records that is why, i need guide because i dont want sorting, any one who has some idea pls guide that how can i get uniquely and physicaly stored records
    regards Rajat.
    Thursday, January 24, 2008 7:37 AM

All replies

  • Hello,


    First, the table to view is in SQL Server,Access or other database engine ?


    I don't understand what does that mean : to see all record of a table uniquely

    Do you mean that in your table you have "doublons" ( that means you ) or you have null values ?


    I suppose you are working with COUNT( DISTINCT field)


    Could you post your T-SQL code?

    So it will be easier to understand your problem ?


    Have a nice day


    Thursday, January 24, 2008 10:26 AM
  • Hi Papy Normand,
    thnsk for concerning my problem.
    okay i try to make easy the problem to understand you. I have a table where fields are id, data, answer, changername.
    i am trying to fill my listbox with name of chaptername okay.
    but problem is my table has many records where chaptername are same.
    but i want to see only unique chaptername in my listbox.
    i trying to access records from this query;
    select distinct chaptername from chapterInfo;
    it is giving the unique records, okay but it arrages all records in Assending order, that is what i do not want.
    i hope u got my problem.
    reply soon.
    regards Rajat.
    Friday, January 25, 2008 8:04 AM
  • Hello,


    Have you tested the group by ?

    See this link :





    SELECT changername FROM tablename GROUP BY changername


    If i remember, you may not include a ORDER BY clause when you are using a GROUP BY clause


    I hope that will help you


    Have a nice day


    Friday, January 25, 2008 8:24 AM
  • Hi Papy Normand,
    thanks to understand my problem.
    but Sir, i have done all, and for urs knowledge the group by and distinct are work same, and gives result sorted that is my problem. i dont want sorted records.
    think again bay
    regards Rajat.
    Friday, January 25, 2008 11:34 AM
  • Hello,


    I tested with GROUP BY and you're right : the results appear sorted


    I search in the T-SQL BOL : i found nothing about you want

    I fear that you have to do that with code in vb or vc#


    in VC# :

    SqlCommand cmd = new SqlCommand("SELECT chaptername from chapterinfo",conn);

    // conn is your SqlConnection

    SqlDataReader dr = cmd.ExecuteDataReader();


    while ( ! dr.Get() )


    string str = Convert.ToString(dr[0]);

    if ( !ListBox1.Items.Contains(str) )





    at the end of the while you have all distinct chaptername


    My code is not tested i wrote it from my memory so be careful, there is maybe an error, but the way i suggest you

    I hope this will help you


    Have a nice day


    Friday, January 25, 2008 4:05 PM

    You might try something like:


    Select LastName, Firstname From Employees Group By LastName, firstname Order By newid()

    Friday, January 25, 2008 5:31 PM
  • If you don't want the results sorted by chaptername, what do you want them sorted by?


    I'd guess that the answer is something like "I want them to show up in the order they were first inserted into the table," which is to say, by ID, assuming the ID's an identity column.  If that's the case, you can do this:


       SELECT chaptername, MIN(ID) as MinID

       FROM MyTable

       GROUP BY chaptername

       ORDER BY MinID


    Saturday, January 26, 2008 4:17 AM