Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Simple SQL Query Expand / Collapse
Author
Message
Posted Sunday, December 30, 2012 7:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, December 30, 2012 8:59 AM
Points: 3, Visits: 6
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!
Post #1401248
Posted Sunday, December 30, 2012 8:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, December 30, 2012 8:59 AM
Points: 3, Visits: 6
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!
Post #1401250
Posted Sunday, December 30, 2012 8:13 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, May 17, 2013 3:51 PM
Points: 376, Visits: 165
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

Post #1401251
Posted Sunday, December 30, 2012 8:39 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 4:11 PM
Points: 37,741, Visits: 30,020
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 2008, MVP
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

Post #1401253
Posted Sunday, December 30, 2012 9:02 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, December 30, 2012 8:59 AM
Points: 3, Visits: 6
ah sorry I wasn't aware there was a difference between them!
Post #1401254
Posted Sunday, December 30, 2012 10:06 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562, Visits: 3,451
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----------
While 1 = 1 (Learning SQL....)
Click to get fast response of your post
Post #1401285
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse