Simple SQL Query

  • I have the following table in my database:

    dataName graph yesNo

    data1 pie 1

    data1 pie 1

    data1 bar 0

    data1 line 1

    Using this query: select graph, sum(yesNo) as count from graphTable where dataName = 'data1' group by graph; I get back the following:

    pie 2

    graph 0

    line 1

    What I want to do is only return the visual that has the highest number next to it rather than all of them, am I able to do this using SQL? Cheers!

  • I managed to get it working now by adding a ORDER BY count DESC LIMIT 1 to the end to return only the highest value. If there's a better way of doing it let me know!

  • Thats how I would have done it.

    Select TOP 1 COUNT

    FROM(

    select graph, sum(yesNo) as count

    from graph where dataName = 'data1' group by graph)J

    order by count desc

  • burgergetsbored (12/30/2012)


    I managed to get it working now by adding a ORDER BY count DESC LIMIT 1 to the end to return only the highest value. If there's a better way of doing it let me know!

    LIMIT 1 is a MySQL command, not a T-SQL. If this is a MySQL database, you're better off asking this on a MySQL forum, not a SQL Server forum. The SQL syntax does differ between the two database engines.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • ah sorry I wasn't aware there was a difference between them!

  • burgergetsbored (12/30/2012)


    What I want to do is only return the visual that has the highest number next to it rather than all of them, am I able to do this using SQL? Cheers!

    Actually you are asking for second highest right ? you can use row_number() . see http://msdn.microsoft.com/en-us/library/ms186734(v=sql.105).aspx

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

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

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