Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server 2008 - General
»
Simple SQL Query
Simple SQL Query
Rate Topic
Display Mode
Topic Options
Author
Message
burgergetsbored
burgergetsbored
Posted Sunday, December 30, 2012 7:19 AM
Forum 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
burgergetsbored
burgergetsbored
Posted Sunday, December 30, 2012 8:06 AM
Forum 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
raym85
raym85
Posted Sunday, December 30, 2012 8:13 AM
Old 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
GilaMonster
GilaMonster
Posted Sunday, December 30, 2012 8:39 AM
SSC-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
burgergetsbored
burgergetsbored
Posted Sunday, December 30, 2012 9:02 AM
Forum 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
Bhuvnesh
Bhuvnesh
Posted Sunday, December 30, 2012 10:06 PM
SSCrazy
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 »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.