0

假设我们有 3 列,所有整数,第一列,id第二列X和第三列Y,其中X1实例何时发生,0何时不发生,以及该实例Y1机会何时发生,0何时不发生。

=================

|  id  | X  | Y | 

=================

| 1    | 1  | 1 | 

| 1    | 0  | 1 | 

| 1    | 0  | 0 | 

| 1    | 1  | 1 | 

| 2    | 0  | 0 | 

| 2    | 0  | 1 |

| 2    | 1  | 1 |  

| .... |    |   | 

=================

我想要做的是通过并基本上X / Y以百分比的形式找到,
即:

(total number of times an instance occurred / total number of opportunities for that to occur, and this all to be grouped by id) * 100

我有哪些有效的查询(但我想知道是否有更有效的方法)是

select id, (CASE WHEN ( sum(Y)) <> 0 THEN ((sum(X))/( sum(Y))) ELSE 0 END) ) * 100) from table GROUP BY id

我想知道它是否对某些人更有效怎么做

sum(cast(case where Y <> 0 THEN (X/Y) ELSE 0 END 0...
4

0 回答 0