2017-03-08 4 views
1

coolumnの内容に依存して計算し、別の列の内容は、私が持つテーブルを持っている分子や分母TSQL私はコラムの内容のdependendenceにTSQLで計算したい

であることschould以下の内容:

+------------+----------+----+-------------------------+ 
| machine-nr | duration | ID | timestamp    | 
+------------+----------+----+-------------------------+ 
| 1108  | 47  | 11 | 2017-02-11 00:08:15.000 | 
| 1112  | 195  | 2 | 2017-02-11 00:13:13.000 | 
| 1108  | 339  | 7 | 2017-02-11 00:13:54.000 | 
| 1108  | 19  | 2 | 2017-02-11 00:13:54.000 | 
| 1112  | 20  | 11 | 2017-02-11 00:13:33.000 | 
+------------+----------+----+-------------------------+ 

は、今私はID 11を持つすべての期間は配当であるべき計算
のこの種及び11、2と7の中でもschould IDを持つすべての期間の合計をしたいです除数。そのような 何か(私はそれがそのように動作doesntの知っている):

SUM(duration if ID = 11)/SUM(duration if ID =11) + SUM(duration if ID = 2) + SUM(Duration if ID = 7) 
group by machine-nr 

私はあなたが私が何を意味するか知っている願っています。

+0

'tsql'は' mysql'ではありません...なぜ両方のタグですか? – Rahul

答えて

0
select machine_nr, 
     sum(case when id = 11 then duration end) 
     /sum(case when id in (11,2,7) then duration end) as end_calc 
from MyTable 
group by machine_nr 
+0

ありがとうございます。 – Jan