• Need some help in MySQL Query

    BrittneTabor Member

    I’m rather new to SQL and I can’t figure out a decent way to do this query – I need to find out what ‘Name’ has the largest ‘in’ number.

    By the looks of this table it’s “SS” (5+2) so I need the query that would display “SS”. Also the timestamp is important because the query only has to find the largest ‘Name’ in August.

    I hope you can understand what I mean. Could anyone please help me with this ?

    Timestamp; Name; in; out
    2014-08-01 09:30:00;VV;1;2
    2014-08-02 09:30:00;SS;5;6
    2014-08-03 09:45:00;VV;3;4
    2014-08-01 09:45:00;SS;2;3
    2014-05-01 09:45:00;KK;2;3
    
  • ShikhaTan Member

    Haven’t really worked with mysql so it might be really simple I just can’t come up with a reasonable syntax

  • Amit Member

    It is going to be in the form of:

    SELECT column_name [, column_name ]
    FROM table1 [, table2 ]
    WHERE column_name OPERATOR
    (SELECT column_name [, column_name ]
    FROM table1 [, table2 ]
    [WHERE])
    

    Using sum and max functions.

    There is a great site that allows you to test things out:

    http://www.w3schools.com/sql/trysql.asp?filename=trysql_func_max

  • SapnaVishwas Member

    Can you provide the table and field names that will be used (not the data, just the table is SALES, field names are x,y,z …? That way the example code will need less work

  • ShikhaTan Member

    Table is named ‘stats’ and field names are like in the first post. (Timestamp, Name, in, out). That’s what you mean right ? The names are not that important I’m sure if I’ll have the syntax I can fix it to work with my db. Thanks again for your effort.

  • SapnaVishwas Member

    A bit too late maybe, but hope it helps..

    SELECT
      name AS name,
      SUM(in) AS sum_in
    FROM temp
      GROUP BY name
      ORDER BY sum_in DESC;
    
Viewing 5 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish