Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help with mysql query

Status
Not open for further replies.

vicmad

Technical User
Feb 11, 2010
13
CA
I'm running SNMP Zenoss connected to mysql database.I'm trying to select the device that fails the most from the
database.
Column name called "devices" has all the device names.
Column name called "events" has all the events that are generated.
How can i write a query that will display the device that fails the most in a month?

petrosky suggested the below query:
SELECT Devices, SUM(`Events`) AS Total FROM devices
GROUP BY Devices
ORDER BY Total DESC LIMIT 1

The above query works and displays the device that fails the most.
What I also need is that it should display the event from the "events" column.

Please help and thanks in advance.
 
What have you tried so far? Are you expecting that someone here will just do the work for you, or did you get stuck trying to figure it out yourself?

-----------------------------------------
I cannot be bought. Find leasing information at
 
Add "events" to your select and group by statements. Personally, I would have used something like this...

SELECT * from (
SELECT devices, events, SUM(*) AS Total
FROM devices
GROUP BY devices, events) as x
ORDER BY Total DESC

Now you can see all failing devices in descending order.
 
kozusnik, i don't think [red]SUM(*)[/red] is valid SQL

vicmad, i don't think SUM(events) is right either, unless events is actually a numeric value that can be summed up

i think this is what you want --
Code:
SELECT Devices
     , [blue]COUNT(*)[/blue] AS number_of_events 
  FROM devices
GROUP 
    BY Devices
ORDER 
    BY number_of_events DESC LIMIT 1
:)


r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
My apologies. I was thinking COUNT(*). Of course SUM(*) won't work. Thanks. :)
 
I like you thank everybody for all the help. You guys rock!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top