Friday, March 9, 2012

Query to do chart

I have a set of events where a device has different errors
say I have the table
-- devEvent --
with the fields
<devModel>
<devSerialNumber>
<eventType>
<eventTime>
and I want to get something that can be accomplished manually like
SELECT DISTINCT(devModel) from devEvent
and loop throught all the results to do the following query
SELECT COUNT(eventType) FROM devEvent WHERE devSerialNumber = 'XXX'
Where XXX are all the results from the first query
Any Help-Pointers will be greatly appreciated
LAYou could just do something like:
select devSerialNumber, count(eventType) as numberEvents
from devEvent
group by devSerialNumber
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"LuisA" <lamezcua@.gmail.com> wrote in message
news:1137626878.884765.270320@.g49g2000cwa.googlegroups.com...
>I have a set of events where a device has different errors
> say I have the table
> -- devEvent --
> with the fields
> <devModel>
> <devSerialNumber>
> <eventType>
> <eventTime>
> and I want to get something that can be accomplished manually like
> SELECT DISTINCT(devModel) from devEvent
> and loop throught all the results to do the following query
> SELECT COUNT(eventType) FROM devEvent WHERE devSerialNumber = 'XXX'
> Where XXX are all the results from the first query
> Any Help-Pointers will be greatly appreciated
> LA
>|||I knew it was simple for someone wha knows this.
I guess I'll have to revise my "understanding" of the GROUP BY statement|||Well, now you know this too :)
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"LuisA" <lamezcua@.gmail.com> wrote in message
news:1137686607.549777.70120@.g44g2000cwa.googlegroups.com...
>I knew it was simple for someone wha knows this.
> I guess I'll have to revise my "understanding" of the GROUP BY statement
>|||Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it instead of useless personal code.
Next, we do not do loops in SQL; this is a DECLARATIVE language, not a
PROCEDURAL language. Finall, ISO-11179 does not use "camelCase"
names; they are a XXXXX to read and understand.
Finally, let's get back to the basics of an RDBMS. Rows are not
records; fields are not columns; tables are not files; there is no
sequential access or ordering in an RDBMS.
I think, based on wild guesses and not what you posted, you want:
SELECT COUNT(event_type)
FROM Events
WHERE serial_nbr
IN (SELECT dev_model
FROM Events);

No comments:

Post a Comment