0
table : metrics

columns:

1. name : Name
2. instance: A name can have several instances 
(Name: John, Instances: John at work, John at concert)
3. metric: IQ, KQ, EQ
4. metric_value: Any numeric

Objective of the query

Find out the metrics whose metric_value is 0 for all instances for all names.

Nature of data

A name's metric 'M' for instance 'X' could be 10. But for the same name and the same metric instance 'Y' could be 0. In this case, 'M' should NOT be returned.

Edit: Sample data:

NAME    INSTANCE    METRIC  VALUE
John    At work         IQ  0
John    At home         EQ  10
John    At a concert    KQ  0
Jim At work         IQ  0
Jim At home         KQ  0
Tina    At home         IQ  100
Tina    At work         EQ  0
Tina    At work         KQ  0

In this case, only KQ should be returned since it is always zero for all Names and their instances.

4

4 に答える 4

0

データに基づくリスト:

SELECT name, metric FROM metrics GROUP BY name, metric HAVING SUM(metric_value) = 0
于 2013-05-31T06:17:51.393 に答える
0

他の回答のほとんどは、それmetricが肯定的であると想定しています。OPは、任意の数値にすることができると言います。これが機能するための 2 つの方法を次に示します。

絶対値の合計を確認します。

SELECT metric
FROM metrics
GROUP BY metric
HAVING SUM(abs(metric_value)) = 0

ゼロ以外の値がないことを明示的に確認します。

SELECT metric
FROM metrics
GROUP BY metric
HAVING SUM(case when metric_value <> 0 and metric_value is not null then 1 else 0 end) = 0
于 2013-05-31T07:19:02.520 に答える
0

このようなものをお探しですか?

SELECT metric
  FROM metrics
 GROUP BY metric
HAVING SUM(metric_value) = 0

これがSQLFiddleのデモです

更新metric_value が負の値を持つことができる場合は、これを使用します

SELECT metric
  FROM metrics
 GROUP BY metric
HAVING SUM(ABS(metric_value)) = 0

これが更新されたSQLFiddleデモです

于 2013-05-31T06:21:18.427 に答える