SQL : group by

Discussion in 'Databases' started by ocm1, Nov 30, 2009.

  1. #1
    [​IMG]

    how to display all column info when using group by ?
     
    ocm1, Nov 30, 2009 IP
  2. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #2
    You could try using the MIN() function.

    select bookid, count(bookid) as bookunit, sum(soldout) as soldout, min(bookname) as bookname, min(booksam) as booksam, ..... etc
    from table1
    group by bookid

    As long as everything is the same for the same bookid then this should work.
     
    rayqsl, Nov 30, 2009 IP
  3. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Try following query..

    SELECT BookName, BookSam, Local, PrintDate, BookId, COUNT(BookId) AS BookUnit, SUM(SoldOut) AS SoldOut FROM table1 GROUP BY BookId
     
    mastermunj, Nov 30, 2009 IP
  4. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #4
    The above won't work. As soon as you use "GROUP BY", everything in the select list has to be a column in the group by clause or an aggregation.
     
    rayqsl, Dec 1, 2009 IP
  5. Traffic-Bug

    Traffic-Bug Active Member

    Messages:
    1,866
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Second that. Everything that is not an aggregate calculation in the SELECT List must appear in the GROUP BY Clause also, otherwise the sql engine throws an error.
     
    Traffic-Bug, Dec 1, 2009 IP
  6. kokoriko

    kokoriko Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    GROUP BY 'BookName' at the end of your query will do the same but order the book name and output like java, java, java, java, java, java, asp, asp
     
    kokoriko, Dec 12, 2009 IP
  7. m.iqbalkh

    m.iqbalkh Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi Guys!

    mastermunj 's SQL statement will work for mySQL but not in MSSQL/Oracle.

    In mySQL it is not necessary to specify all the coulmns (which appears in select) in group by. But in Other databases you must ned to specify it.

    for exaple:
    -- //** This will work in mySQL but not in other databases **//--
    SELECT COUNT(*), Field1,Field2 from TABLE GROUP BY Field1;

    -- //** This will work for all databases **//--
    SELECT COUNT(*), Field1,Field2 from TABLE GROUP BY Field1,Field2;
     
    m.iqbalkh, Dec 16, 2009 IP
  8. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #8
    Yeah, I've read that MySQL is pretty flexible in what SQL it runs which can be a good thing or a bad thing (bad if you want to run the code in MSSQL or Orcale at a later date). ocm1 didn't say which SQL was being used so safer to use SQL that should work in all SQL engines.
     
    rayqsl, Dec 22, 2009 IP