Results 1 to 17 of 17

Thread: merge every few rows to one

  1. #1
    Junior Member
    Join Date
    Nov 2007
    Answers
    8

    merge every few rows to one

    hello,

    I have data that lookes like this:

    user grade
    ------ ------
    Adam 20
    Adam 11
    Adam 99
    Roy 10
    Roy 55
    Adam 100



    what I need to show is only the top grade of each user (and it has to be sorted)

    like that:

    user grade
    ------ ------
    Adam 100
    Roy 55


    I had so much troubles trying to figure it out.

    what do i add to the sql:

    SELECT tblGrades.user, tblGrades.Grade
    FROM tblGrades
    ORDER BY tblGrades.Grade desc;

    (if I add: GROUP BY tblGrades.user, tblGrades.Grade it does not help me)

    anyone knows how to perform that?
    thank you :-)


  2. #2
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    Re: merge every few rows to one

    SELECT tblGrades.user, max(tblGrades.Grade) FROM tblGrades
    group by tblgrades.user
    ORDER BY tblGrades.user;


  3. #3
    Junior Member
    Join Date
    Nov 2007
    Answers
    8

    Re: merge every few rows to one

    hi
    thank you!

    why can't I sort it by the field of the grade?
    (you gave me a solution to sort it by the name)

    I added the Blue code but it wont work.

    SELECT tblGrades.user, max(tblGrades.Grade) AS grad
    FROM tblGrades
    group by tblgrades.user
    ORDER BY grad;

    thanx again!




  4. #4
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    Re: merge every few rows to one

    There is no error in ur code.Try this also
    SELECT tblGrades.user, max(tblGrades.Grade) grad
    FROM tblGrades
    group by tblgrades.user
    ORDER BY max(tblGrades.Grade);


  5. #5
    Contributing Member
    Join Date
    Sep 2007
    Answers
    35

    Re: merge every few rows to one

    hi

    from this u can get it.
    eg:
    select usr,max(grade) from grd group by usr


  6. #6
    Junior Member
    Join Date
    Nov 2007
    Answers
    5

    Thumbs up Re: merge every few rows to one

    Quote Originally Posted by rumbleEye View Post
    hello,

    I have data that lookes like this:

    user grade
    ------ ------
    Adam 20
    Adam 11
    Adam 99
    Roy 10
    Roy 55
    Adam 100



    what I need to show is only the top grade of each user (and it has to be sorted)

    like that:

    user grade
    ------ ------
    Adam 100
    Roy 55


    I had so much troubles trying to figure it out.

    what do i add to the sql:

    SELECT tblGrades.user, tblGrades.Grade
    FROM tblGrades
    ORDER BY tblGrades.Grade desc;

    (if I add: GROUP BY tblGrades.user, tblGrades.Grade it does not help me)

    anyone knows how to perform that?
    thank you :-)
    select uname, max(grade) from aa group by uname


  7. #7
    Junior Member
    Join Date
    Nov 2007
    Answers
    8

    Re: merge every few rows to one

    hi
    thank you all.


    thanx susarlasireesha, I didn't try the order by max(grade)
    didn't think it is possible.
    this is what made it work. :-)


    my problem now is that I have added one more field to tblGrades:
    subject




    if I only add to the SELECT the tblGrades.subject, and to the GROUP BY i add tblGrades.subject,
    all the data that is arriving is messed up. I simply get more than one user.
    (for example: Adam appears more than once in my results).

    this is my code


    Code:
    SELECT tblGrades.user, tblGrades.subject, max(tblGrades.Grade) grad
    FROM tblGrades
    group by tblgrades.user, tblGrades.subject
    ORDER BY max(tblGrades.Grade);
    why can't it simply still group the user to appear unique (only one user name, his grade and subject)?

    What I get is that bad result:


    Code:
    user     Grade  Subject
    -----      --------  ----------
    Adam 100 math
    Adam 99 history
    Adam 20 geology
    Adam 11 lidriture
    Roy 55 sports
    Roy 10 science

    and what I want to get is this:

    Code:
    user     Grade  Subject
    -----      --------  ----------
    Adam 100 math
    Roy 55 sports

    why cant i make it work?


  8. #8
    Contributing Member
    Join Date
    Jul 2007
    Answers
    42

    Re: merge every few rows to one

    select tblGrades.user,grade,sub from tblgrades group by tblGrades.user,grade,sub having max(grade) in(select max(grade) from tblgrades group by tblGrades.users) order by grade desc;


    hi one doubt how is use column name user i.e user is keyword.

    Last edited by priyasp_msit; 11-30-2007 at 07:21 AM.

  9. #9
    Contributing Member
    Join Date
    Nov 2007
    Answers
    53

    Re: merge every few rows to one

    Quote Originally Posted by rumbleEye View Post
    hello,

    I have data that lookes like this:

    user grade
    ------ ------
    Adam 20
    Adam 11
    Adam 99
    Roy 10
    Roy 55
    Adam 100



    what I need to show is only the top grade of each user (and it has to be sorted)

    like that:

    user grade
    ------ ------
    Adam 100
    Roy 55


    I had so much troubles trying to figure it out.

    what do i add to the sql:

    SELECT tblGrades.user, tblGrades.Grade
    FROM tblGrades
    ORDER BY tblGrades.Grade desc;

    (if I add: GROUP BY tblGrades.user, tblGrades.Grade it does not help me)

    anyone knows how to perform that?
    thank you :-)
    Dear rumbleEye,

    Try this and reply

    select max(grade),name
    from table_name
    group by name
    order by name

    I hope this will work for you.

    Have a pleasant time....


  10. #10
    Junior Member
    Join Date
    Sep 2007
    Answers
    10

    Re: merge every few rows to one

    select distinct(tblGrades.user),tblGrades.subject,max(tblGrades.Grade)grad
    from tblGrades
    group by tblgrades.user,tblGrades.subject
    order by max(tblGrades.Grade)


  11. #11
    Administrator
    Join Date
    May 2006
    Answers
    331

    Re: merge every few rows to one

    Hello,

    try this ..

    Select distinct c.usern, c.grade, b.subject from tblgrades b,
    (select usern, max(grade) grade from tblgrades group by usern) c
    where b.usern= c.usern
    and b.grade= c.grade

    sample data i have used
    usern grade subject
    adam 20 geology
    adam 11 litriture
    adam 99 history
    adam 100 math
    roy 10 science
    roy 55 sports
    james 10 oracle

    result
    usern grade subject
    adam 100 math
    roy 55 sports
    james 10 oracle

    thanks,

    Quote Originally Posted by rumbleeye View Post
    hello, i have data that lookes like this: user grade
    ------ ------
    adam 20
    adam 11
    adam 99
    roy 10
    roy 55
    adam 100 what i need to show is only the top grade of each user (and it has to be sorted) like that: user grade
    ------ ------
    adam 100
    roy 55 i had so much troubles trying to figure it out. What do i add to the sql: select tblgrades.user, tblgrades.grade
    from tblgrades
    order by tblgrades.grade desc; (if i add: group by tblgrades.user, tblgrades.grade it does not help me) anyone knows how to perform that?
    thank you :-)



  12. #12
    Contributing Member
    Join Date
    Nov 2007
    Answers
    53

    Re: merge every few rows to one

    Dear Friend,

    Try this and check

    select NAME,SUBJECT,MARKS
    from TABLE_NAME where (NAME,MARKS) in (
    select NAME nm ,max(MARKS) mrk
    from TABLE_NAME
    group by NAME)
    order by NAME

    The above query first finds the maximum marks out of all the subjects, of a particular student. Then after based on the Name and Marks, it will select the
    subject name.

    Try this i hope it will work

    Reply soon


  13. #13
    Contributing Member
    Join Date
    Nov 2007
    Answers
    53

    Re: merge every few rows to one

    Quote Originally Posted by rumbleEye View Post
    hello,

    I have data that lookes like this:

    user grade
    ------ ------
    Adam 20
    Adam 11
    Adam 99
    Roy 10
    Roy 55
    Adam 100



    what I need to show is only the top grade of each user (and it has to be sorted)

    like that:

    user grade
    ------ ------
    Adam 100
    Roy 55


    I had so much troubles trying to figure it out.

    what do i add to the sql:

    SELECT tblGrades.user, tblGrades.Grade
    FROM tblGrades
    ORDER BY tblGrades.Grade desc;

    (if I add: GROUP BY tblGrades.user, tblGrades.Grade it does not help me)

    anyone knows how to perform that?
    thank you :-)




    Dear Friend,

    Try this and check

    select NAME,SUBJECT,MARKS
    from TABLE_NAME where (NAME,MARKS) in (
    select NAME nm ,max(MARKS) mrk
    from TABLE_NAME
    group by NAME)
    order by NAME

    The above query first finds the maximum marks out of all the subjects, of a particular student. Then after based on the Name and Marks, it will select the
    subject name.

    Try this i hope it will work

    Reply soon


  14. #14
    Junior Member
    Join Date
    Nov 2007
    Answers
    8

    Re: merge every few rows to one

    Thank you all !

    * I gave a try to the distinct command, But it didnt work.

    * about the other solution, I beleive that adding another Select within the existing one will slow the resalts (later when the data will grow bigger) am I right?

    aventually, it only worked with what ADMIN suggested.
    (a combination of distinct command and the select inside a select)

    still, if Adam got twice 100 (wich is possible- I mean in two different subjects) it shows Adam twice.

    user grade subject
    ----- ------ --------
    Adam 100 math
    Adam 100 sports
    Roy 50 history



    I have tryed sreekumar_nair_it's solution.
    I started testing it but didnt quite manage yet.
    Im going to try it some more.


  15. #15
    Contributing Member
    Join Date
    Nov 2007
    Answers
    53

    Re: merge every few rows to one

    Quote Originally Posted by rumbleEye View Post
    Thank you all !

    * I gave a try to the distinct command, But it didnt work.

    * about the other solution, I beleive that adding another Select within the existing one will slow the resalts (later when the data will grow bigger) am I right?

    aventually, it only worked with what ADMIN suggested.
    (a combination of distinct command and the select inside a select)

    still, if Adam got twice 100 (wich is possible- I mean in two different subjects) it shows Adam twice.

    user grade subject
    ----- ------ --------
    Adam 100 math
    Adam 100 sports
    Roy 50 history



    I have tryed sreekumar_nair_it's solution.
    I started testing it but didnt quite manage yet.
    Im going to try it some more.
    Dear rumbleEye,

    Thanks for your kind reply.

    Yes you are right. Writing a select inside another will slow down the performance, but only if you are using in-built functions with column
    names (for. e.g. count(column_name),distinct(column_name),max(column_name) etc) or making multiple join conditions or subqueries.

    I tried the same on one of the transaction table of my organization consisting of 76000 rows
    and the query execution time is 138 msec.

    Dont worry,It wont be that much slow.

    Even if you are not satified with the solution, we will find other solution together dont worry.

    Now your question is, what if ADAM scores 100 in both MATHS and SPORTS.

    Kindly specify your requirement in such conditions, i mean what is the Output needed in such conditions.

    Have a pleasant time


  16. #16
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    Re: merge every few rows to one

    select a.name,max(a.marks),a.subject from tn a
    where (a.name,a.marks ) in (select name,max(marks) from tn group by name)
    group by a.name,a.subject


  17. #17
    Contributing Member
    Join Date
    Sep 2005
    Answers
    91

    Re: merge every few rows to one

    this one works... as admin said..

    Select distinct c.usern, c.grade, b.subject from tblgrades b,
    (select usern, max(grade) grade from tblgrades group by usern) c
    where b.usern= c.usern
    and b.grade= c.grade

    just tired myself with the same sample table


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact