sql - Conditional Distinct -


Sorry for a little introduction, but I want to make it clear what I'm trying to do. If someone is able to come up with a more appropriate title, please feel free to edit.

I wrote an SNMP collector who checks every switch in our data center once per hour and checks which ports are online, and MS SQL 2's 12 DB DB inspires it It was often that administrators do not report a blocked server or any other device and we are running out of switch ports.

The DB schema looks like this (simplified screenshot):

db The schema

Interface table is a child's table for crawl (crawl = run) the number of the SNMP collector) is not constant for every switch as the number of interfaces but changes between the crawls, like That line card is included or removed.

Now I want to write a query that switch every interface that always had 2 ifOperStatus value and ifOperStatus of 1 was never there.

I have written a query of three nested sub-questions, it is slow to read and hell. There must definitely be an easy way.

My view was to filter the ports that have never been changed by using

  (COUNT (DISTINCT dbo.interfaces.ifOperStatus) = 1) < / Code>  

and during the last crawl, ifOperStatus 2 has an internal entry with a list of ports. Ugly, as I said.

Then, a sample output from DB will look like this:

 Sample Questions

And I'm looking for a query that gives lines 5-7 because ifOperStatus never changes, but does not return lines 3-4 because ifOperStatus flapped. / p>

How about

  HAVING (MIN (dbo.Interfaces. ifOperStatus) = 2 and max (dbo.interfaces.ifOperStatus) = 2)   

MIN and MAX The server is not required, one set of all the prices seen so far, the highest / lowest it can also be avoided the need to connect to the list of ports that had 2 ifOperStatus during the previous crawl.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -