You must Sign In to post a response.
  • Category: ASP.NET

    How to fetch record from resulted table?

    I come across a situation where after executing query getting result like this

    StdId Studentname Subcode Grade
    1 Pinky 1 A
    1 pinky 2 A
    1 leena 1 A
    1 leena 2 A
    1 leena 3 B
    2 Rupali 1 A
    2 Megha 1 A

    Grade are from A to D.I want to show record only once when grade is same for same stid,does not matter what is subcode and studentname.If grade is different for same stid,show both record. Here I want to show

    StdId Studentname Subcode Grade
    1 leena 2 A
    1 leena 3 B
    2 Rupali 1 A

    How to do that?
  • #751957
    The ToTable method is a really helpful and easy to use method, where the first parameter is of Boolean type which when true means it will return distinct (unique) rows (records), while the next parameters are the name of the Columns whose data you want to be distinct (unique).

    //Assuming dt is the datatable containing your original data


    dt = dt.DefaultView.ToTable(true, "stId", "studentName", "grade");

    Let me know if this solves your problem

  • #751961
    Hi,

    I can not understand your exact requirement, can you please elaborate more?
    Update your question with more detailed and exact requirement to get more responses from forum members.
    I hope you understand.

    Regards,
    Nirav Prabtani (Senior Web Developer)
    Email : niravjprabtani@gmail.com
    blog : niravprabtani.blogspot.in

  • #751970
    Try this:
    Select
    Max(StdId),
    Max(Studentname),
    Max(SubCode),
    Max(Grade)
    From
    YourTable
    Group By
    StdId, Grade

    I hope this is what you are looking for. Let me know if solves your problem or have still problem.

  • #751971
    Thanks Avinesh for ur reply.It really helped me.
    But problem occure when I am grouping stdid and grade,it take max studentname and studentcode.Here I added one record more to table

    id StudentName SubCode Grade Stdid
    3 leena 1 A 1
    4 leena 2 A 1
    5 leena 3 B 1
    1 pinky 1 A 1
    2 pinky 2 A 1
    8 smita 1 A 1
    7 Megha 1 A 2
    6 Rupali 1 A 2

    Result :
    stid Studentname SubCode Grade
    1 leena 3 B
    1 smita 2 A
    2 Rupali 1 A

    If I am fetching smita,then subcode for smita should come not max.Subcode for smita is 1 not 2.I think max should apply either on studentname or subcode not both.If I applied on one group by will not work.How to handle this?

  • #751984
    Hi,

    I can't find out the type of data you wanted to fetch. Can you explain your requirement more clearly with a detailed sample record.


    Regards,
    Asheej T K

  • #751997
    If am not wrong then you are looking the latest updated record by id

    If this is the case then.

    Use
    Select Max(Id),
    Stdid, Max(StudentName),
    Grade, Max(SubCode)
    ....
    ...same as above.

    Sorry I don't have SQL server so I can't test this buy yeah this will work out for you.


  • Sign In to post your comments