How to write query with groupby

  • Hi,

    How can we write the query using groupby. I need to have group by only one column. Is it possible through subquery?

    select col1, col2, col3

    from testtable

    group by col1.

  • Suppose

    select col1, min(col2), max(col2) , count(col2)

    from mytable

    group by col1

    returns 1 , -10, 5000, 1000

    What do you mean, select col1, col2 ... ? Which col2 value?

  • ramana3327 (1/21/2015)


    Hi,

    How can we write the query using groupby. I need to have group by only one column. Is it possible through subquery?

    select col1, col2, col3

    from testtable

    group by col1.

    Can you show is the homework question?



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Use some aggregate function like MAX() , MIN() for col2 and col3, since on grouping multiple row would be shown as single row.

  • You need to use any aggregate function on columns col2, col3 as these are not part of group by clause.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply